【问题标题】:insert query with varchar as data type for primary key插入以 varchar 作为主键数据类型的查询
【发布时间】:2013-04-26 13:33:54
【问题描述】:

程序不接受下面给出的查询-

 public class loginDaos {

       public void create(loginBean bean) {
           ConnectionPool c = ConnectionPool.getInstance();
           c.initialize();
           Connection con = c.getConnection();
           try {
               String sql = "INSERT INTO login VALUES(?,?,?,?)";
               PreparedStatement pstmt = con.prepareStatement(sql);
               pstmt.setString(1, bean.getCode());
               pstmt.setString(2, bean.getUserid());
               pstmt.setString(3, bean.getPassword());
               pstmt.setString(4, bean.getPosition());
               pstmt.executeUpdate(sql);
           }
       } catch (Exception e) {
           e.printStackTrace();
       }
   }


   public static void main(String args[]) {
       loginDaos ld = new loginDaos();
       loginBean lb = new loginBean("representative", "rep7", "rep7", "r");
       ld.create(lb);

   }
   }

表格是:

 CREATE TABLE login
( userid char(25);
  password varchar(45),
  code char(5),
  position char(),
  PRIMARY KEY (code)
 );

我目前将用户名、密码、代码和位置的值保留为字符串。堆栈跟踪是:

 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?,?,?)' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.Util.getInstance(Util.java:381)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2536)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1564)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1485)
    at login.loginDaos.create(loginDaos.java:33)
    at login.loginDaos.main(loginDaos.java:42)

构建成功(总时间:0 秒)

救命!

【问题讨论】:

    标签: java mysql jdbc primary-key varchar


    【解决方案1】:

    当您使用PreparedStatement 时,您应该使用不带String 参数的executeQuery()executeUpdate()execute() 方法。

    所以要解决问题,请使用:

    // ....
    pstmt.setString(3, bean.getPassword());
    pstmt.setString(4, bean.getPosition());
    pstmt.executeUpdate();
    

    MySQL 驱动程序实现有一个错误,因为JDBC specification 声明接受字符串的方法在PreparedStatementCallableStatement 实现上调用时应该抛出SQLException。在这种情况下,它只会抛出异常,因为它会尝试直接执行参数化查询。

    【讨论】:

    • 谢谢!现在将永远记住这个概念!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 2022-08-11
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    相关资源
    最近更新 更多