Java关联ORACLE数据库

 1 //Java连接ORACLE数据库
 2 
 3 import java.sql.*;
 4 /*import java.sql.Connection;
 5 import java.sql.DriverManager;
 6 import java.sql.SQLException;
 7 */
 8 
 9 public class DBUtil {
10 
11     private static String user = "pec";
12     private static String password = ""; //密码
13     private static String url = "jdbc:oracle:thin:@10.96.119.23:1521:bitest";
14 
15     static {
16         try {
17             Class.forName("oracle.jdbc.driver.OracleDriver");
18         } catch (ClassNotFoundException e) {
19             System.out.println("找不到驱动类");
20         }
21     }
22 
23     public static Connection getConnection() {
24         Connection conn = null;
25         try {
26             conn = DriverManager.getConnection(url, user, password);
27             System.out.println("连接到数据库");
28         } catch (SQLException e) {
29             e.printStackTrace();
30         }
31         return conn;
32     }
33 
34     public static void closeConn(Connection conn, PreparedStatement st,
35             ResultSet rs) {
36         try {
37             if (rs != null && !rs.isClosed()) {
38                 rs.close();
39             }
40         } catch (SQLException e1) {
41             e1.printStackTrace();
42         } finally {
43             try {
44                 if (st != null && !st.isClosed()) {
45                     st.close();
46                 }
47             } catch (SQLException e) {
48                 e.printStackTrace();
49             } finally {
50                 try {
51                     if (conn != null && !conn.isClosed()) {
52                         conn.close();
53                     }
54                 } catch (SQLException e) {
55                     e.printStackTrace();
56                 }
57             }
58         }
59 
60     }
61 
62 }
Java连接数据库

 

posted on 2016-06-15 16:37  lanshure  阅读(92)  评论(0编辑  收藏  举报

导航