当前位置:网站首页>JDBC常见面试题
JDBC常见面试题
2022-07-01 05:23:00 【lanleihhh】
数据库连接池
允许程序重复使用连接池中的 数据库连接,而不是销毁原来的连接,再创建一个新的连接

工作原理
服务器启动时,创建一定数量的数据库连接Connection,统一管理起来(抽象出一个连接池的概念)。
客户端需要连接时,从池中返回一个空闲连接,将其标记为忙
没有空闲连接的话,就新建一定数量的连接(由配置参数)
连接不再使用后,将其标记为空闲,其他线程可以使用该连接
客户端请求的连接数超过了最大连接数量时,该请求会被加入到等待队列中
实现方案
另一篇博客:数据库连接池的简单实现
JDBC的驱动过程
- 注册驱动
Class.forName("com.mysql.jdbc.Driver")
反射创建Driver对象,加载Driver类时,执行静态方法DriverManager.registerDriver(new Driver());注册驱动 - 建立连接
通过Connection建⽴连接Connection coon = DriverManager.getConnection(URL,USER,PWD); - 创建运行SQL的对象Statement
Statement statement = coon.prepareStatement(sql); - 执行SQL语句
statement.executeQuery();查询;返回ResultSetstatement.executeUpdate();增删改;返回影响的行数 - 处理结果集
ResultSet 保存执行的结果
遍历查询结果方式为resultSet.next() - 释放资源
释放数据库连接对象Connection
释放执行SQL的对象Statement
释放结果集的对象ResultSet
Statement和PreparedStatement区别
PreparedStatement接口继承了Statement接口
都是通过Connection连接对象获取
可读性、可维护性
Statement 只能用字符串来拼接参数
stmt.executeUpdate("insert into tb_name (col1,col2,col2,col4) " +
"values ('"+var1+"','"+var2+"',"+var3+",'"+var4+"')");
PreparedStatement 使用占位符,动态设置参数
ps = con.prepareStatement("insert into tb_name (col1,col2,col2,col4) " +
"values (?,?,?,?)");
ps.setString(1, var1);
ps.setString(2, var2);
ps.setString(3, var3);
ps.setString(4, var4);
性能
- Statement执行SQL语句没有预编译,每次执行都需要重新编译
- PrepareStatement会预编译SQL语句,多次执行时,只需要运行,不用每次编译一遍
安全性
由于Statement是用字符串拼接参数,很容易被SQL注入
PrepareStatement 预编译处理,每个占位符只能插入一个值,会过滤其他语句(提升了安全性)
JDBC中大数据量如何分页
边栏推荐
- Global and Chinese market of 3D CAD 2022-2028: Research Report on technology, participants, trends, market size and share
- 提高企业产品交付效率系列(1)—— 企业应用一键安装和升级
- Application and principle of ThreadPoolExecutor thread pool
- LevelDB源码分析之memtable
- 如何选择导电滑环材料
- Understand several related problems in JVM - JVM memory layout, class loading mechanism, garbage collection
- Go learning notes (5) basic types and declarations (4)
- CockroachDB 分布式事务源码分析之 TxnCoordSender
- Precautions for use of conductive slip ring
- Tar command
猜你喜欢

How to create a progress bar that changes color according to progress

数字金额加逗号;js给数字加三位一逗号间隔的两种方法;js数据格式化

Numeric amount plus comma; JS two methods of adding three digits and a comma to numbers; JS data formatting

Txncoordsender of cockroachdb distributed transaction source code analysis

How to hide browser network IP address and modify IP internet access?

el-form表单新增表单项动态校验;el-form校验动态表单v-if不生效;

CockroachDB 分布式事务源码分析之 TxnCoordSender

Summary of spanner's paper

提高企业产品交付效率系列(1)—— 企业应用一键安装和升级

How to traverse massive data in redis
随机推荐
AcWing 885. Find the combination number I (recursive preprocessing)
Rust hello-word
Actual combat: basic use of Redux
Redis database deployment and common commands
Use and principle of AQS related implementation classes
How to meet the requirements of source code confidentiality and source code security management
Simple read / write verification of qdatastream
Receiving package install and uninstall events
複制寶貝提示材質不能為空,如何解决?
Programmers dig "holes" to get rich: if they find a loophole, they will be rewarded 12.72 million yuan
[SRS] use of Vhost isolated stream: push / pull Stream Address
1076 Forwards on Weibo
Set集合详细讲解
Practice of combining rook CEPH and rainbow, a cloud native storage solution
QT等待框制作
基于TI DRV8424驱动步进电机实现调速和行程控制
Causes of short circuit of conductive slip ring and Countermeasures
Use and principle of reentrantlock
[Yugong series] February 2022 Net architecture class 005 ABP vNext Net core web application getting started configuration
Global and Chinese market for kitchen range hoods 2022-2028: Research Report on technology, participants, trends, market size and share