日期:2014-05-20  浏览次数:20851 次

PreparedStatement与模糊查询
String sql = "select * from TableName where columnName like ?";
pstmt.setString(1,"%aaa%");
我想要实现以上查询.但查询不出来.不知是何原因? 难道预编译不支持模糊查询? 不可能吧.... 疑惑中....

------解决方案--------------------
这位兄弟提的问题。本来这个SQL就可以查出来啊。我就查出来了
private static Connection con;
private static PreparedStatement pstm;
private static ResultSet rs ;
private static String url = "jdbc:mysql://127.0.0.1:3306/mydb";
private static String driver = "com.mysql.jdbc.Driver";
private static String user = "root";
private static String pwd = "root";
public static void main(String[] args) {
try {
String sql = "select id from user where name like ?";
Class.forName(driver);
con = DriverManager.getConnection(url,user,pwd);
pstm = con.prepareStatement(sql);
pstm.setString(1, "%job%");
rs = pstm.executeQuery();
rs.next();
String name = rs.getString("id");
System.out.println(name);
} catch (Exception e) {
e.printStackTrace();
}

打印的结果-------------32768