当前位置: 移动技术网 > IT编程>开发语言>Java > PreparementStatement接口

PreparementStatement接口

2019年05月11日  | 移动技术网IT编程  | 我要评论

1.sql注入问题
在以前过程中,总是采取拼接sql语句的方式,来实现数据的增删改查!

string sql=select * from user where username="" and password=""

由于没有对拼接的字符进行检查,很容易遭受到恶意的攻击,例如变成如下操作。

select * from user where username='老李' or '1'='1' and password=";
由此及产生了sql注入的问题。

 

2.preparement

preparedstatement 解决sql注入原理,运行在sql中参数以?占位符的方式表示

select * from user where username = ? and password = ? ;

将带有?的sql 发送给数据库完成编译(不能执行的sql 带有?的sql 进行编译 叫做预编译),在sql编译后发现缺少两个参数

preparedstatement 可以将? 代替参数发送给数据库服务器,因为sql已经编译过,参数中特殊字符不会当做特殊字符编译,无法达到sql注入的目的主要是采取预编译

3.demo演示

 1  public user finduserbyusernameandpassword(string username, string password) {  
 2   
 3         string sql = "select * from user where username=? and password=?";  
 4   
 5         connection con = null;  
 6         preparedstatement pst = null;  
 7         resultset rs = null;  
 8         try {  
 9             // 1.得到连接对象  
10             con = jdbcutils.getconnection();  
11   
12             // 2.获取操作sql语句对象  
13             pst = con.preparestatement(sql); // 将sql语句进行预加载.  
14   
15             // 需要对占位符进行传参数  
16             pst.setstring(1, username);  
17             pst.setstring(2, password);  
18   
19             // 3.操作sql语句  
20             rs = pst.executequery();// 注意无参数  
21   
22             // 4.操作结果集  
23             if (rs.next()) {  
24                 user user = new user();  
25                 user.setid(rs.getint("id"));  
26                 user.setusername(rs.getstring("username"));  
27                 user.setpassword(rs.getstring("password"));  
28                 return user;  
29             }  
30         } catch (classnotfoundexception e) {  
31             e.printstacktrace();  
32         } catch (sqlexception e) {  
33             e.printstacktrace();  
34         }  
35   
36         return null;  
37     }

4.批处理操作

另外preparementstatement还支持批量sql语句的操作,有兴趣的可以查一下相关的api,主要方法如下

addbatch(); 添加sql到批处理

executebatch();执行批处理

 

 

5.demo演示

 1 public class preparedstatementbatchtest {  
 2   
 3     public static void main(string[] args) throws classnotfoundexception,  
 4             sqlexception {  
 5   
 6         string sql = "insert into user values(?,?)";  
 7         // 1.得到connection  
 8         connection con = jdbcutils.getconnection();  
 9   
10         // 2.得到preparedstatement 对象  
11         preparedstatement pst = con.preparestatement(sql);  
12   
13         // 3.执行批处理  
14         long l1=system.currenttimemillis();  
15         for (int i = 1; i <= 10000; i++) {  
16   
17             pst.setint(1, i);  
18             pst.setstring(2, "name" + i);  
19   
20             pst.addbatch(); //添加批处理  
21               
22             if(i%1000==0){  
23                 pst.executebatch();  
24                 pst.clearbatch(); //清空批处理语句.  
25             }  
26         }  
27           
28         system.out.println(system.currenttimemillis()-l1);  
29           
30         pst.executebatch();  
31           
32         //4.关闭资源  
33         pst.close();  
34         con.close();  
35     }  

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网