본문 바로가기
MySql

MySQL 예외를 발생시키는 select 문의 매개 변수화 된 where 절이있는 PreparedStatement

by 베이스 공부 2020. 12. 20.
반응형

구문에 문제가 있음을 보여주는 SQLException으로 예외가 발생하고 설명서에서 동일하게 확인하십시오.

내 자바 코드는 다음과 같습니다.

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)

 

해결 방법

 

코드에 오류가 하나 있습니다.

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

 

참조 페이지 https://stackoverflow.com/questions/19462563

 

 

반응형

댓글