我没有收到任何错误,但为什么我不能将数据插入数据库表?

时间:2014-06-13 05:08:41

标签: java derby

我尝试将数据插入到数据库表中,但它不能,并且没有检测到错误。如果有人可以帮我找到我的错误。我很高兴知道

这是我的servlet

Connection theConnection = null;

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String nameL = request.getParameter("name");
String questionL = request.getParameter("question");
String schemeL = request.getParameter("scheme");
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    try {
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet InputData</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet InputData at " + request.getContextPath () + "</h1>");
        out.println("</body>");
        out.println("</html>");
        out.println("<P align=center>");
        out.println("<form name=form1 action=\"http://localhost:8080/mywebapp/-\">");
        out.println("<p>");
        out.println("<input type=submit name=\"submit\" value=\"GET DATA FROM DB\">");
        out.println("</p>");
        out.println("</form>");

        // Load database driver
        try {
            Class.forName("org.apache.derby.jdbc.ClientDriver");
    } catch (ClassNotFoundException cnfe){
                System.out.println(cnfe);
    }
        // Create a Connection to contacts db Data source
        try {
            theConnection = DriverManager.getConnection(dbURI,"demo","demo");
    } catch (SQLException sqle) {
                System.out.println(sqle);
    }
        // prepare statement for inserting data into table
        try {

            str = "INSERT INTO DEMO.QUESTION "
                  + "(LECTURER_ID,QUESTION,SCHEME) "
                  + "VALUES ((Select LECTURER_ID,? from DEMO.LECTURER where LECT_NAME = '" +nameL+ "'), ?, ?)";

            PreparedStatement theStatement=theConnection.prepareStatement(str,Statement.RETURN_GENERATED_KEYS);

    //Set parameters for INSERT statement and execute it
    theStatement.setString(1, nameL);
    theStatement.setString(2, questionL);
    theStatement.setString(3, schemeL);
    theStatement.executeUpdate();

            ResultSet QUES_ID = theStatement.getGeneratedKeys();
            QUES_ID.next();
            int autoGeneratedID = QUES_ID.getInt(1);

    } catch (SQLException sqle) {
                System.out.println(sqle);
    }

            theConnection.close(); //Close database Connection
    }catch(Exception e) {
        out.println(e.getMessage());//Print trapped error.
} finally {
        out.close();
    }
}

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    processRequest(request, response);
} 

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    processRequest(request, response);
}

public String getServletInfo() {
    return "Short description";
}// </editor-fold>

这是我的html表单

 "            <form class=\"col-md-9\" name=\"form1\" action=\"InputQuestion\">\n" +

            "            <b><legend>Maklumat Soalan (Esei):</legend></b>\n" +   

            "            <label>Soalan :</label>\n" +
            "            <textarea class=\"form-control\" type=\"text\" name=\"question\"  placeholder=\"Soalan\" rows=\"6\"></textarea>\n" +

            "            <label>Skema Jawapan :</label>\n" +
            "            <textarea class=\"form-control\" type=\"text\" name=\"scheme\" placeholder=\"Skema Jawapan\" rows=\"6\"></textarea>\n" +

            "            <br>\n" +

            "            <input type=\"hidden\" name=\"name_lec\" value= "+ theResult.getString(2) +">\n" +
            "            <input type=\"submit\" name=\"submit\" value=\"Hantar\">\n" +
            "            <input type=\"reset\" name=\"reset\" value=\"Penetapan Semula\">\n" +

        "            </form>\n" +

0 个答案:

没有答案