当前位置:网站首页>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共享服务和ssh远程控制服务
- js 哈希表 02
- Qt | 鼠标事件和滚轮事件 QMouseEvent、QWheelEvent
- 4. FTP service configuration and principle
- Druid 查询超时配置的探究 → DataSource 和 JdbcTemplate 的 queryTimeout 到底谁生效?
- Upgrade glibc 2.29 checking LD_ LIBRARY_ Path variable... Contains current directory error solution
- Flask框架——消息闪现
- Redis usage scenario
- UE4 collision
- 7.shell实用的小工具cut等
猜你喜欢

Kraken中事件通道原理分析

Microwave technology homework course design - Discrete capacitance and inductance + microstrip single stub + microstrip double stub

Acquisition and compilation of UE4 source code

Analysis of event channel principle in Kraken

ONNX(Open Neural Network Exchange)介绍

哥廷根大学提出CLIPSeg:一个使用文本和图像prompt能同时作三个分割任务的模型

Visual thematic map of American airport go style: ArcGIS Pro version

Pytorch tensor list is converted to tensor list of tensor to tensor using torch.stack()

Keras深度学习实战(16)——自编码器详解

HCIP(13)
随机推荐
Flask框架——flask-caching缓存
HCIA实验(06)
Introduction to onnx (open neural network exchange)
Mysql5.7 master-slave database deployment (offline deployment)
Voxceleb1 dataset Download
树形动态规划
The most comprehensive UE4 file operation in history, including opening, reading, writing, adding, deleting, modifying and checking
淦,为什么 '𠮷𠮷𠮷' .length !== 3 ??
Gan, why '𠮷 𠮷'.Length== 3 ??
Cloud native ide: the first general codeless development platform of IVX for free
Storage, computing, distributed Virtualization (collection and sorting is suitable for Xiaobai)
C# 类库的生成,使用类库对象对DataGridView 进行数据绑定
Keras深度学习实战(16)——自编码器详解
HCIP (01)
HCIA实验(08)
5. This simple "echo" usage, can the child next door!
Differences between redis and mongodb
【域泛化】2022 IJCAI领域泛化教程报告
4. FTP service configuration and principle
Storage, computing, distributed storage (collection and sorting is suitable for Xiaobai)












