前天有人问我说,我知道用Statement可能会产生注入问题,但是啥是注入问题?其实我知道他了解过,但是现在忘记了…..
谁说不是呢,我也是有些遗忘了,我就知道如果在sql语句后直接拼接条件,是可能产生注入问题 ,当然了如果你设置的条件是你想要,肯定不会出现注入问题的
话不多说,既然说到了注入问题,就去看看这个传说中的“注入问题”….
先让问题暴露出来,看下面的代码:
现在是这样,假设现在我的库里的数据是这样的:
1 黄药师 1900-1-1 400
2 黄晓明 1920-1-1 800
public class StatementTest{
public static void create(){
Connection con = null;
Statement st = null;
try{
con =
st = con.createStatement();
String sql = "insert into users(name, birthday, money) values('周伯通', '1900-1-1', 300)";
st.execute(sql);
System.out.println("存入成功!");
}catch(Exception e){
e.printStackTrace();
}finally{
}
public static void read(String uid){
Connection con = null;
Statement st = null;
ResultSet rs = null;
try{
con =
st = con.createStatement();
String sql = "select * from users where uid =" + uid;
st.execute(sql);
rs = st.getResultSet();
while(rs.next()){
System.out.print(rs.getObject(1) + "\t");
System.out.print(rs.getObject(2) + "\t");
System.out.print(rs.getObject(3) + "\t");
System.out.print(rs.getObject(4) + "\t");
}
}catch(Exception e){
e.printStackTrace();
}finally{
}
}
}
public static void main(String[] args){
create();
read("1 or 1 < 2");
}
}
这个时候,查询到的结果是全部:
1 黄药师 1900-1-1 400
2 黄晓明 1920-1-1 800
3 周伯通 1900-1-1 300
可以看出,传入的参数中,包含了sql代码! 这个代码会让条件失效!
解决方法:以后再也不要使用Statement了。取而代之使用java.sql.PreparedStatement!
public static void read(String uid){
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
con =
String sql = "select * from users where uid = ?";
ps = con.prepareStatement(sql);
ps.setString(1,Integer.parseInt("uid"));
ps.executeQuery();
rs = ps.getResultSet();
while(rs.next()){
System.out.print(rs.getObject(1) + "\t");
System.out.print(rs.getObject(2) + "\t");
System.out.print(rs.getObject(3) + "\t");
System.out.print(rs.getObject(4) + "\t");
}
}catch(Exception e){
e.printStackTrace();
}finally{
}
}
再在main方法里调用 read(“1 or 1 < 2”)时,就会报错了,通过PrepareStatement预处理语句,将不合理的条件过滤掉,解决了注入问题。
所以以后就把createStatement舍弃吧,也尽量不要在sql语句后面直接拼接条件。
<script type="text/javascript">
$(function () {
$('pre.prettyprint code').each(function () {
var lines = $(this).text().split('\n').length;
var $numbering = $('<ul/>').addClass('pre-numbering').hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i <= lines; i++) {
$numbering.append($('<li/>').text(i));
};
$numbering.fadeIn(1700);
});
});
</script>
版权声明:本文为博主原创文章,未经博主允许不得转载。
分享到:
相关推荐
prepareStatement和Statement的区别
4.创建`Statement` (此处存在SQL注入问题) 5.执行SQL 6.解析查询对象`ResultSet` 7.关闭连接 `ResultSet 、Statement 、Connection` 3.SQL注入问题与解决(Statement 、PreparedStatement) 3.1.模拟SQL注入 3.2....
hsbc statement template
Statement Tracer for Oracle绿色
project scope statement template
statement-tracer-for-oracle 跟踪器
simple templet of income statement in english
jdbc中statement和prepared区别,jdbc中statement和prepared区别
Generate code statement for hardcode
Statement和PreparedStatement之间的区别
解决:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after statement closed.
SQL Statement,一些sql语句的基本语句,方便学习数据库
java数据库连接PrepareStatement
爱思唯尔旗下的Credit Author Statement模板 论文已正式出版~ 相关博客:https://blog.csdn.net/qq_39763246/article/details/126797385
Statement Tracer for ADO 绿色版,捕捉ADO SQL代码.
NULL 博文链接:https://lao1984wang.iteye.com/blog/977055
Do you know how to prepare a cashflow statement?
java中PreparedStatement和Statement的区别