JDBCutils 클래스 작성(연결 만들기, 연결 닫기)

1245 단어
package com.usc.jdbcutil;

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

/*
 *  JDBC 
 *  , 
 */
public class JDBCUtils {
	
	private JDBCUtils(){}
	private static Connection con;
	
	static{
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://localhost:3306/mybase";
			String username = "root";
			String password ="";
		    con = DriverManager.getConnection(url,username,password);
		} catch (Exception e) {
			throw new RuntimeException(e+" ");
		}
		
	}
	
	/*
	 *  , 
	 */
	public static Connection getConnection(){
		return con;
	}
	
	public static void close(Connection con,Statement stat){

		if(stat!=null){
			try{
				stat.close();
			}catch(SQLException ex){}
		}
		if(con!=null){
			try{
				con.close();
			}catch(SQLException ex){}
		}
	}
	
	public static void close(Connection con,Statement stat,ResultSet rs){
		if(rs!=null){
			try{
				rs.close();
			}catch(SQLException ex){}
		}
		if(stat!=null){
			try{
				stat.close();
			}catch(SQLException ex){}
		}
		if(con!=null){
			try{
				con.close();
			}catch(SQLException ex){}
		}
	}
}

좋은 웹페이지 즐겨찾기