java連結資料庫使用prepareStatement引數化模糊查詢的兩種方法
阿新 • • 發佈:2019-02-11
String expr = "select *from table where url like?"; pstmt =con.prepareStatement(expr); String a="a"; pstmt.setString(1,"%"+a+"%");//自動新增單引號 (包裝後的引數) pstmt.execute(); System.out.println(pstmt.toString());//列印sql //會預設生成sql: select *from table where url like '%http%' //第二種方法 String sql="select * from emp e where upper(e.ename) like upper('%'||?||'%')"; pStmt= DBUtil.pStmt(conn, sql); DBUtil.setParams(pStmt, name); rs = pStmt.executeQuery();