package com.gyarmy.Utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ResourceBundle; public class JdbcUtils { private static String dbClass; private static String url; private static String username; private static String password; static{ ResourceBundle rb = ResourceBundle.getBundle("db"); dbClass = rb.getString("dbClass"); url = rb.getString("url"); username = rb.getString("username"); password = rb.getString("password"); } //链接数据库 public static Connection getConnection(){ //加载驱动器 loadDriver(); //建立链接 try { Connection conn = DriverManager.getConnection(url,username,password); return conn; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } private static void loadDriver() { // TODO Auto-generated method stub try { Class.forName(dbClass); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //释放资源 /* * Connection conn = null; Statement stmt = null; ResultSet rs = null; */ public static void releaseRes(ResultSet rs ,Statement stmt ,Connection conn) { if(rs!=null){ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } rs =null; } if(stmt!=null){ try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } stmt=null; } if(conn!=null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } conn=null; } } }
0则评论给“jdbc的工具类(一)”