当前位置:网站首页>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.
边栏推荐
- NFT mall /nft blind box / virtual blind box /nft transaction / customizable second opening
- HDU1698_ Just a Hook
- 【表达式计算】双栈 : 表达式计算问题的通用解法
- redis分布式在线安装
- JS date and time format (year, month, day, hour, minute, second, week, quarter, time difference acquisition, date and timestamp conversion function)
- Top of the tide - reading notes + excerpts + insights
- Chapter 10 enumeration classes and annotations
- 详述HashSet的add方法
- @Postconstruct annotations and initializingbean perform some initialization operations after bean instantiation
- Fundamentals of mathematics 02 - sequence limit
猜你喜欢

II. Analysis of the execution process of make menuconfig

BSP视频教程第21期:轻松一键实现串口DMA不定长收发,支持裸机和RTOS,含MDK和IAR两种玩法,比STM32CubeMX还方便(2022-07-24)

Openpyxl drawing radar map

Implicit indicators for evaluating the advantages and disadvantages of automated testing

Shutter project scrollcontroller attached to multiple scroll views, failed assertion: line 109 POS 12 error handling

Interviewer: how to deal with the data loss of redis master-slave cluster switching?

详述try-catch-finally

12 pictures, take you to thoroughly understand ZGC garbage collector!

Detail throw and throws

Chapter 10 enumeration classes and annotations
随机推荐
A personal blog integrating technology, art and sports.
NFT mall /nft blind box / virtual blind box /nft transaction / customizable second opening
详述throw与throws
SSM practical project - front back separation (easy to understand)
虚拟偶像的歌声原来是这样生成的!
SQL question brushing: find out the current salary of all employees
P1321 word overlay restore [getting started]
Fundamentals of mathematics 02 - sequence limit
@Postconstruct annotations and initializingbean perform some initialization operations after bean instantiation
P1876 turn on the light [getting started]
2021-3-23-meituan-regular sequence
Nodejs body parser middleware processes post form data of type multipart / form data, and req.body cannot receive data
Play CSDN editor
4. Analysis of the execution process of make modules for general purposes
概述静态内部类与非静态内部类
C program debugging and exception handling (try catch)
Julia beginner tutorial 2022
How to use the server to build our blog
Openpyxl drawing area map
12 pictures, take you to thoroughly understand ZGC garbage collector!