我可以在一个.jsp文件中使用多个预先表示吗?我试图执行这个“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();
}
}
}
%>
答案 0 :(得分:0)
您可以根据需要拥有尽可能多的PreparedStatement
。
改善您的代码:
1.您必须声明int updateQuery_1 = 0和updateQuery_2 = 0
2.您必须在两个不同的sql语句中使用updateQuery_1 = pstatement.executeUpdate();
和updateQuery_2 = pstatement.executeUpdate();
。