当前位置:网站首页>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
边栏推荐
- Global and Chinese market of broadband amplifiers 2022-2028: Research Report on technology, participants, trends, market size and share
- 0xc000007b the application cannot start the solution normally (the pro test is valid)
- Web Security (x) what is OAuth 2.0?
- Vmware workstation network card settings and three common network modes
- Thread process foundation of JUC
- Understand several related problems in JVM - JVM memory layout, class loading mechanism, garbage collection
- How to hide browser network IP address and modify IP internet access?
- [SRS] use of Vhost isolated stream: push / pull Stream Address
- Some common commands of podman
- Copy baby prompt: material cannot be empty. How to solve it?
猜你喜欢
导电滑环使用的注意事项
工业导电滑环的应用
Use and principle of Park unpark
0xc000007b应用程序无法正常启动解决方案(亲测有效)
Programmers dig "holes" to get rich: if they find a loophole, they will be rewarded 12.72 million yuan
Copy baby prompt: material cannot be empty. How to solve it?
Day 05 - file operation function
多表操作-外键级联操作
LevelDB源码分析之memtable
Summary of spanner's paper
随机推荐
Some common commands of podman
How to hide browser network IP address and modify IP internet access?
Global and Chinese market of 3D design and modeling software 2022-2028: Research Report on technology, participants, trends, market size and share
移动端常用解决方案
Thread process foundation of JUC
QT等待框制作
How to create a progress bar that changes color according to progress
Global and Chinese market of solder wire 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
Use and principle of wait notify
Flutter 实现每次进来界面都刷新数据
在Rainbond中一键部署高可用 EMQX 集群
Global and Chinese market of protection circuit modules 2022-2028: Research Report on technology, participants, trends, market size and share
Precautions for use of conductive slip ring
Leetcode522- longest special sequence ii- hash table - String - double pointer
导电滑环使用的注意事项
Leetcode316- remove duplicate letters - stack - greedy - string
导电滑环短路的原因以及应对措施
实战:redux的基本使用
QDataStream的簡單讀寫驗證