温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

jdbc如何实现防止sql注入

发布时间:2020-09-16 10:37:07 来源:亿速云 阅读:169 作者:小新 栏目:编程语言

这篇文章主要介绍jdbc如何实现防止sql注入,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

JDBC-防止sql注入漏洞

使用预编译可有效防止sql的注入漏洞。

原因:在statement中不能够有效的防止sql的注入漏洞,在于用户传入参数的时候可能会传入一些特殊字符,比如单引号' ' ,或者是-- 这种会影响到我们的sql语句.

所以使用预编译中的占位符,也就是?,可以有效的处理这一问题.

public class Prepared {
@Test
public void papa(){
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
//注册驱动
// Class.forName("com.mysql.jdbc.Driver");
//建立连接
// conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","dumy");
conn =JDBCUtils.getConnection();
//编写sql代码
//String sql = "select * from administer where id = ?";
String sql = "select * from administer where username = ? and password =?";
//预编译
pstmt = conn.prepareStatement(sql);
//给? 赋值
pstmt.setString(1, "ddd");
pstmt.setString(2, "123");
rs= pstmt.executeQuery();
while(rs.next()){
System.out.println("登录成功");
}
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCUtils.release(pstmt, conn, rs);
}
}
}

以上是jdbc如何实现防止sql注入的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI