2015-04-01 57 views
-1

我想在表中搜索包含子字符串的标题(在我的情况下,子字符串是传递给getBookForTitle方法的标题)。问题是它不会返回任何东西。使用占位符搜索postgresql

public void getBookForTitle(String title) { 
      PreparedStatement stm = null; 
      ResultSet rs = null; 
    try { 
       stm = connection.prepareStatement("Select * from books where name like '%?%'; "); 
       rs = stm.executeQuery(); 
       while (rs.next()) { 
        System.out.print(rs.getInt(1)); 
        System.out.print(": "); 
        System.out.print(rs.getString(1)); 
        System.out.println(rs.getBoolean(3)); 
       } 
      } catch (SQLException ex) { 
       ex.printStackTrace(); 
      } 

     } 

回答

1

您从不将值绑定到占位符。此外,占位符不应包含%符号和单引号。

它必须看起来像:

stm = connection.prepareStatement("Select * from books where name like ? "); 
stm.setString(1,"%"+your_string+"%")