2010.03.15——jdbcの使用

1786 ワード

2010.03.15——jdbcの使用
今日はバッチデータの書き出しにjdbcを使いますので、おなじみです
1.jdbcの6つのステップ
    
1)  Driver
     2)    
     3)  Statement
     4)  sql
     5)select--->     
     6)    (rs,stm,conn)

 
2.jdbcutil
public class JdbcUtil {
	private static Properties info=new Properties();
	static{	
		try {
			InputStream is=JdbcUtil.class
			   .getResourceAsStream("config/config.properties");
			info.load(is);
			is.close();
		} catch (Exception e) {
			throw  new ExceptionInInitializerError(e);
		}
	}
	
	private static final ThreadLocal<Connection> tl=new ThreadLocal<Connection>();
    public static Connection  getConnection() throws Exception{
    	Connection conn=tl.get();
    	if(conn==null){
    		Class.forName(info.getProperty("driver"));
    		conn=DriverManager.getConnection(info.getProperty("url"),
        			info.getProperty("username"),info.getProperty("password"));
    		tl.set(conn);
    	}
    	return conn;
    }
	public static void release(ResultSet rs,Statement stm,Connection conn){
		if(rs!=null)  try{ rs.close();} catch(Exception ee){}
		if(stm!=null)  try{ stm.close();} catch(Exception ee){}
		if(conn!=null) try{ conn.close();} catch(Exception ee){}
	}
	public static void main(String[] args) throws Exception{
		System.out.println(getConnection());
	}
}

config.properties
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:orcl
username=system
password=cody