当前位置:网站首页>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();
}
}
边栏推荐
- Delay queue `delayqueue`
- winow10安装Nexus nexus-3.20.1-01
- pytorch Default process group is not initialized
- Bean拷贝详解
- The interviewer of a large front-line factory asked: do you really understand e-commerce order development?
- win10远程连接云服务器
- 一线大厂面试官问:你真的懂电商订单开发吗?
- Tar: /usr/local: cannot find tar in the Archive: due to the previous error, it will exit in the last error state
- Classical cryptosystem -- substitution and replacement
- 2022 CISP-PTE(一)文件包含
猜你喜欢
[openairinterface5g] rrcsetupcomplete for RRC NR resolution
OpenCV怎么下载?OpenCV下载后怎么配置?
SQL 注入绕过(一)
mssql如何使用语句导出并删除多表数据
Quick realization of Bluetooth ibeacn function
Interviewer: please introduce cache penetration, cache null value, cache avalanche and cache breakdown, which are easy to understand
POI replacing text and pictures in docx
面试官:大量请求 Redis 不存在的数据,从而打倒数据库,你有什么方案?
Currying Scala functions
如何优雅的写 Controller 层代码?
随机推荐
Caldera installation and simple use
Process termination (have you really learned recursion? Test your recursion Foundation)
延时队列`DelayQueue`
Idea method template
Gérer 1000 serveurs par personne? Cet outil d'automatisation o & M doit être maîtrisé
2022 le fichier CISP - Pte (i) contient:
面试官:大量请求 Redis 不存在的数据,从而打倒数据库,你有什么方案?
2022 cisp-pte (II) SQL injection
Modeling competition - optical transport network modeling and value evaluation
Rust Async: smol源码分析-Executor篇
mssql如何使用语句导出并删除多表数据
Tar: /usr/local: cannot find tar in the Archive: due to the previous error, it will exit in the last error state
请问如何在网页通过excel文件的形式向后段数据库添加数据
Basic SQL operations in tidb
R 语言Analyzing wine data
Interviewer: please introduce cache penetration, cache null value, cache avalanche and cache breakdown, which are easy to understand
uview的安装和功能
Unrecognized VM option ‘‘
Classical cryptosystem -- substitution and replacement
Fractional Order PID control