Java Prepared语句不执行 [英] Java Prepared statement not executing
问题描述
我创建了一个小的3层程序,包括:前端 - > servlet - >数据库。
前端我在表单中输入一些细节。它们被传递给一个servlet,它将呈现一些HTML并显示输入到表单中的值,同时还调用了DatabaseHelper类。 DatabaseHelper然后连接并将这些相同的值插入到表中。
我知道这些值被传递给servlet类ok,因为它们显示在HTML中。所以问题必须在准备的语句。问题是,我看不到任何错误的语句本身。当我查询表本身时,没有数据。
数据库连接是功能性的,因为我可以使用硬编码语句将值插入数据库,而不是准备语句。
下面是使用语句Im。非常感谢任何建议。
public void addRegisterDetails(String name,String email,String country,String password,){
try {
String driver =com.mysql.jdbc.Driver;
Class.forName(driver).newInstance();
//创建db连接
con = DriverManager.getConnection(url,USERNAME,PASSWORD);
st = con.createStatement();
String query =INSERT INTO user_information(name,email,country,password)+VALUES(?,?,?,?)
PreparedStatement preparedStmt = con.prepareStatement(query);
preparedStmt.setString(1,name);
preparedStmt.setString(2,电子邮件);
preparedStmt.setString(3,country);
preparedStmt.setString(4,password);
preparedStmt.execute();
} catch(ClassNotFoundException ex){
System.out.println(ex);
} catch(Exception e){
e.printStackTrace();
}
}
表定义
id |名称|电子邮件|国家| 解决方案
p>您应该在语句对象上调用方法 executeUpdate()
。
任何调用提交数据,任何事务处理。如果你跳过这段代码的目的这个问题的很好;否则这是相当重要的一步(如果一切顺利,提交,回退异常情况)
I have created a small 3 tier program, consisting of : front end -> servlet -> database.
Front end I enter some details into a form. They are passed to a servlet, which will render some HTML and display the values entered into the form, while also calling a class DatabaseHelper. The DatabaseHelper then connects and inserts these same values into a table.
I know the values are being passed to the servlet class ok, as they are being displayed in the HTML. So the problem must lie within the prepared statement. Problem is, I cannot see any fault with the statement itself. When I query the table itself, there is no data there.
Database connectivity is functional, as I can insert values into a database using hardcoded statements, just not a prepared statement.
Here is a look at the statement Im using. Any advice is much appreciated.
public void addRegisterDetails(String name, String email, String country, String password, ){
try{
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver).newInstance();
// Make db connection
con = DriverManager.getConnection(url, USERNAME, PASSWORD);
st = con.createStatement();
String query = " INSERT INTO user_information (name, email, country, password)" + " VALUES (?, ?, ?, ?)";
PreparedStatement preparedStmt = con.prepareStatement(query);
preparedStmt.setString (1, name);
preparedStmt.setString (2, email);
preparedStmt.setString (3, country);
preparedStmt.setString (4, password);
preparedStmt.execute();
}catch(ClassNotFoundException ex) {
System.out.println(ex);
}catch(Exception e){
e.printStackTrace();
}
}
Table definition
id| name | email | country | password
all VARCHAR except the id, which is type INT.
You should invoke the method executeUpdate()
on the statement object.
Also, I don't see any call to commit the data, any transaction handling. It's fine if you skipped that piece of code for the purpose of this question; otherwise it's quite an important step ( commit if all goes well, rollback for exception scenarios)
这篇关于Java Prepared语句不执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!