복붙노트

[SQL] 근처의 사용에 바로 구문 '?'

SQL

근처의 사용에 바로 구문 '?'

나는 자바 코드를 :

String searchPerson = "select * from persons where surname like ? and name like ?";
//connect to DB
PreparedStatement statement = connect.prepareStatement(searchPerson);
statement.setString(1,"%"+ surname + "%");
statement.setString(2, "%" + name + "%");
ResultSet resultPerson = statement.executeQuery(searchPerson);
//..code

그럼되는 SQLException이있다 :

해결법

  1. ==============================

    1.다음과 같이 매개 변수없이의 prepareStatement를 실행한다 :

    다음과 같이 매개 변수없이의 prepareStatement를 실행한다 :

    statement.executeQuery()
    

    그대로 (바인딩 된 매개 변수없이) 제공된 쿼리를 실행하는 String 매개 변수와 함께하는 executeQuery를 호출.

  2. ==============================

    2.

    ResultSet resultPerson = statement.executeQuery(searchPerson);
    

    해야한다

    ResultSet resultPerson = statement.executeQuery();
    
  3. ==============================

    3.statement.setString으로 시도 (1 ''% '+ 성 +'% ' ");

    statement.setString으로 시도 (1 ''% '+ 성 +'% ' ");

  4. from https://stackoverflow.com/questions/24917917/right-syntax-to-use-near by cc-by-sa and MIT license