servlet中的JDBC

时间:2014-12-17 10:22:04

标签: java servlets jdbc

我有一个简单的表单来从user获取数据并存储到数据库。

这是我的简单形式:

<!DOCTYPE html>
<html>
<head>
    <title> Database Insertion Form </title>
</head>
<body>

</body>
<h1>
    <center> Phonebook</center>
</h1>
<Form method=post action="DbServlet">

    <pre>
        Surname: <input type="text" name="Surname">
        Forename: <input type="text" name="Forenames">
        Phone number: <input type="text" name="PhoneNum">
    </pre>
    <br><br>
    <center><input type="submit" VALUE="Commit"></center>
</Form>
</html>

这是DbServlet Servlet:

@WebServlet("/DbServlet")
public class DbServlet extends HttpServlet {

    private PreparedStatement ps;
    private Connection connection;
    private String URL = "jdbc:mysql://localhost/lib";

    @Override
    public void init() throws ServletException {
        super.init();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(URL, "root", "2323");
        } catch (SQLException e) {
            e.printStackTrace();
            System.exit(1);
        } catch (ClassNotFoundException cnfe) {
            cnfe.printStackTrace();
        }
    }

    public void doPost(HttpServletRequest request,HttpServletResponse response)
                                         throws IOException, ServletException {
        String surname, forenames, telNum;
        surname = request.getParameter("Surname");
        forenames = request.getParameter("Forenames");
        telNum = request.getParameter("PhoneNum");
        response.setContentType("text/HTML");
        PrintWriter out = response.getWriter();

        out.println("<HTML>");
        out.println("<HEAD>");
        out.println("<TITLE>Servlet + JDBC</TITLE>");
        out.println("</HEAD>");
        out.println("<BODY>");
        String query = "INSERT INTO PhoneNums"
                + " VALUES('" + surname + "','"
                + forenames + "','" + telNum + "')";

        try {
            ps = connection.prepareStatement(query);
            ps.executeUpdate();
            JOptionPane.showMessageDialog(null, "Done");
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        }

        out.println("</BODY>");
        out.println("</HTML>");
        out.flush();
    }

    @Override
    public void destroy() {
        try {
            connection.close();
        } catch (Exception ex) {
            System.out.println(
                    "Error on closing database!");
            ex.printStackTrace();
            System.exit(1);
        }
    }
}

当我点击html表单中的submit按钮时,没有任何反应,甚至是任何异常!

MySQL连接器jar文件存在于Tomcat的lib文件夹中。

2 个答案:

答案 0 :(得分:1)

在您的表单中,您已撰写<Form method=post action="DbServlet">

你没有用你的方法名称(post)写""(双引号),我猜因此没有为表单定义post方法,默认情况下它调用doGet()方法你的servlet而不是doPost()

所以写一下,

<form method="post" action="DbServlet">

我想提出的第二个建议是:

当您在代码中使用PreparedStatement时,无需为查询管理'单引号。

您可以在查询字符串中使用?问号,然后再使用setXXXsetInt()

setString方法
String query = "INSERT INTO PhoneNums  VALUES (?, ?, ?)";
ps = connection.prepareStatement(query);
ps.setString(1, surname);
ps.setString(2, forenames);
ps.setString(3, telNum);
ps.executeUpdate();


// I believe all these three are strings

答案 1 :(得分:0)

您可能需要提交更新:

connection.commit();

当servlet停止服务时(可能永远不会)调用destroy(和connection.close())。