项目主题三:数据库回滚,提交

本文介绍了一个Java类Sqlconnect用于数据库连接、初始化、事务处理及查询、更新操作的基本用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;

public class Sqlconnect {
	public Connection dbConn = null;

	public Statement stmt;
	public PreparedStatement st = null;
	boolean autoCommit;
	public ResultSet rs = null;
	public String sql = null;

	public String driverName = "";
	public String dbURL = "";
	public String userName = "";
	public String userPwd = "";

	
//连接数据库
	public SQLconnect() {
		init();
	}

	//初始化数据库
	public void init() {
		try {
			Map<String,String> map= InitialConf.getXmlMap();
			driverName = map.get("DBdriverName");
			dbURL = map.get("DBurl");
			userName = map.get("DBuserName");
			userPwd = map.get("DBuserPwd");
			Class.forName(driverName);
			dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static SQLconnect getNewInstance() {
		return new SQLconnect();
	}

	
//开启事务
	public void beginTrans() throws SQLException {
		try {
			autoCommit = dbConn.getAutoCommit();
			dbConn.setAutoCommit(false);
		} catch (SQLException ex) {
			throw ex;
		}
	}

	//事务提交
	public void commit() throws SQLException {
		try {
			dbConn.commit();
			dbConn.setAutoCommit(autoCommit);
		} catch (SQLException ex) {
			throw ex;
		}
	}

	//事务回滚
	public void rollback() {
		try {
			dbConn.rollback();
			dbConn.setAutoCommit(autoCommit);
		} catch (SQLException ex) {
			ex.printStackTrace();
		}
	}

	
//自动提交
	public boolean getAutoCommit() throws SQLException {
		boolean result = false;
		try {
			result = dbConn.getAutoCommit();
		} catch (SQLException ex) {
			throw ex;
		}
		return result;
	}

	
</pre><pre code_snippet_id="353403" snippet_file_name="blog_20140519_10_5766327" name="code" class="java">//查询
	public ResultSet executeQuery(String sql) throws SQLException {
		ResultSet rs = null;
		try {
			stmt = dbConn.createStatement();
			rs = stmt.executeQuery(sql);
		} catch (SQLException ex) {
			throw ex;
		}
		return rs;
	}

	
</pre><pre code_snippet_id="353403" snippet_file_name="blog_20140519_10_5766327" name="code" class="java">//更新,c插入,删除
	public int executeUpdate(String sql) throws SQLException {
		try {
			stmt = dbConn.createStatement();
			return stmt.executeUpdate(sql);
		} catch (SQLException ex) {
			throw ex;
		}
	}

	
</pre><pre code_snippet_id="353403" snippet_file_name="blog_20140519_10_5766327" name="code" class="java">//关闭数据库连接
	public void close() throws SQLException {
		try {
			if (stmt != null) {
				stmt.close();
				stmt = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (dbConn != null)
					dbConn.close();
			} catch (SQLException e) {
				e.printStackTrace();
				throw e;
			}
		}

	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值