封装DBUtil工具类简化JDBC操作

DBUtil.java代码如下:

package cn.zhangzhilin.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;

public class DBUtil {
	final static String URL = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=utf-8";
	final static String USERNAME = "root";
	final static String PWD = "123456";
	public static Connection connection = null;
	public static PreparedStatement pstmt = null;
	public static ResultSet resultSet = null;
	
	//获取链接
	public static Connection getConnection() throws ClassNotFoundException, SQLException {
		Class.forName("com.mysql.cj.jdbc.Driver");
		connection = DriverManager.getConnection(URL, USERNAME, PWD);
		return connection;
	}
	
	//参数赋值
	public static PreparedStatement creatPreparedStatement(String sql,Object[] params) throws SQLException, ClassNotFoundException {
		pstmt = getConnection().prepareStatement(sql);
		if (params!=null) {
			for (int i = 0; i < params.length; i++) {
				pstmt.setObject(i+1, params[i]);
			}
		}
		return pstmt;
	}
	
	//增删改
	public static boolean executeUpdate(String sql,Object[] params) {
		try {
			getConnection();
			pstmt = creatPreparedStatement(sql,params);
			int count = pstmt.executeUpdate();
			if (count>0) {
				return true;
			}else {
				return false;
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return false;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			closeAll(resultSet, pstmt, connection);
		}
	}
	
	//查询
	public static ResultSet executeQuery(String sql,Object[] params) {
		try {
			getConnection();
			pstmt = creatPreparedStatement(sql,params);
			resultSet = pstmt.executeQuery();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return resultSet;
	}
	
	//关闭连接
	public static void closeAll(ResultSet resultSet,Statement stmt,Connection connection) {
		try {
			if (resultSet != null) resultSet.close();
			if (stmt != null) stmt.close();
			if (connection != null) connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//获取总数
	public static int getTotalCount(String sql,Object[] params) {
		int count = -1;
		try {
			pstmt = creatPreparedStatement(sql, params);
			resultSet = pstmt.executeQuery();
			if(resultSet.next()) {
				count = resultSet.getInt(1);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch ( SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeAll(resultSet, pstmt, connection);
		}
		return count;
	}
	
	
}

使用案例

增加用户

public boolean addUser(User user) {
		System.out.println(user);
		String sql = "insert into user VALUES (?,?,?,?)";
		//DBUtil的executeUpdate(String sql,Object[] params)方法返回boolean标识是否执行成功。
		return DBUtil.executeUpdate(sql, new Object[] {user.getId(),user.getNickname(),user.getUsername(),user.getPassword()});
	}

查询用户

public boolean queryUser(User user) {
		String sql = "select * from user where user_name=? and user_password=?";
		//DBUtil的executeQuery(String sql,Object[] params)方法返回resultSet对象,只需要对resultSet对象做相应处理即可。
		ResultSet resultSet = DBUtil.executeQuery(sql, new Object[] {user.getUsername(),user.getPassword()});
		 try {
			if (resultSet.next()) {
				 return true;
			}else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			//调用DBUtil的静态方法closeAll(ResultSet resultSet,Statement stmt,Connection connection)关闭连接。
			DBUtil.closeAll(resultSet, DBUtil.pstmt,  DBUtil.connection);
		}
	}

你可能感兴趣的:(java,jdbc,mysql,sql)