-1

我可以在一个.jsp 文件中有多个preparestatement 吗?我正在尝试执行此“jsp 文件”,但此代码对我不起作用。像这样的东西:

<%
    String login = request.getParameter("login");
    String pwd = request.getParameter("password");
    String name = request.getParameter("name");

    Connection connection = null;
    PreparedStatement pstatement = null;
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    int updateQuery = 0;
    if (login != null && password != null && full_name != null && ulevel != null && team_id != null) {
        if (login != "" && password != "" && full_name != "" && ulevel != "" && team_id != "") {
            try {
                connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/<mydatabase>", "<mylogin>", "<mypwd>");
                String queryString_1 = "INSERT INTO users (login,password) VALUES (?, ?)";
                pstatement = connection.prepareStatement(queryString_1);
                pstatement.setString(1, login);
                pstatement.setString(2, password);
                updateQuery = pstatement.executeUpdate();
                String queryString_2 = "INSERT INTO members (name) VALUES (?)";
                pstatement = connection.prepareStatement(queryString_2);
                pstatement.setString(1, name);
                updateQuery = pstatement.executeUpdate();
                if(updateQuery_1 != 0 && updateQuery_2 != 0) {
                   response.sendRedirect("index.jsp");
                }
            } catch (Exception ex) {
                out.println("Unable to connect to database.");
                } finally {
                pstatement.close();
                connection.close();
            }
        } 
    } 
%>
4

1 回答 1

0

您可以根据PreparedStatement需要拥有任意数量的 s。

要改进您的代码:
1. 您必须声明 int updateQuery_1 = 0 和 updateQuery_2 = 0。
2. 您必须在两个不同的 sql 语句上使用updateQuery_1 = pstatement.executeUpdate();和。updateQuery_2 = pstatement.executeUpdate();

于 2012-11-26T15:25:44.150 回答