当前位置:网站首页>JDBC common interview questions
JDBC common interview questions
2022-07-01 05:29:00 【lanleihhh】
JDBC Frequently asked questions
Database connection pool
Allow the program to reuse... In the connection pool Database connection , Instead of destroying the original connection , Create a new connection

working principle
When the server starts , Create a certain number of database connections Connection, Unified management ( Abstract out a concept of connection pool ).
When the client needs to connect , Return an idle connection from the pool , Mark it as busy
If there is no idle connection , Just create a certain number of connections ( By configuration parameters )
After the connection is no longer used , Mark it as free , Other threads can use this connection
When the number of connections requested by the client exceeds the maximum number of connections , The request will be added to the waiting queue
Implementation scheme
Another blog : Simple implementation of database connection pool
JDBC Driving process of
- Registration drive
Class.forName("com.mysql.jdbc.Driver")
Reflection to create Driver object , load Driver Class time , Execute static methodsDriverManager.registerDriver(new Driver());Registration drive - Establishing a connection
adopt Connection build ⽴ ConnectConnection coon = DriverManager.getConnection(URL,USER,PWD); - Create run SQL The object of Statement
Statement statement = coon.prepareStatement(sql); - perform SQL sentence
statement.executeQuery();Inquire about ; return ResultSetstatement.executeUpdate();Additions and deletions ; Returns the number of rows affected - Processing result set
ResultSet Save the results of execution
The method of traversing query results isresultSet.next() - Release resources
Release database connection objectConnection
Release execution SQL The object ofStatement
Release the objects of the result setResultSet
Statement and PreparedStatement difference
PreparedStatement Interface inherited Statement Interface
It's all through Connection Connect object to get
Readability 、 Maintainability
Statement Only strings can be used to splice parameters
stmt.executeUpdate("insert into tb_name (col1,col2,col2,col4) " +
"values ('"+var1+"','"+var2+"',"+var3+",'"+var4+"')");
PreparedStatement Use placeholders , Dynamic setting parameters
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);
performance
- Statement perform SQL Statement is not precompiled , Every execution needs to be recompiled
- PrepareStatement Will the precompiled SQL sentence , When executed many times , Just run , Don't compile it every time
Security
because Statement Concatenate parameters with strings , It's easy to get caught. SQL Inject
PrepareStatement Precompiling , Only one value can be inserted per placeholder , Will filter other statements ( Improved security )
JDBC How to page medium and large amounts of data
边栏推荐
- Programmers dig "holes" to get rich: if they find a loophole, they will be rewarded 12.72 million yuan
- Go learning notes (5) basic types and declarations (4)
- [data recovery in North Asia] a data recovery case of raid crash caused by hard disk drop during data synchronization of hot spare disk of RAID5 disk array
- Leetcode top 100 questions 1 Sum of two numbers
- QT等待框制作
- Global and Chinese market of high-end home theater 2022-2028: Research Report on technology, participants, trends, market size and share
- Global and Chinese market of 3D CAD 2022-2028: Research Report on technology, participants, trends, market size and share
- 基于TI DRV8424驱动步进电机实现调速和行程控制
- RecycleView的一些使用
- CockroachDB: The Resilient Geo-Distributed SQL Database 论文阅读笔记
猜你喜欢

Unity drags and modifies scene camera parameters under the editor

Data consistency between redis and database

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

工业导电滑环的应用

Daily question -leetcode1175- permutation of prime numbers - Mathematics

C# wpf 使用DockPanel实现截屏框

Rainbow combines neuvector to practice container safety management

Txncoordsender of cockroachdb distributed transaction source code analysis

Single page application

LevelDB源码分析之LRU Cache
随机推荐
Global and Chinese markets of InGaAs APD arrays 2022-2028: Research Report on technology, participants, trends, market size and share
Spanner 论文小结
Detailed explanation of set
Go learning notes (5) basic types and declarations (4)
Rainbond结合NeuVector实践容器安全管理
Set set detailed explanation
Youqitong [vip] v3.7.2022.0106 official January 22 Edition
Flutter can refresh data every time the interface comes in
busybox生成的东西
第05天-文件操作函数
3D建模与处理软件简介 刘利刚 中国科技大学
轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
Precautions for use of conductive slip ring
Ebpf cilium practice (2) - underlying network observability
Series of improving enterprise product delivery efficiency (1) -- one click installation and upgrade of enterprise applications
Global and Chinese markets of Ethernet communication modules 2022-2028: Research Report on technology, participants, trends, market size and share
Leetcode1497- check whether array pairs can be divided by K - array - hash table - count
Set集合詳細講解
Usage and principle of synchronized
Use and principle of Park unpark