【发布时间】:2017-09-20 04:11:13
【问题描述】:
我的代码有点问题,因为它显示了这个错误
'java.sql.SQLSyntaxErrorException:分配的值的数量与指定或隐含的列数不同。'
我读过类似的问题,但仍然对我的错误显示方式感到困惑。我也尝试将“,”像这样放在值之间,但仍然没有好处。什么是prepared statement?
//CONNECTION
Connection conn = null;
try{
conn =
DriverManager.getConnection("jdbc:derby://localhost:1527/MetroEventsDB",
"root", "root");
System.out.println("Connected");
//Insertion
Statement stmt = (Statement) conn.createStatement();
stmt.execute("INSERT INTO Users(UserID, Password, FirstName, LastName, Gender, Birthdate) VALUES('"+txtUserID.getText()+txtPassword.getText()+txtFirstName.getText()+txtLastName.getText()+gender+txtBirthdate.getText()+"')");
}catch(SQLException e){
System.err.println(e);
}
【问题讨论】:
-
请学习如何正确使用
PreparedStatement,例如通过阅读JDBC tutorial -
糟糕了,我看了这么久,我发现我错过了单个 qoutation ......所以它应该看起来像这样。 VALUES('"+txtUserID.getText()+" ', ' "+txtPassword.getText()
-
不,不应该。它应该是带有占位符的
PreparedStatement。您应该不将用户输入连接到 SQL 查询中