JDBC JdbcUtils( 本博多次出现的简陋工具类)

package test;

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

/* 简陋工具类  */ 
public final class JdbcUtils { 
    private static String url="jdbc:mysql://localhost:3306/world"; 
    private static String user = "root"; 
    private static String password="mysql";

    static { 
        try { 
            Class.forName("com.mysql.jdbc.Driver"); 
        } catch (ClassNotFoundException e) { 
            throw new ExceptionInInitializerError(e); 
        }

    } 
    
    private JdbcUtils() {} 
    
    public static Connection getConnection() throws SQLException { 
        return DriverManager.getConnection(url,user,password); 
    } 
    
    public static void free(ResultSet rs,Statement st,Connection conn) { 
        try { 
            if (rs != null) { 
                rs.close(); 
            } 
        }catch(SQLException e) { 
            e.printStackTrace(); 
        } 
        finally { 
            try { 
                if (st != null) { 
                    st.close(); 
                } 
            } catch(SQLException e){ 
                e.printStackTrace(); 
            } finally { 
                if (conn != null) 
                    try { 
                        conn.close(); 
                    } catch (SQLException e) { 
                        e.printStackTrace(); 
                    } 
            } 
        } 
    } 
}

posted @ 2013-12-06 15:51  剑握在手  阅读(420)  评论(0编辑  收藏  举报
返回顶部↑