当前位置:网站首页>JDBC learning
JDBC learning
2022-06-29 04:17:00 【Learning for jisoo】
List of articles
Reference resources b Stand Mr. Han Shunping jdbc Course
One 、 Connect to database
Mode one :
Mode two 
Mode three 
Mode 4 
Methods five 
Two 、 Scribble
Summarize the interfaces and common methods 

Transaction processing
package com.aylxx.jdbc.lx;
import com.aylxx.jdbc.utils.JDBCUtils;
import org.junit.jupiter.api.Test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Founctiontest {
@Test
public void test01() {
Connection connection=null;
PreparedStatement preparedStatement=null;
String sql1="update account set money=money-100 where name=' Anyulong '";
String sql2="update account set money=money+100 where name=' Fu Fu Xu '";
try {
connection= JDBCUtils.getConnection();
connection.setAutoCommit(false);
preparedStatement = connection.prepareStatement(sql1);
preparedStatement.executeUpdate();
int i=1/0;
preparedStatement=connection.prepareStatement(sql2);
preparedStatement.executeUpdate();
connection.commit();
} catch (SQLException e) {
System.out.println(" An abnormal undo operation occurred ");
try {
connection.rollback();
} catch (SQLException ex) {
ex.printStackTrace();
}
e.printStackTrace();
}finally {
JDBCUtils.close(null,preparedStatement,connection);
}
}
}
jdbc tool kit
Prevent writing code that connects to the database again and again
convenient
package com.aylxx.jdbc.utils;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
public class JDBCUtils {
private static String user;
private static String pwd;
private static String url;
private static String driver;
static {
try {
Properties properties = new Properties();
properties.load(new FileInputStream("src\\jdbc.properties"));
user=properties.getProperty("user");
pwd=properties.getProperty("password");
url=properties.getProperty("url");
driver=properties.getProperty("driver");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static Connection getConnection(){
try {
return DriverManager.getConnection(url,user,pwd);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static void close(ResultSet resultSet, Statement statement,Connection connection){
try {
if(resultSet!=null)
{
resultSet.close();
}
if(statement!=null)
{
statement.close();
}
if(connection!=null){
connection.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
Batch application
Traditional processing time
@Test
public void test01(){
Connection connection=null;
PreparedStatement preparedStatement=null;
String sql="insert into ll values(?,?)";
long start=System.currentTimeMillis();
try {
connection = JDBCUtils.getConnection();
preparedStatement=connection.prepareStatement(sql);
for (int i = 0; i < 5000; i++) {
preparedStatement.setString(1,"ayl"+i);
preparedStatement.setString(2,"666");
preparedStatement.executeUpdate();
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCUtils.close(null,preparedStatement,connection);
}
long end=System.currentTimeMillis();
System.out.println(end-start);
}
Insert 5000 Data run time :8684ms
The batch : Yes preparedStatement Medium addbatch() Method , add to sql sentence ,executebatch() perform sql sentence ,clearbatch() Empty sql sentence
public void test01(){
Connection connection=null;
PreparedStatement preparedStatement=null;
String sql="insert into ll values(?,?)";
long start=System.currentTimeMillis();
try {
connection = JDBCUtils.getConnection();
preparedStatement=connection.prepareStatement(sql);
for (int i = 0; i < 5000; i++) {
preparedStatement.setString(1,"ayl"+i);
preparedStatement.setString(2,"666");
preparedStatement.addBatch();
if((i+1)%1000==0)
{
preparedStatement.executeBatch();
preparedStatement.clearBatch();
}
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCUtils.close(null,preparedStatement,connection);
}
long end=System.currentTimeMillis();
System.out.println(end-start);
}
The elapsed time :1170ms
It can be seen that the efficiency of batch processing will be greatly improved when a large number of insert statements are executed .
边栏推荐
- Analysis on the types of source code anti leakage technology
- Cloud native weekly | grafana 9 was officially released; The Chinese version of cloud native vocabulary is now online
- [WC2021] 斐波那契——数论、斐波那契数列
- [laravel series 8] out of the world of laravel
- moudo网络库剖析
- 在命令行登录mysql数据库以及查看版本号
- MySQL column to row conversion without Union
- [C language] explain the thread exit function pthread_ exit
- 1019 digital black hole
- 为什么说测试岗位是巨坑?8年测试人告诉你千万别上当
猜你喜欢

Log in to the MySQL database and view the version number on the command line

Seattention channel attention mechanism

SqlServer如何查询除去整列字段为null的结果

自己动手搭建一个简单的网站

How sqlserver queries and removes results with null fields in the whole column

从零到一,教你搭建「以文搜图」搜索服务(一)

【新功能】Ambire 钱包集成了 Metis 网络

Baidu AI Cloud service grid product CSM release 𞓜 hot public beta

Ask a simple question about SQL

HCIE-Security Day41:理论学习:信息收集与网络探测
随机推荐
C language -- branch structure
项目开发修养
Blue Bridge Cup ruler method
云主机mysql在本地电脑连接不上
Sword finger offer II 040 Largest rectangle in matrix
ECS 4 sync point, write group, version number
How to create robots Txt file?
Baidu AI Cloud service grid product CSM release 𞓜 hot public beta
Is the increased life insurance off the shelf? What additional life insurance products are available now?
Canoe- how to parse messages and display information in the trace window (use of program node and structure type system variables)
Hot renewal process
Logstash starts too slowly or even gets stuck
Why are you a test / development programmer? Can you recall
The second meeting of the Second Council of Euler open source community was held, and Xinhua III, hyperfusion and Godson Zhongke became members of the Council
剑指 Offer II 040. 矩阵中最大的矩形
Is the sink usually the JDBC insert update delete?
1019 digital black hole
Remote connection of raspberry pie in VNC Viewer Mode
How sqlserver queries and removes results with null fields in the whole column
快速开发项目-VScode插件