jdbc 连接数据库、批量操作、事务(oracle、mysql)

 

 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.PreparedStatement;
 6 import java.sql.ResultSet;
 7 import java.sql.SQLException;
 8 
 9 public class JdbcUtil
10 {
11     public static final String USERNAME = "root";
12 
13     public static final String PASSWORD = "root";
14 
15     public static final String URL = "jdbc:oracle:thin:@192.168.2.1:1521:orcl";
16 
17     static
18     {
19         try
20         {
21             Class.forName("oracle.jdbc.driver.OracleDriver");
22         }
23         catch (ClassNotFoundException e)
24         {
25             e.printStackTrace();
26         }
27     }
28 
29     public static Connection getConnection()
30     {
31         Connection conn = null;
32         try
33         {
34             conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
35         }
36         catch (SQLException e)
37         {
38             e.printStackTrace();
39         }
40 
41         return conn;
42     }
43 
44     public static void closeConnection(ResultSet rs, PreparedStatement ps, Connection conn)
45     {
46         try
47         {
48             if (rs != null)
49             {
50                 rs.close();
51             }
52         }
53         catch (Exception e)
54         {
55             // TODO: handle exception
56         }
57         finally
58         {
59             try
60             {
61                 if (ps != null)
62                 {
63                     ps.close();
64                 }
65             }
66             catch (Exception e2)
67             {
68                 // TODO: handle exception
69             }
70             finally
71             {
72                 try
73                 {
74                     if (conn != null)
75                     {
76                         conn.close();
77                     }
78                 }
79                 catch (SQLException e)
80                 {
81                     e.printStackTrace();
82                 }
83             }
84 
85         }
86     }
87 }



 

posted @ 2012-10-22 23:44  刺客XIII  阅读(1786)  评论(0编辑  收藏  举报