javabean

package dbc; import java.sql.*; import java.util.Properties; public class JdbcUtil { static String driver; static String url; static String user; static String password; static Properties pr=new Properties(); JdbcUtil(){} static{ try{pr.load(JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties")); driver=pr.getProperty("driver"); url=pr.getProperty("url"); user=pr.getProperty("username"); password=pr.getProperty("password"); Class.forName(driver); }catch(Exception e){ throw new ExceptionInInitializerError(e); } } 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)rs.close();}catch(SQLException e){e.printStackTrace();} finally{if(conn!=null)try{conn.close();}catch(SQLException e){e.printStackTrace();}}} } }

posted on 2017-10-30 21:11  哈哈呵  阅读(119)  评论(0)    收藏  举报

导航