2015-04-01 54 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+"%")