JDBC代码简化优化——(三)DBUtil工具类的封装(still need to supplement)

代码如下

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

interface IRowMapper{
	void rowMapper(ResultSet rs);
}

public class DBUtil {
	
	public static void query(String sql,IRowMapper rowMapper) {
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root");
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			rowMapper.rowMapper(resultSet);//多态
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if (resultSet!=null) {
					resultSet.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			try {
				if (statement!=null) {
					statement.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			try {
				if (connection!=null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public static boolean update(String sql) {
		Connection connection = null;
		Statement statement= null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root");
			statement= connection.createStatement();
			return statement.executeUpdate(sql)>0;
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if (statement!=null) {
					statement.close();
				}
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			try {
				if (connection !=null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return false;
	}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

interface IRowMapper{
	void rowMapper(ResultSet rs);
}

public class DBUtil {
	
	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");//1、加载驱动
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	private static Connection getConnection() {//2、获取连接
		try {
			return DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void query(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、执行SQL
			rowMapper.rowMapper(resultSet);//多态,5、处理结果
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			close(resultSet,statement,connection);//6、释放资源
		}
	}

	public static boolean update(String sql) {
		Connection connection = null;
		Statement statement= null;
		try {
			connection = getConnection();
			statement= connection.createStatement();//3、创建语句
			return statement.executeUpdate(sql)>0;//4、执行SQL 5、处理结果
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			close(statement,connection);//6、释放资源
		}
		
		return false;
	}
	
	private static void close(Statement statement,Connection connection) {
		try {
			if (statement!=null) {
				statement.close();
			}
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		try {
			if (connection !=null) {
				connection.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	private static void close(ResultSet resultSet,Statement statement,Connection connection) {
		try {
			if (resultSet!=null) {
				resultSet.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		close(statement,connection);
	}
}

猜你喜欢

转载自blog.csdn.net/qq_44724446/article/details/89918464