标签:add rgs root esc ESS where mys jdbc col
PreparedStatement预编译的SQL可以有效的防止SQL注入,但是有些写法需要值得注意。
Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/ssm","root","root"); StringBuffer sql =new StringBuffer("select ID,COMMAND,DESCRIPTION,CONTENT from message where 1=1") ; ArrayList<String> args = new ArrayList<String>(); if(command!=null&&!"".equals(command)){ sql.append(" and COMMAND=?"); args.add(command); } if(description!=null&&!"".equals(description)){ sql.append(" and DESCRIPTION like concat(‘%‘,?,‘%‘)"); args.add(description); } PreparedStatement ps = conn.prepareStatement(sql.toString()); for(int i=0;i<args.size();i++){ ps.setString(i+1, args.get(i)); } ResultSet rs = ps.executeQuery();
最关键的部分在
sql.append(" and DESCRIPTION like concat(‘%‘,?,‘%‘)");
使用concat可以有效地拼接字符串
标签:add rgs root esc ESS where mys jdbc col
原文地址:https://www.cnblogs.com/famousLion/p/9612987.html