gpt4 book ai didi

java - MySql Jsp错误

转载 作者:行者123 更新时间:2023-11-29 13:23:10 24 4
gpt4 key购买 nike

此页面是从用户输入信息并按注册的另一个页面重定向的。该代码应该将信息保存到数据库中并自动将它们重定向到index.jsp。由于某些奇怪的原因,信息确实保存到数据库中,但我仍然收到此错误。

这是错误和代码:

org.apache.jasper.JasperException: An exception occurred processing JSP page /savereg.jsp at line 19

16: Statement st=con.createStatement();
17: String sqlqueryCommand="insert into subscriber(name, street_address, city, state, zip, email, login, password) values('" + name + "','" + address + "','" + city + "','" + state + "','" + zip + "','" + email + "','" + user + "','" + pass + "')";
18:
19: st.executeUpdate(sqlqueryCommand);
20:
21: response.sendRedirect("index.jsp");
22: %>

最佳答案

尝试使用 try/catch block ,也许还有记录器。您的代码容易受到 sql 注入(inject)的攻击。使用后请确保关闭数据库连接。尝试下面的代码:

Connection dbConnection = null;
PreparedStatement preparedStatement = null;
String insertTableSQL = "insert into subscriber(name, street_address, city, state, zip, email, login, password) values(?, ?, ?, ?, ?, ?, ?, ?)";

try{

dbConnection = getDBConnection();
preparedStatement = dbConnection.prepareStatement(insertTableSQL);

preparedStatement.setString(1, name);
preparedStatement.setString(2, street_address);
preparedStatement.setString(3, city);
preparedStatement.setString(4, state);
preparedStatement.setString(5, zip);
preparedStatement.setString(6, email);
preparedStatement.setString(7, login);
preparedStatement.setString(8, password);
preparedStatement.executeUpdate();


} catch (Exception e){
e.printStackTrace();
} finally {
preparedStatement.close();
dbConnection.close();
}

关于java - MySql Jsp错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20509479/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com