PreparedStatement对象替代Statement解决sql注入
来源:互联网 发布:手机mv制作软件 编辑:程序博客网 时间:2024/05/22 09:23
PreparedStatement对象替代Statement解决sql注入
使用非法SQL语句(如' or 1==1 or '),
利用Statement对象的缺点,从而非法操作数据库表的行为,叫SQL注入。
public static void main(String[] args) throws Exception{
try{
DriverManager.registerDriver(new Driver());
//URL,数据库用户名,密码
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc","root","root");
String sql = "select id,name,sal from users where name = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
//为?占位符设置值
//?只能代替实际值,不能代替表名,列名
//?从左向右,第一个?号为1,第二个?号为2,依次类推
pstmt.setString(1,name);
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
int id = rs.getInt("id");
name = rs.getString("name");
int sal = rs.getInt("sal");
System.out.println(id);
System.out.println(name);
System.out.println(sal);
System.out.println("------------------------");
}
}catch(Exception e){
e.printStackTrace();
throw e;
}finally{
JdbcUtil.close(conn);
JdbcUtil.close(pstmt);
JdbcUtil.close(rs);
}
}
- PreparedStatement对象替代Statement解决sql注入
- JDBC之SQL注入,PreparedStatement和Statement
- 防范 SQL 注入,需要采用PreparedStatement取代Statement。
- 防范 SQL 注入,需要采用PreparedStatement取代Statement
- 初学JDBC__第四节(SQL注入,PreparedStatement和Statement )
- Statement和PreparedStatement之间的区别以及sql注入
- JDBC要点总结、SQL注入示例(Statement和PreparedStatement)
- PreparedStatement与Statement对比、sql语句的被注入问题
- Statement、PreparedStatement执行SQL
- Statement和PreparedStatement的区别; 什么是SQL注入,怎么防止SQL注入?
- PreparedStatement对象和Statement对象
- 深入理解Statement、PreparedStatement对象
- SQL注入(PreparedStatement)
- PreparedStatement 防止sql注入
- JDBC(PreparedStatement,sql注入)
- sql注入小解,以及statement和preparedStatement的理解和区别
- PreparedStatement对象和Statement对象(一)
- PreparedStatement对象和Statement对象(二)
- C++学习:map(键值对)和set(集合)
- 4A平台的管理功能
- Hive1.2.1<常用操作示例>
- ASP.NET MVC 5 实现基于Quartz.net 的任务调度管理平台(四)(完)
- 数据库日期转换
- PreparedStatement对象替代Statement解决sql注入
- tiny4412学习(一)之从零搭建linux系统(烧写uboot、内核进emmc+uboot启动内核)
- Qt(C++) 截屏的方法
- Android 为什么我的Galaxy S2上空间不足?转载
- 日期类 class_Date
- NodeJS学习笔记-0
- Vmware12安装Redhat6.8
- 数组中重复的字
- 解决wine中文字体方块或乱码