动机:
这里不讨论JDBC之外的东西,这里仅假定选用JDBC。
如果用Statement进行数据库操作,要自已进行SQL防注入处理;
如果用PreparedStatement,虽然可以防注入,但是当在拼接条件时,如果条件变动或有字段变动,按默认的处理方式,则需要人工肉眼去注意占位符的前后顺序,容易出错。 因此有了以下工具。
思路:
仿 Delphi中的SQL字符串预处理,引入以”:r"开始的占位关键字段。例:
select * from man where no=:rno and name like :rname
用户写入如上类似的SQL语句,然后为“rno"、“rname”指定参数值,然后直接使用即可。
优点:
1、动态拼接时,参数的位置可以任意鸾化,SQL语句的参数可以动态增减
2、天然防SQL注入
3、对于动态的SQL拼接的要求限制很少,但又几乎没损SQL的功能。 唯一要求就是 SQL语句中的参数必须是 “:r"或“:R"开始 空格结束。
4、用户按普通字符串,按自已的目的与SQL语法要求,动态拼接即可。 凡时要用到参数的地方,以":r"开始 空格结束 命名参数即可。例:
update man set name = :rname where no=:rno
缺点:
1、增加了对SQL语句的处理,理论上性能会有所下降。
2、为了简单方便,在封装时在对PreparedStatement指定参数时,使用了
setObject方法。有可能会带来性能上的微小下降。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.Test;
public class PreparedStatementTool {
protected static final Logger loger=Logger.getLogger(PreparedStatementTool.class.getName());
public static class ParamMap{
//主存储体,Key为占位字段,value为其值
private HashMap<String,String> map;
//在产生PreparedStatement所需sqlL字符串时,Key为占位字段的生成顺序,值为占位字段
private ArrayList<String> indexs;
public void put(String key,String value){
if(map==null) map=new HashMap<String,String>();
map.put(key, value);
}
public void clear(){
if (map !=null) map.clear();
if(indexs!=null) indexs.clear();
}
public String get(String key){
if(map!=null) return map.get(key);
return null;
}
public int getSize(){
if(indexs!=null) return indexs.size();
return 0;
}
public String get(int i){
if(indexs==null) return null;
String key=indexs.get(i);
if (key==null || key.equals("")) return null;
if(map!=null ) return map.get(key);
return null;
}
protected void setIndex(String key){
if(map==null) return ;
if(indexs==null) indexs=new ArrayList<String>();
indexs.add(key);
}
}
public static ParamMap getParamMapInstance(){
return new ParamMap();
}
public static boolean isDelimiter(char c){
if(Character.isWhitespace(c) ){
return true;
}
return false;
}
public static String getSql(String presql,ParamMap paraemeters){
StringBuilder sb=new StringBuilder();
StringBuilder old=new StringBuilder(presql);
StringBuilder temp=new StringBuilder();
boolean r=false,colon=false;
for(int i=0;i<presql.length();i++){
char c=presql.charAt(i);
if( isDelimiter(c) && temp.length()>0){
r=false;colon=false;
paraemeters.setIndex(temp.toString());
sb.append('?').append(c);
temp.delete(0, temp.length());
} else if( isDelimiter(c) && temp.length()<=0){
r=false;colon=false;
sb.append(c);
} else if(Character.toUpperCase(c)==':'){
colon=true;//如果是空格后第一个是冒号
}else if(Character.toUpperCase(c)=='R' && colon){
colon=false;
r=true;//如果空格后是冒号且是R开头的(不会大小写)
temp.append(c);
} else if(r){
temp.append(c);
} else {
sb.append(c);
}
}
if(r){
paraemeters.setIndex(temp.toString());
sb.append('?');
}
return sb.toString();
}
public static PreparedStatement getPreparedStatement(Connection cnn,String preSql,
ParamMap paraemeters) throws SQLException
{
String str=getSql(preSql,paraemeters);
loger.log(Level.INFO, str);
PreparedStatement preState = cnn.prepareStatement(str);
StringBuilder sb=new StringBuilder("paraemeters is: ");
for(int i=0;i<paraemeters.getSize();i++){
preState.setObject(i+1,paraemeters.get(i));
sb.append(i+1).append(':').append(paraemeters.get(i));
sb.append("; ");
}
loger.log(Level.INFO,sb.toString() );
return preState;
}
@Test
public void test() throws ClassNotFoundException, SQLException{
String connType="com.mysql.jdbc.Driver";
String DBurl="jdbc:mysql://localhost:3306/floceay";
String user="root";
String pass="123456";
Class.forName(connType);
Connection con=DriverManager.getConnection(DBurl, user, pass);
String str="select * from man where no=:rno and name like :rname ";
ParamMap ps=getParamMapInstance();
ps.put("rname", "%dd%");
ps.put("rno", "1");
PreparedStatement preState = getPreparedStatement(con, str, ps);
ResultSet result= preState.executeQuery();
System.out.println("man表数据如下:");
while(result.next()){
System.out.println("no:"+result.getObject("no") +
";name:"+result.getString("name"));
}
preState.close();
con.close();
System.out.println();
}
}
完毕