DBUtil工具类(带源码)

包括数据库的连接,登录,注册,修改(增删改),查询,判断信息是否在数据库中存在,事务的批量处理,释放资源

package com.jd.util;
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");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取数据库连接
	 * 
	 * @author 李庚威
	 */
	private static Connection getConnection() {
		try {
			String url = PropertiesUtil.getValue("jdbc.url");
			String uN = PropertiesUtil.getValue("jdbc.uN");
			String password = PropertiesUtil.getValue("jdbc.password");
			return DriverManager.getConnection(url, uN, password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 修改数据
	 * @author 李庚威
	 */
	public static boolean update(String sql){
		Statement statement = null;
		Connection connection = null;
        try {
        	connection = getConnection();
            statement = connection.createStatement();
            int result = statement.executeUpdate(sql);
            return result>0;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(statement, connection);
        }
        return false;
    }
	
	/**
	 * 修改数据
	 * 防止sql注入
	 * @author 李庚威
	 */
	public static boolean update(String sql,Object ... params) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = getConnection();
			preparedStatement = connection.prepareStatement(sql);
			for (int i = 1; i <= params.length; i++) {
				preparedStatement.setObject(i, params[i-1]);
			}
			int result = preparedStatement.executeUpdate();//这里!!!!!!!!!!
			return result>0;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(preparedStatement,connection);
		}
		return false;
	}
	
	/**
	 * 判断数据是否存在
	 * 
	 * @author 李庚威
	 */
	public static boolean exist(String sql) {
		
		class RowMapper implements IRowMapper{
			boolean flag;
			@Override
			public void rowMapper(ResultSet rs) {
				try {
					flag=rs.next();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		RowMapper rowMapper = new RowMapper();
		select(sql,rowMapper);
		return rowMapper.flag;
	}
	
	/**
	 * 判断数据是否存在
	 * 防止sql注入
	 * @author 李庚威
	 */
	public static boolean exist(String sql,Object ...params) {
		
		class RowMapper implements IRowMapper{
			boolean flag;
			@Override
			public void rowMapper(ResultSet rs) {
				try {
					flag=rs.next();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		RowMapper rowMapper = new RowMapper();
		select(sql,rowMapper,params);
		return rowMapper.flag;
	}
	
	/**
	 * 注册
	 * 防止sql注入
	 * @author 李庚威
	 */
	public static void login(String sql,Object ...params ) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = getConnection();
			preparedStatement = connection.prepareStatement(sql);
			for(int i=1; i<=params.length;i++) {
				preparedStatement.setObject(i, params[i-1]);
			}
			int result = preparedStatement.executeUpdate();
			if(result>0) {
				System.out.println("注册成功");
			}else {
				System.out.println("注册失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(preparedStatement, connection);
		}
	}
	
	/**
	 * 查询数据
	 * 防止sql注入
	 * @author 李庚威
	 */
	public static void select(String sql,IRowMapper rowMapper,Object ... params) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet rs = null;
		try {
			connection = getConnection();
			preparedStatement = connection.prepareStatement(sql);
			for (int i = 1; i <= params.length; i++) {
				preparedStatement.setObject(i,params[i-1]);
			}
			rs = preparedStatement.executeQuery();//把数据库响应的查询结果存放在ResultSet类对象中
			rowMapper.rowMapper(rs);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(rs,preparedStatement, connection);
		}
	}
	/**
	 * 批量处理
	 * 
	 * @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(statement, connection);
		}
		return false;
	}
	
	/**
	 * 批量处理
	 * 防止sql注入
	 * @author 李庚威
	 */
	public static boolean batch(String sql,Object[] ... params) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = getConnection();
			connection.setAutoCommit(false);
			preparedStatement = connection.prepareStatement(sql);
			for (int i = 1; i <= params.length; i++) {
				for(int j = 1; j <= params[i].length; j++) {
					preparedStatement.setObject(i, params[i-1][j-1]);
				}
				preparedStatement.addBatch(sql);
			}
			preparedStatement.executeBatch();
			connection.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			if(connection!=null) {
				try {
					connection.rollback();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		} finally {
			close(preparedStatement, connection);
		}
		return false;
	}
	/**
	 * 释放资源
	 * 
	 * @author 李庚威
	 */
	public static void close(Statement statement,Connection connection) {
		if (statement != null) {
			try {
				statement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (connection != null) {
			try {
				statement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 释放资源
	 * 
	 * @author 李庚威
	 */
	public static void close(ResultSet rs,Statement statement,Connection connection){
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		close(statement,connection);//调用重载的方法
	}	
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值