我得到异常,因为SQLException显示语法有问题并检查手册是否相同。
我的java代码如下。
PreparedStatement pst; String sql ="SELECT * FROM patient.medicine where _id=?"; pst = cn.prepareStatement(sql); pst.setInt(1, 1); ResultSet rs = pst.executeQuery(sql);如果我通过附加保存id值的变量来执行此操作,一切正常。
*SELECT * FROM patient.medicine where _name=?* Oct 19, 2013 11:15:46 AM com.seed.entity.Patient getData SEVERE: null 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(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) at com.mysql.jdbc.Util.getInstance(Util.java:386) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2728) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2678) at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1612) at com.seed.entity.Patient.getData(Patient.java:257) at com.seed.entity.Patient.main(Patient.java:360)I am getting exception as SQLException showing there is a something wrong in the syntax and check the manual for same.
My java code is as follows.
PreparedStatement pst; String sql ="SELECT * FROM patient.medicine where _id=?"; pst = cn.prepareStatement(sql); pst.setInt(1, 1); ResultSet rs = pst.executeQuery(sql);If I execute this by appending the variable holding a value for id, everything works fine.
*SELECT * FROM patient.medicine where _name=?* Oct 19, 2013 11:15:46 AM com.seed.entity.Patient getData SEVERE: null 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(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) at com.mysql.jdbc.Util.getInstance(Util.java:386) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2728) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2678) at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1612) at com.seed.entity.Patient.getData(Patient.java:257) at com.seed.entity.Patient.main(Patient.java:360)最满意答案
您的代码中有一个错误:
PreparedStatement pst; String sql ="SELECT * FROM patient.patient P WHERE P._ID = ?"; pst = cn.prepareStatement(sql); pst.setInt(1, 1); ResultSet rs = pst.executeQuery(); // without argumentsThere is one error in you code:
PreparedStatement pst; String sql ="SELECT * FROM patient.patient P WHERE P._ID = ?"; pst = cn.prepareStatement(sql); pst.setInt(1, 1); ResultSet rs = pst.executeQuery(); // without argumentsPreparedStatement with parameterised where子句在select语句中抛出异常(PreparedStatement with parameterised where clause in select statement throwing Exception)我得到异常,因为SQLException显示语法有问题并检查手册是否相同。
我的java代码如下。
PreparedStatement pst; String sql ="SELECT * FROM patient.medicine where _id=?"; pst = cn.prepareStatement(sql); pst.setInt(1, 1); ResultSet rs = pst.executeQuery(sql);如果我通过附加保存id值的变量来执行此操作,一切正常。
*SELECT * FROM patient.medicine where _name=?* Oct 19, 2013 11:15:46 AM com.seed.entity.Patient getData SEVERE: null 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(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) at com.mysql.jdbc.Util.getInstance(Util.java:386) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2728) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2678) at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1612) at com.seed.entity.Patient.getData(Patient.java:257) at com.seed.entity.Patient.main(Patient.java:360)I am getting exception as SQLException showing there is a something wrong in the syntax and check the manual for same.
My java code is as follows.
PreparedStatement pst; String sql ="SELECT * FROM patient.medicine where _id=?"; pst = cn.prepareStatement(sql); pst.setInt(1, 1); ResultSet rs = pst.executeQuery(sql);If I execute this by appending the variable holding a value for id, everything works fine.
*SELECT * FROM patient.medicine where _name=?* Oct 19, 2013 11:15:46 AM com.seed.entity.Patient getData SEVERE: null 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(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) at com.mysql.jdbc.Util.getInstance(Util.java:386) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2728) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2678) at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1612) at com.seed.entity.Patient.getData(Patient.java:257) at com.seed.entity.Patient.main(Patient.java:360)最满意答案
您的代码中有一个错误:
PreparedStatement pst; String sql ="SELECT * FROM patient.patient P WHERE P._ID = ?"; pst = cn.prepareStatement(sql); pst.setInt(1, 1); ResultSet rs = pst.executeQuery(); // without argumentsThere is one error in you code:
PreparedStatement pst; String sql ="SELECT * FROM patient.patient P WHERE P._ID = ?"; pst = cn.prepareStatement(sql); pst.setInt(1, 1); ResultSet rs = pst.executeQuery(); // without arguments
发布评论