-1

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必要な数のを持っているかもしれません。

コードを改善するには、次の
手順に従います。1. int updateQuery_1=0およびupdateQuery_2=0を宣言する必要があります。2。とを2つの異なるSQLステートメントで
使用する必要があります。updateQuery_1 = pstatement.executeUpdate();updateQuery_2 = pstatement.executeUpdate();

于 2012-11-26T15:25:44.150 に答える