当前位置:网站首页>JDBC的APi补充
JDBC的APi补充
2022-07-25 10:06:00 【华为云】
四、ResultSet
ResultSet(结果集对象)作用:
1.封装了DQL查询语句的结果
ResultSet stmt.executeQuery(sql):执行DQL语句,返回ResultSet对象
获取查询结果
boolean next():(1)将光标从当前位置向前移动一行(2)判断当前行是否是有效行
返回值:当前行有数据返回true,当前没数据返回false。
xxx getXxx(参数):获取数据
解释:xxx表示数据类型;如int getInt(参数);String getString(参数);
参数:对于int是列的编号,从1开始,对于String是列的名称。
使用步骤:
1、游标向下移动一行,并判断该行是否有数据:next()
2、获取数据:getXxx(参数)
示例:
实例:
数据库中emp表
运行之后:
ResultSet案例
需求:查询account账户数据,封装为Account对象中,并且存储到ArrayList集合中
创建一个pojo包,用来存放对象的。
创建了一个类,提供getSet方法
jdbc包下创建的类中
运行结果:
五、PreparedStatement
PreparedStatement作用:
1、预编译SQL语句并执行:预防SQL注入问题
SQL注入
SQL注入是通过操作输入来修改事先定义好的SQL语句,用以达到执行代码对服务器进行攻击的方法。
演示普通登录:
首先数据录kc_db1下的emp表为:
运行结果:
输入其他(不成功的原因是数据库中没有账号密码为这个的):
sql注入演示:
对于这条sql语句来说不点在于密码,账号任意
运行结果:
这条SQL语句是:select *from emp where ename='随便写的名'and password=' ' or '1'='1'
sql注入的本质就是改变原有的SQL语句,加入or之后1=1恒为真,所以这条语句就是true
PreparedStatement解决SQL注入
①获取PreparedStatement对象
②设置参数
PreparedStatement对象:setXxx(参数1,参数2):表示给参数1(?的位置)赋值为参数2
Xxx:数据类型;任意setInt(参数1,参数2)
参数:
- 参数1:表示?的位置编号,从1开始
- 参数2: ?的值
③执行sql
executeUpdate();/excuteQuery();括号内不需要传递sql。
创建类:
运行结果:
这样就防止了sql注入,setXxx会对传入的参数会进行转义,不会拼接成字符串而是\' or\ ' 1\' = \' 1\'
输入正确的:
PrepareStatement原理
PrepareStatement好处:
1、预编译SQL,性能更高
2、防止sql注入。
my.ini配置文件可以看到日志
预编译功能默认关闭
①:PreparedStatement预编译功能开启:userServerPrepStmts=true
在sql语句?之后书写参数
开启就会要prepare预编译:
关闭之后就没有Prepare阶段
边栏推荐
猜你喜欢

5. NFS shared services and SSH Remote Control Services

VLAN configuration and application (take Huawei ENSP as an example)

js 哈希表 01

Kraken中事件通道原理分析

BGP联邦实验

AI technology stack is too huge! Wu Enda gives career planning: lifelong learning

Hucang integrated e-commerce project (II): project use technology, version and basic environment preparation
QT | mouse events and wheel events qmouseevent, qwheelevent

Using numpy for elevation statistics and visualization

Analysis of event channel principle in Kraken
随机推荐
使用Numpy进行高程统计及可视化
Hucang integrated e-commerce project (II): project use technology, version and basic environment preparation
7.shell实用的小工具cut等
Reproduce asvspoof 2021 baseline rawnet2
HCIP (01)
3.信你能理解的!shell脚本之循环语句与函数,数组,冒泡排序
Qt | 鼠标事件和滚轮事件 QMouseEvent、QWheelEvent
9.shell文本处理三剑客之awk
What is the meaning of ordinary people's life?
Electromagnetic field and electromagnetic wave experiment I familiar with the application of MATLAB software in the field of electromagnetic field
4.隔壁小孩都会的,各种shell符号{}[]等
JS hash table 01
AI系统前沿动态第43期:OneFlow v0.8.0正式发布;GPU发现人脑连接;AI博士生在线众筹研究主题
2.介绍部署LAMP平台+DISCUZ论坛
Flask框架——flask-caching缓存
disabled和readonly 以及焦点问题
5.这简单的 “echo” 用法隔壁小孩能不会吗!
5.NFS共享服务和ssh远程控制服务
The most comprehensive UE4 file operation in history, including opening, reading, writing, adding, deleting, modifying and checking
Differences between redis and mongodb












