JDBC的工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
A: 抽取两个方法,一个获取Connection对象,一个是释放资源
 
 
import java.io.FileReader;
 
import java.sql.Connection;
 
import java.sql.DriverManager;
 
import java.sql.ResultSet;
 
import java.sql.SQLException;
 
import java.sql.Statement;
 
import java.util.Properties;
 
  
 
public class JdbcUtil {       
 
         private JdbcUtil() {}                                       // 私有化构造方法     
 
         // 定义成员变量
 
         private static String jdbcUrl= null ;
 
         private static String userName = null ;
 
         private static String password = null ;
 
         // 加载数据
 
         static {                  
 
                   try {                           
 
                            Properties prop = new Properties() ;
 
                            prop.load(new FileReader("jdbc.properties")) ;
 
                            jdbcUrl = prop.getProperty("jdbcUrl") ;
 
                            userName = prop.getProperty("userName") ;
 
                            password = prop.getProperty("password") ;
 
                            Class.forName(prop.getProperty("driverClassName")) ;                  
 
                   } catch (Exception e) {
 
                            e.printStackTrace();
 
                   }                   
 
         }        
 
/**
 
          * 获取连接
 
          */
 
         public static Connection getConnection() throws SQLException {
 
                   return DriverManager.getConnection(jdbcUrl, userName, password) ;
 
         }       
 
         /**
 
          * 释放资源
 
          */
 
public static void close(Connection conn , Statement st , ResultSet rs) throws SQLException {
 
                   if(conn != null) conn.close() ;
 
                   if(st != null) st.close() ;
 
                   if(rs != null) rs.close() ;
 
         }
 
}

 

posted on   LoaderMan  阅读(178)  评论(0编辑  收藏  举报

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

喜欢请打赏

扫描二维码打赏

了解更多

点击右上角即可分享
微信分享提示