占位符报语法错误
新增和更新方法类里面的占位符能够跑起来,但是删除和查询单个女神里面的占位符就会报语法错误,将占位符手动改为具体整型数字就可以正确运行。
以下是部分源码。
//删除功能代码块 public void delPeople (Integer id) throws SQLException { //获取数据库连接 Connection conn = DBUtil.getConnection(); //编写sql语法规范的字符串为数据库修改数据 String sql = "" + " DELETE FROM test" + " WHERE id=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1, id); //通过PreparedStatement对象执行SQL语句 pstmt.execute(); } //查询功能代码块 public People get (Integer id) throws Exception { //获取数据库连接 Connection conn = DBUtil.getConnection(); //编写sql语法规范的字符串为数据库修改数据 String sql = "" + " SELECT * FROM test" + " WHERE id=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1, id); //通过PreparedStatement对象的executeQuery方法查询数据库存储的信息 ResultSet rs = pstmt.executeQuery(sql); People p = null; while(rs.next()) { p = new People(); p.setAge(rs.getInt("age")); p.setUsername(rs.getString("username")); p.setId(rs.getInt("id")); p.setSex(rs.getInt("sex")); } return p; } //错误信息 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:425) at com.mysql.jdbc.Util.getInstance(Util.java:408) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:943) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2497) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2455) at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1369) at com.javajdbc.dao.PeopleDao.get(PeopleDao.java:103) at com.javajdbc.peopleaction.PeopleAction.main(PeopleAction.java:35)