当前位置:网站首页>Detail the execution process of JDBC query method
Detail the execution process of JDBC query method
2022-07-27 12:40:00 【Rippling rippling】
Code
package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
interface IRowMapper {
void row(ResultSet resultSet);
}
public class Select {
public static void select(String sql, IRowMapper rowMapper) {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
Class.forName("com.mysql.jdbc.Driver");// load
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1/test", "root", "root");
statement = connection.createStatement();
resultSet = statement.executeQuery(sql);
rowMapper.row(resultSet);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
public static void main(String[] args) {
class RowMapper implements IRowMapper {
@Override
public void row(ResultSet resultSet) {
try {
while (resultSet.next()) {
String name = resultSet.getString("name");
String address = resultSet.getString("address");
System.out.print(name);
System.out.print(address);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
select("select name,address from student", new RowMapper());
}
}
result
nullnull
Navicat Premium 12
Here we are MySQL Graphical client management tools are built on MySQL, Execute statement .
The code we need to use is :
use test;
create table student(
id char(50),
name char(40),
user_name varchar(50),
address varchar(85),
password varchar(150)
);
Execution process
Because the judgment statement should be inserted from the outside , therefore , We use interfaces and inner classes to realize this function .
1. Let's take another look main How to execute the code in the method .
perform select sentence , Pass in select Method is a statement for querying information , And an object .
2. Jump to the select Method .
(1) Load driver class . Use Class.forName(“com.mysql.jdbc.Driver”); Statement to realize this function . Among them com.mysql.jdbc.Driver Can find Driver() copied .
(2) Connect . Use connection = DriverManager.getConnection(“jdbc:mysql://127.0.0.1/test”, “root”, “root”); Realize this function ,jdbc, It means a kind of java API.MySQL Point out that we use MySQL Executive .127.0.0.1 It's local ,root Is the user name ,root It's a password . It doesn't have to be exactly the same as mine .
(3) Create statement . Use statement = connection.createStatement(); Statements for .createStatement Translation is to create sentences .
(4) Execute statement .executeQuery() Method realization . This method is used to execute select sentence , But you can only execute query statements , After execution, it returns the query result ResultSet object . Then we have to judge , So we called row Method . Pass in resultSet.
3. Enter the internal class implementation row Method .resultSet.next() The original content is a continuous strip with its own space .( Look at the picture below ), There is a pointer above the first content , At this time, the pointer points to nothing , However , When we use it , It will see if there is anything below it , If there is content, keep going down , If not, it will stop . We use this feature to judge whether the statement is completed . stay while Inside , We define name and address, Then the output . Due to runtime error , We use try-catch To throw . return select Method , Release resources . namely finally Contents of Li . Because if you release it first connection Words , because statement There is still , Therefore, it cannot be released successfully . So let's release statement, Re release connection.
边栏推荐
- JVM memory layout detailed, illustrated, well written!
- @Postconstruct annotations and initializingbean perform some initialization operations after bean instantiation
- Map接口
- V. introduction of other objectives and general options
- Play CSDN editor
- Implicit indicators for evaluating the advantages and disadvantages of automated testing
- 2022 global Vocational Education Industry Development Report
- 5V升压9V芯片
- About the problem that the onapplicationevent method of the custom listener is executed multiple times
- 2021-3-22-directed graph sorting
猜你喜欢

Mixin\ plug in \scoped style

V. introduction of other objectives and general options

HDU1698_ Just a Hook

Necessary foundation: Signature Verification

20210512 recursive formula

The song of the virtual idol was originally generated in this way!

2022 global Vocational Education Industry Development Report

(07) flask is OK if you have a hand -- flask Sqlalchemy

J9 number theory: how long is the mainstreaming of decentralized identity?

【数据库数据恢复】SQL Server数据库所在磁盘分区空间不足报错的数据恢复案例
随机推荐
20210520 TCP sliding window
如何获取Class类对象
Chapter 13 IO flow
Watermelon Book + pumpkin book chapter 1-2
2021-4-7-find the maximum water holding capacity
Interviewer: how can you close an order without using a scheduled task?
关于 CMS 垃圾回收器,你真的懂了吗?
Set接口
Error: slf4j: class path contains multiple slf4j bindings
堆
爱可可AI前沿推介(7.27)
【表达式计算】双栈 : 表达式计算问题的通用解法
I/o instance operation
POJ1988_ Cube Stacking
C program debugging and exception handling (try catch)
多表查询
SparkSubmit.main()方法提交外部参数,远程提交standalone集群任务
Detailed explanation of flask framework
Map接口
Photoshop web design tutorial