Java连接mysql数据库可重复利用

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class ConDB {
	static String driver = "com.mysql.jdbc.Driver";
	static String url = "jdbc:mysql://localhost:3306/test";
    //用户名和密码
	static String user = "root";
	static String password = "root";

	// 获取链接
	public static Connection getConn() {
		Connection connection = null;
		try {
			Class.forName(driver);
			connection = DriverManager.getConnection(url, user, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return connection;
	}

	// 获取Statement类
	// @param connection 连接
	public static Statement getStatement(Connection connection) {
		Statement statement = null;
		try {
			statement = connection.createStatement();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return statement;
	}

	// 执行sql语句 主要用户查询的操作(适用于一条或多条查询结果)
	// @param statement statement对象
	// @param sql sql语句
	public static ResultSet getResultSet(Statement statement, String sql) {
		ResultSet resultSet = null;
		try {
			boolean execute = statement.execute(sql);
			if (execute) {
				resultSet = statement.getResultSet();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return resultSet;
	}

	// 获取去操作的行数
	// @param statement statement对象
	// @param sql sql语句
	// @return 0 为成功 -1 失败
	public static int getUpdateCount(Statement statement, String sql) {
		int count = 0;
		try {
			boolean execute = statement.execute(sql);
			if (execute) {
				count = statement.getUpdateCount();
			}
		} catch (SQLException e) {
			count = -1;
			e.printStackTrace();
		}
		return count;
	}

	// 关闭链接
	public static void close(ResultSet rs, Statement st, Connection conn) {
		try {
			if (rs != null) {
				rs.close();
			}
			if (st != null) {
				st.close();
			}
			if (conn != null) {
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws SQLException {
		// 查找
		/*
		 * ArrayList<Customer> list = new ArrayList<Customer>(); Connection conn =
		 * ConDB.getConn(); String sql = "select * from customer_info"; Statement
		 * statement = ConDB.getStatement(conn); ResultSet resultSet =
		 * ConDB.getResultSet(statement, sql); while (resultSet.next()) { Customer
		 * customer = new Customer(); customer.setLoginName(resultSet.getString(2));
		 * customer.setLoginPassword(resultSet.getString(3));
		 * customer.setRealName(resultSet.getString(4));
		 * customer.setAge(resultSet.getInt(5)); list.add(customer); }
		 * System.out.println(list.toString()); ConDB.close(resultSet, null, conn);
		 */

		// 插入
		/*
		 * Customer customer = new Customer(); customer.setLoginName("xiaoming");
		 * customer.setLoginPassword("123456"); customer.setRealName("小蓝");
		 * customer.setAge(23); Connection conn = ConDB.getConn(); String sql =
		 * "insert into customerinfo " +
		 * "(login_name,login_password,real_name,age) value " +
		 * "("+customer.getLoginName()+","+customer.getLoginPassword()
		 * +","+customer.getRealName()+","+customer.getAge()+")"; Statement statement
		 * = ConDB.getStatement(conn); int updateCount = ConDB.getUpdateCount(statement,
		 * sql); System.out.println(updateCount); ConDB.close(null, statement, conn);
		 */

	}

}

前几天看到了关于事务的东东,在Dao层的处理多条语句的时候,可以将事务关闭(默认是开启的)

conn.setAutoCommit(false);

然后执行任意多条的DML语句,在执行后进行事务的提交,

conn.commit();

在报出异常的时候进行事务的回滚

conn.rollback();

经验分享 程序员 微信小程序 职场和发展