DBUtil工具类

利用了函数式接口:

import java.sql.ResultSet;

@FunctionalInterface
public interface IRowMapper {
	void rowMapper(ResultSet resultSet);
}

DBUtil代码:

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

/**
 * 数据库工具类
 *
 * @author 徐冰
 */
public class DBUtil {
	static {
		try {
			Class.forName("com.mysql.jdbc.Driver"); //1、加载驱动类
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取连接
	 *
	 * @author 徐冰
	 */
	private static Connection getConnection() {
		try {
			return DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root"); //2、获取连接
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 修改数据
	 *
	 * @author 徐冰
	 */
	public static boolean update(String sql) {
		Connection connection = null;
		Statement statement = null;
		try {
			connection = getConnection();
			statement = connection.createStatement(); //3、创建语句
			int result = statement.executeUpdate(sql); //4、执行语句
			if (result > 0) { //5、处理结果
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally { //6、释放资源
			close(connection, statement);
		}
		return false;
	}

	/**
	 * 修改数据
	 *
	 * @author 徐冰
	 */
	public static boolean update(String sql, Object... para) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = getConnection();
			preparedStatement = connection.prepareStatement(sql); //3、创建语句
			for (int i = 1; i <= para.length; i++) { 
				preparedStatement.setObject(i, para[i - 1]);
			}
			int result = preparedStatement.executeUpdate(); //4、执行语句
			if (result > 0) { //5、处理结果
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(connection, preparedStatement); //6、释放资源
		}
		return false;
	}

	/**
	 * 查询数据
	 *
	 * @author 徐冰
	 */
	public static void select(String sql, IRowMapper rowMapper) {
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			connection = getConnection();
			statement = connection.createStatement(); //3、创建语句
			resultSet = statement.executeQuery(sql);  //4、执行语句
			rowMapper.rowMapper(resultSet);  //5、处理结果
		} catch (Exception e) {
			e.printStackTrace(); 
		} finally {
			close(connection, statement, resultSet);  //6、释放资源
		}
	}

	/**
	 * 查询数据
	 *
	 * @author 徐冰
	 */
	public static void select(String sql, IRowMapper rowMapper, Object... para) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			connection = getConnection();
			preparedStatement = connection.prepareStatement(sql);  //3、创建语句
			for (int i = 1; i <= para.length; i++) {
				preparedStatement.setObject(i, para[i - 1]);
			}
			resultSet = preparedStatement.executeQuery();  //4、执行语句
			rowMapper.rowMapper(resultSet); //5、处理结果
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(connection, preparedStatement, resultSet);  //6、释放资源
		}
	}

	/**
	 * 批量处理
	 *
	 * @author 徐冰
	 */
	public static boolean batch(String... sqls) {
		Connection connection = null;
		Statement statement = null;
		try {
			connection = getConnection();
			connection.setAutoCommit(false);
			statement = connection.createStatement();
			for (String sql : sqls) {
				statement.addBatch(sql);
			}
			statement.executeBatch();
			connection.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			if (connection != null) {
				try {
					connection.rollback();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		} finally {
			close(connection, statement);
		}
		return false;
	}

	/**
	 * 批量处理
	 *
	 * @author 徐冰
	 */
	public static boolean batch(String sql, Object[]... para) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = getConnection();
			connection.setAutoCommit(false);
			preparedStatement = connection.prepareStatement(sql);
			for (int i = 0; i < para.length; i++) {
				for (int j = 1; j <= para[i].length; j++) {
					preparedStatement.setObject(j, para[i][j - 1]);
				}
				preparedStatement.addBatch();
			}
			preparedStatement.executeBatch();
			connection.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			if (connection != null) {
				try {
					connection.rollback();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		} finally {
			close(connection, preparedStatement);
		}
		return false;
	}
	
	/**
	 * 判断数据是否存在
	 *
	 * @author 徐冰
	 */
	public static boolean exist(String sql, Object... para) {
		class RowMapper implements IRowMapper {
			boolean state;

			@Override
			public void rowMapper(ResultSet resultSet) {
				try {
					state = resultSet.next();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		RowMapper rowMapper = new RowMapper();
		select(sql, rowMapper, para);
		return rowMapper.state;
	}

	/**
	 * 释放资源
	 *
	 * @author 徐冰
	 */
	public static void close(Connection connection, Statement statement) {
		if (statement != null) {
			try {
				statement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		if (connection != null) {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 释放资源
	 *
	 * @author 徐冰
	 */
	public static void close(Connection connection, Statement statement, ResultSet resultSet) {
		if (resultSet != null) {
			try {
				resultSet.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		close(connection, statement);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值