栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

MySQL——JDBC工具类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MySQL——JDBC工具类

特点

线程安全分离配置手动事务 API

获取PreparedStatement

getStatement(String sql);

提交事务,关闭连接

commitAndClose(Statement... stmt);

回滚事务,关闭连接

rollbackAndClose(Statement... stmt);
全部代码
public class JDBCUtil {
	private static final ThreadLocal local = new ThreadLocal<>();

	//获取Coon
	private static Connection getConn() {
		Connection conn1 = local.get();
		if (conn1 == null) {
			Connection conn2 = newConnection();
			local.set(conn2);
			start();
			return conn2;
		} else {
			return conn1;
		}
	}

	//新Coon
	private static Connection newConnection() {
		try {
			//获取配置文件信息
			Properties info = new Properties();
			InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("db.properties");
			info.load(is);
			String driver = info.getProperty("driver");
			String url = info.getProperty("url");
			String user = info.getProperty("user");
			String password = info.getProperty("password");
			//获取驱动,连接
			Class.forName(driver);
			return DriverManager.getConnection(url, user, password);
		} catch (Exception e) {
			throw new RuntimeException("获取连接失败");
		}
	}

	//获取PS
	public static PreparedStatement getStatement(String sql) throws SQLException {
		return getConn().prepareStatement(sql);
	}

	//关闭Coon,PS
	private static void close(Connection conn, Statement... stmt) {
		try {
			for (Statement statement : stmt) {
				if (statement != null) {
					statement.close();
				}
			}
			if (conn != null) {
				conn.close();
				local.remove();
			}
		} catch (Exception e) {
			throw new RuntimeException("关闭异常");
		}
	}

	//事务的开启提交回滚
	private static void start() {
		try {
			getConn().setAutoCommit(false);
		} catch (SQLException e) {
			throw new RuntimeException("事务异常");
		}
	}

	public static void commitAndClose(Statement... stmt) {
		try {
			Connection conn = getConn();
			conn.commit();
			close(conn, stmt);
		} catch (SQLException e) {
			throw new RuntimeException("事务异常");
		}
	}

	public static void rollbackAndClose(Statement... stmt) {
		try {
			Connection conn = getConn();
			conn.rollback();
			close(conn, stmt);
		} catch (SQLException e) {
			throw new RuntimeException("事务异常");
		}
	}
}
测试

pom


    mysql
    mysql-connector-java
    8.0.25

resources下db.properties

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/xxx
user=xxx
password=xxx

测试代码

@Test
public void fun1() {
	PreparedStatement ps1 = null;
	PreparedStatement ps2 = null;
	String sql1 = "update student set sage=sage+1 where sid=1";
	String sql2 = "update student set sage=sage+10 where sid=1";
	try {
		//自动开启连接,开启事务
		ps1 = JDBCUtil.getStatement(sql1);
		ps1.executeUpdate();
		//Integer error = 1 / 0;
		ps2 = JDBCUtil.getStatement(sql2);
		ps2.executeUpdate();
		//提交
		JDBCUtil.commitAndClose(ps1, ps2);
	} catch (SQLException e) {
		e.printStackTrace();
		//回滚
		JDBCUtil.rollbackAndClose(ps1, ps2);
	}
}

运行结果:
数据库中的值加了11

手动产生一个异常,运行结果:
数据库中没有发生变化

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/710874.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号