java连接oracle(总结版)

前言:之前利用java连接mysql,感觉基本的原理其实差不多

直接用源码:

 1 package com.mon11.day18;
 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 import javax.naming.spi.DirStateFactory.Result;
10 
11 /**
12  * 类说明 :
13  * 
14  * @author 作者 :chenyanlong
15  * @version 创建时间:2017年11月18日
16  */
17 public class Base {
18 
19     String driver_class = "oracle.jdbc.driver.OracleDriver";
20     String url = "jdbc:oracle:thin:@localhost:1521:orcl";
21     String user = "scott";
22     String password = "tiger";
23 
24     Connection conn = null;
25 
26     public static void main(String[] args) throws Exception {
27           Base bs=new Base();
28           System.out.println(bs.getConnection1());
29     }
30 
31     /**
32      * 获取数据库连接
33      * @return 
34      * @throws Exception 
35      */
36     public Connection getConnection1() throws Exception{
37         try {
38             Class.forName(driver_class);//加载驱动
39             conn=DriverManager.getConnection(url, user, password);
40             
41             
42         } catch (ClassNotFoundException e) {
43             // TODO Auto-generated catch block
44             //e.printStackTrace();
45             throw new Exception("连接出错了"+e.getMessage());
46         }
47         return conn;
48     }
49     
50     /**
51      * 关闭驱动
52      * @throws Exception 
53      * 
54      */
55     public void colseAll(ResultSet rs,PreparedStatement ps,Connection conn) throws Exception{
56          
57             try {
58                 
59                 if(rs!=null){
60                   rs.close();
61                 }
62                 if(ps!=null){
63                     ps.close();
64                 }
65                 if(conn!=null){
66                     conn.close();
67                 }
68             } catch (SQLException e) {
69                 throw new Exception("异常关闭错误"+e.getMessage());
70             }
71         
72     }    
73 }
View Code

运行效果:

 

posted @ 2017-11-18 16:08  glacial_water  阅读(202)  评论(0编辑  收藏  举报
Document