JavaWeb JDBC 패키지 클래스

4823 단어 JavaWeb
전언
JavaWeb을 공부할 때, 많은 다중 데이터베이스 조작을 써서, 이 코드를 봉인하여 코드의 작업량을 효과적으로 줄일 수 있다.말하지 말고 코드를 붙여라.
코드
봉인하기 전에 설정 파일이 필요합니다 --db.properties, 이 파일은 src 디렉터리에 두어야 합니다. 다른 가방에 두지 않는 것이 좋습니다. 코드는 다음과 같습니다.
driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/    
username =    
password =     

JDBC 패키지 클래스
public class DBUtil {

	private DBUtil() {
		throw new RuntimeException("       ");
	}

	static ResourceBundle bundle = ResourceBundle.getBundle("db");
	private static String driver = bundle.getString("driver");
	private static String url = bundle.getString("url");
	private static String username = bundle.getString("username");
	private static String password = bundle.getString("password");

	static {
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 *        
	 * 
	 * @return
	 */
	public static Connection getConnection() {
		try {
			return DriverManager.getConnection(url, username, password);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 *     
	 * 
	 * @param sql
	 *                 sql  
	 * @param params
	 *                 
	 * @return       
	 */
	public static int update(String sql, Object... params) {
		Connection connection = getConnection();
		PreparedStatement preparedStatement = null;
		try {
			preparedStatement = connection.prepareStatement(sql);
			if (params != null) {
				for (int i = 0; i < params.length; i++) {
					preparedStatement.setObject(i + 1, params[i]);
				}
			}

			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			close(preparedStatement, connection);
		}

		return 0;
	}

	public static int update(String sql, List> params) {
		return update(sql, params.toArray());
	}

	public static void updates(Map> map) {
		if (map != null) {
			Connection connection = getConnection();
			try {
				connection.setAutoCommit(false);
				for (String sql : map.keySet()) {
					PreparedStatement prepareStatement = connection.prepareStatement(sql);
					List params = map.get(sql);
					if (params != null) {
						for (int i = 0; i < params.size(); i++) {
							prepareStatement.setObject(i + 1, params.get(i));
						}
					}

					prepareStatement.executeUpdate();
					prepareStatement.close();
				}

				connection.commit();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 *     
	 * 
	 * @param sql
	 *                 sql  
	 * @param params
	 *                 
	 * @return       :Map             
	 */
	public static List> retrieve(String sql, Object... params) {
		List> list = new ArrayList<>();
		Connection connection = getConnection();
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			preparedStatement = connection.prepareStatement(sql);
			if (params != null) {
				for (int i = 0; i < params.length; i++) {
					preparedStatement.setObject(i + 1, params[i]);
				}
			}
			resultSet = preparedStatement.executeQuery();
			ResultSetMetaData metaData = resultSet.getMetaData();
			int columnCount = metaData.getColumnCount();
			while (resultSet.next()) {
				Map map = new HashMap<>();
				for (int i = 0; i < columnCount; i++) {
					String columnName = metaData.getColumnName(i + 1);
					Object value = resultSet.getObject(columnName);
					if (value == null) {
						value = "";
					}

					map.put(columnName, value);
				}

				list.add(map);
			}

			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(resultSet, preparedStatement, connection);
		}

		return null;
	}

	public static void close(ResultSet resultSet) {
		if (resultSet != null) {
			try {
				resultSet.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public static void close(PreparedStatement preparedStatement) {
		if (preparedStatement != null) {
			try {
				preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

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

	public static void close(PreparedStatement preparedStatement, Connection connection) {
		close(preparedStatement);
		close(connection);
	}

	public static void close(ResultSet resultSet, PreparedStatement preparedStatement, Connection connection) {
		close(resultSet);
		close(preparedStatement);
		close(connection);
	}
}

좋은 웹페이지 즐겨찾기