Java 选择语句中带有参数化 where 子句的 PreparedStatement 抛出异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19462563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:28:34  来源:igfitidea点击:

PreparedStatement with parameterised where clause in select statement throwing Exception

javamysqljdbcprepared-statement

提问by SME

I am getting exception as SQLException showing there is a something wrong in the syntax and check the manual for same.

我收到异常,因为 SQLException 显示语法有问题,并检查手册是否相同。

My java code is as follows.

我的java代码如下。

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.

如果我通过附加包含 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)

采纳答案by Paul Vargas

There 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

回答by Ravi Thapliyal

Your SQL should just be

你的 SQL 应该只是

SELECT * FROM patient WHERE patient._ID = ?

The table name patient.patientseems incorrect since the column is referred to as patient._ID

表名patient.patient似乎不正确,因为该列被称为patient._ID

回答by SpringLearner

change this String sql = "SELECT * FROM patient.patient WHERE patient._ID=?";

改变这个 String sql = "SELECT * FROM patient.patient WHERE patient._ID=?";

to String sql = "SELECT * FROM patient WHERE patient._ID=?";

String sql = "SELECT * FROM patient WHERE patient._ID=?";