当前位置:网站首页>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阶段
边栏推荐
- 接口流量突增,如何做好性能调优?
- HCIP实验(01)
- C3d model pytorch source code sentence by sentence analysis (II)
- 信号完整性(SI)电源完整性(PI)学习笔记(三十四)100条估计信号完整性效应的经验法则
- Flask框架——flask-caching缓存
- 9. Shell text processing three swordsmen awk
- 1.Shell编程规范与变量
- Using px2rem does not take effect
- Configuration of OSPF protocol (take Huawei ENSP as an example)
- ONNX(Open Neural Network Exchange)介绍
猜你喜欢
![[strategic mode] like Zhugeliang's brocade bag](/img/c8/4baeabbc3674b956f6d376ab71e15a.png)
[strategic mode] like Zhugeliang's brocade bag

美国机场围棋风格可视化专题图:ArcGIS Pro版本

性能测试中TPS的计算【杭州多测师】【杭州多测师_王sir】

我,AI博士生,在线众筹研究主题

Voxceleb1 dataset Download

After switching the shell command line terminal (bash/zsh), CONDA cannot be used: command not found

MySQL offline deployment

使用px2rem不生效

异步Servlet在转转图片服务的实践

AI技术栈太庞大!吴恩达给出职业生涯规划:终生学习
随机推荐
异步Servlet在转转图片服务的实践
JS collection
Pytorch tensor list is converted to tensor list of tensor to tensor using torch.stack()
6.PXE结合Kickstart原理和配置实现无人值守自动装机
HCIA实验(06)
Qt | 鼠标事件和滚轮事件 QMouseEvent、QWheelEvent
2021 致景笔试总结
[strategic mode] like Zhugeliang's brocade bag
信号完整性(SI)电源完整性(PI)学习笔记(三十四)100条估计信号完整性效应的经验法则
4. FTP service configuration and principle
搭建LNMP+DISCUZ论坛
Flask框架——Session与Cookie
【蓝桥杯集训100题】scratch太极图 蓝桥杯scratch比赛专项预测编程题 集训模拟练习题第22题
4. Children next door will know all kinds of shell symbols {}[], etc
2. Conditional statements of shell script
Implementation of recommendation system collaborative filtering in spark
QT | mouse events and wheel events qmouseevent, qwheelevent
2021 qunar written examination summary
1.Shell编程规范与变量
HCIA实验(09)












