当前位置:网站首页>JDBC事务提交事例
JDBC事务提交事例
2022-06-27 06:55:00 【青云ing】
import com.mysql.jdbc.Driver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class JDBCDemo {
public static void main(String[] args) throws Exception {
//1 获取连接
String url = "jdbc:mysql://IP:Port/db_name";
String username = "username ";
String password = "password ";
Connection conn = DriverManager.getConnection(url, username, password);
//2 sql 语句
String sql = "update account set age=18 where id=1";
//3 获取执行sql的对象
Statement stmt = conn.createStatement();
try {
//开启事务
conn.setAutoCommit(false);
//4 执行sql
int count = stmt.executeUpdate(sql);
//5 打印处理结果
System.out.println(count);
//提交事务
conn.commit();
} catch (Exception e) {
conn.rollback();
e.printStackTrace();
}
//6 释放资源
stmt.close();
conn.close();
}
}
边栏推荐
猜你喜欢

Configuring FTP, enterprise official website, database and other methods for ECS

Currying Scala functions

Solve the problem of win10 wsl2 IP change

(已解决) MINet 进行测试时报错如下 raise NotImplementedError

How to write controller layer code gracefully?

Memory barrier store buffer, invalid queue

Coggle 30 Days of ML 7月竞赛学习

在线文本数字识别列表求和工具

2018 mathematical modeling competition - special clothing design for high temperature operation

小米面试官:听你说精通注册中心,我们来聊 3 天 3 夜
随机推荐
Solve the problem of win10 wsl2 IP change
Get the query parameter in the address URL specify the parameter method
Instance tunnel use
Fast implementation of thread mesh networking
poi导出excle
通过uview让tabbar根据权限显示相应数量的tabbar
C# 请问怎么在更新数据库时候调用line与rows
From 5 seconds to 1 second, the system flies
Configuring FTP, enterprise official website, database and other methods for ECS
使用 Blackbox Exporter 测试网络连通性
How to implement redis cache of highly paid programmers & interview questions series 116? How do I find a hot key? What are the possible problems with caching?
Memory barrier store buffer, invalid queue
Transaction overview of tidb
Date database date strings are converted to and from each other
manim 数学引擎
window右键管理
Winow10 installation nexus nexus-3.20.1-01
面试官:大量请求 Redis 不存在的数据,从而打倒数据库,你有什么方案?
请问如何将数据从oracle导入fastDFS?
How torch.gather works