JDBC共通接続、追加削除・クラス変更

17985 ワード

JDBC共通接続、追加削除・クラス変更
package com.xxx.yyy.dao;

import java.sql.*;

/**
 * 	>>>                    
 * 	    :
 *	public static void main(String[] args) throws SQLException {
 *		ResultSet rs = executeQuery("SELECT * FROM user;", null);
 *		while (rs.next()) {
 *			System.out.println(rs.getObject(1));
 *		}
 *		Object [] s = {1234560};
 *		boolean flag = executeUpdate("UPDATE `user` SET password = ?;",s);
 *		System.out.println(flag);
 *	}
 */
public class DBUtil {
	static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
	static final String URL = "jdbc:mysql://localhost:3306/his";
	static final String USERNAME = "root";
	static final String PASSWORD = "root";
	
	public static Connection connection = null;
	public static PreparedStatement pstmt = null;
	public static ResultSet rs = null;
	/**
	 *	>>>        DriverManager.getConnection(URL,USERNAME,PASSWORD);
	 */
	public static Connection getConnection() throws SQLException, ClassNotFoundException {
		Class.forName(JDBC_DRIVER);
		return DriverManager.getConnection(URL,USERNAME,PASSWORD);
	}
	/**
	 *	>>>          DriverManager.getConnection(URL,USERNAME,PASSWORD);
	 */
	public static Connection getOneConnection() throws SQLException, ClassNotFoundException {
		if (DBUtil.connection == null) {
			return getConnection();
		} else {
			return DBUtil.connection;
		}
	}
	/**
	 * 	>>>  String sql      ?   
	 */
	public static PreparedStatement createPreParedStatement(String sql,Object[] params) throws SQLException, ClassNotFoundException {
		connection = getOneConnection();
		pstmt = connection.prepareStatement(sql);
		if (params != null) {
			for (int i = 0; i < params.length; i++) {
				pstmt.setObject((i+1),params[i]);
			}
		}
		return pstmt;
	}
	/**
	 * 	(((      )))
	 * 	>>>  :String sql = "";  Object[] params = {int id,String name.....};
	 * 	>>>  :   boolean  t/f 
	 * 	>>>  :    boolean flag = null; 
	 * 			  flag = executeUpdate(String sql,Object[] params);
	 * 	>>>       if (pstmt!=null) pstmt.close();
	 *  		if (rs!=null) rs.close();
	 *		   if (DBUtil.connection!=null) DBUtil.connection.close();
	 */
	public static boolean executeUpdate(String sql,Object[] params) {
		try {
			pstmt = createPreParedStatement(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(connection, pstmt, rs);
		}
	}
	/**
	 * 	(((    )))                                                
	 * 	>>>  :String sql = "select ? from `user` where id =?";
	 * 		?   Object[] params = {String name,int id.....};   null
	 * 	>>>  :  PreparedStatement pstmt = null;     rs
	 * 	>>>  :     PreparedStatement pstmt = null;
	 * 		 pstmt = executeQuery(String sql,Object[] params)
	 *	>>>        DBUtil.closeAll(DBUtil.connection, pstmt, rs);
	 *			
	 */
	public static ResultSet executeQuery(String sql,Object[] params) {
		try {
			pstmt = createPreParedStatement( sql,params);
			rs = pstmt.executeQuery();
			return rs;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} 
	}
/**
 * 	(((       )))
 * 			try {
 *				if (pstmt!=null) pstmt.close();
 *				if (rs!=null) rs.close();
 *				if (connection!=null) connection.close();
 *			} catch (SQLException e) {
 *				e.printStackTrace();
 *			}
 * 		
 */
	public static void closeAll(Connection connection,Statement pstmt,ResultSet rs) {
			try {
				if (pstmt!=null) pstmt.close();
				if (rs!=null) rs.close();
				if (connection!=null) connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
	}
	
}