【问题标题】:java.sql.SQLSyntaxErrorException?java.sql.SQLSyntaxErrorException?
【发布时间】: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 查询中

标签: sql derby


【解决方案1】:

您在 stmt.execute 中有语法错误。正确的语法是,

 stmt.execute("INSERT INTO Users(UserID, Password, FirstName, LastName, Gender, Birthdate) VALUES('"+txtUserID.getText()+"','"+txtPassword.getText()+"','"+txtFirstName.getText()+"','"+txtLastName.getText()+"','"+gender+"','"+txtBirthdate.getText()+"')");

【讨论】:

  • 是的!!....啊哈哈我错过了那个单引号啊哈...谢谢你的回复
猜你喜欢
  • 1970-01-01
  • 2021-11-30
  • 2017-12-09
  • 2016-01-02
  • 2015-02-28
  • 1970-01-01
  • 1970-01-01
  • 2019-09-28
  • 2015-12-19
相关资源
最近更新 更多