Menu

trouble in PreparedStatement select in ACCESS DB with contains

Help
Hugo P.
2017-09-07
2017-09-07
  • Hugo P.

    Hugo P. - 2017-09-07

    Hello.

    I'm tring to make this work, but unable to do so.

    String query = "SELECT * FROM professores WHERE nome Like '?';";
    PreparedStatement ps = getConnection().prepareStatement(query);
    ps.setString(1, nomeProf);

    error I'm getting: UCAExc:::4.0.2 Invalid argument in JDBC call: parameter index out of range: 1

    please, help.

     
  • Gord Thompson

    Gord Thompson - 2017-09-08

    Parameter placeholders are simply bare question marks. They should never be enclosed in quotes, hash marks, or any other delimiter. '?' is not a parameter placeholder, it is a literal string containing a single question mark. To use a parameterized LIKE query you should do something similar to this:

    String query = "SELECT * FROM professores WHERE nome Like ?";
    PreparedStatement ps = getConnection().prepareStatement(query);
    ps.setString(1, nomeProf + "%");
    
     
    • Hugo P.

      Hugo P. - 2017-09-09

      Thanks man, that worked.
      I was trying with the quote because the sintaxe for the MS Access use the quote, did'nt realize it wold work with the '%'.
      Did'nt realize I should put the '%' inside the ps.setString()

      summarizing for people with similar problem:
      I was trying :

      String query = "SELECT * FROM professores WHERE nome Like %?%";
      PreparedStatement ps = getConnection().prepareStatement(query);
      ps.setString(1, nomeProf);
      

      It work like:

      String query = "SELECT * FROM professores WHERE nome Like ?";
      PreparedStatement ps = getConnection().prepareStatement(query);
      ps.setString(1,  "%"+nomeProf+"%");
      

      Sorry for the bad english.
      Thank you.

       

      Last edit: Hugo P. 2017-09-09

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.