标签:author resource 创建 class类 catch todo lock bsp import
1 package com.cn.utils; 2 3 import java.io.InputStream; 4 import java.sql.Connection; 5 import java.sql.DriverManager; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 import java.sql.Statement; 9 import java.util.Properties; 10 11 /** 12 * JDBC工具类 13 * 14 * @author15 */ 16 public class JDBCUtils { 17 static Properties prp = null; 18 static { 19 // 读取JDBC.properties属性文件 20 // 创建Properties属性对象 21 prp = new Properties(); 22 InputStream inStream = JDBCUtils.class.getResourceAsStream("/JDBC.properties"); 23 try { 24 prp.load(inStream); 25 // 驱动的class类对象 驱动的注册 driverClassName 驱动名称 26 Class.forName(prp.getProperty("driverClassName")); 27 } catch (Exception e) { 28 e.printStackTrace(); 29 } 30 } 31 32 // 获取连接的方法 33 public static Connection getConnection() throws SQLException { 34 // 获取到Connection对象 35 return DriverManager.getConnection(prp.getProperty("url"), prp.getProperty("user"), 36 prp.getProperty("password")); 37 } 38 39 // 关闭资源 :ResultSet Connection Statement 40 public static void close(ResultSet rs, Connection conn, Statement st) { 41 try { 42 if (rs != null) { 43 rs.close(); 44 } 45 } catch (SQLException e) { 46 // TODO Auto-generated catch block 47 e.printStackTrace(); 48 } 49 try { 50 if (conn != null) { 51 conn.close(); 52 } 53 } catch (SQLException e) { 54 // TODO Auto-generated catch block 55 e.printStackTrace(); 56 } 57 try { 58 if (st != null) { 59 st.close(); 60 } 61 } catch (SQLException e) { 62 // TODO Auto-generated catch block 63 e.printStackTrace(); 64 } 65 } 66 }
标签:author resource 创建 class类 catch todo lock bsp import
原文地址:http://www.cnblogs.com/xiaoxiaoyisheng/p/7460296.html