qq_34573155
2016-04-27 01:43
采纳率: 100%
浏览 3.4k

JDBC使用占位符的插入语句插入不成功是什么原因

……
public static void insert(String name,String password) throws Exception{

          Connection conn = null;
          PreparedStatement st = null;
          ResultSet rs =null;
          conn = DbUtils.getConn();
        String sql="insert into User(name,password) values(?,?)";
        st = conn.prepareStatement(sql);

        st.setString(1,name);
        st.setString(2, password);
        int num = st.executeUpdate(sql);
                    if(num>0){
                     System.out.println("插入成功!!");
             }


                   DbUtils.release(conn, st, rs);
    }

        public static void main(String args[]) throws Exception{
    …… 
        IUserDao.insert("hello","89790");
    …… 
    }

}

+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| userID | int(11) | YES | | NULL | |
| name | varchar(255) | YES | | NULL | |
| sex | varchar(255) | YES | | NULL | |
| birthday | date | YES | | NULL | |
| password | varchar(255) | YES | | NULL | |
+----------+--------------+------+-----+---------+-------+
一直报错,但是不知道是怎么错了。求教 orz
Exception in thread "main" 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:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:939)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2547)
at com.mysql.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1541)
at com.mysql.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2605)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1469)
at dao.IUserDao.insert(IUserDao.java:107)
at dao.IUserDao.main(IUserDao.java:120)

表名没错,id不是自增的,没有primarykey。把sql写死是可以插入的,用“?”就不行了,为什么呢?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • lambda-fk 2016-04-27 02:12
    已采纳

    看看错误信息:
    【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】
    sql的语法错误了

    st = conn.prepareStatement(sql);
    这个已经将sql语句设置了

        st.setString(1,name);
        st.setString(2, password);
    

    ** int num = st.executeUpdate(sql);**
    你这里就不要这个sql的参数了,不然认为你是按照sql中有值的方式不是占位符的方式来执行

    int num = st.executeUpdate(sql);
    改为
    int num = st.executeUpdate();

    8 打赏 评论
  • devmiao 2016-04-27 01:44

    user是关键字,需要转义

    打赏 评论
  • little_how 2016-04-27 02:08

    我就想知道你的preparedstatement属于哪个包的?
    是java.sql包下的吗?

    打赏 评论

相关推荐 更多相似问题