ORACLE数据库JDBC连接

--建立驱动,使用SQL,通过传入 sql调用方法,获取结果集


public static ResultSet getResult(String sql){
    <pre name="code" class="java">        Connection con = getConnection();
     try{
            Statement stmt = con.createStatement() ;   
            ResultSet rs = stmt.executeQuery(sql) ;
            //con.close();
            return rs;
        }catch(SQLException e){
            // TODO Auto-generated catch block
        }
        return null;   
    }

   
private static Connection getConnection(){
        try{  
            Properties p = new Properties();
            InputStream in =null;
            String driver="";
            String username="";
            String password="";
            String url="";
            try{
                 in=new FileInputStream("WebContent/datasource.properties");
                 p.load(in);
                 driver=p.getProperty("jdbc.driverClassName");
                 url=p.getProperty("jdbc.url");
                 username=p.getProperty("jdbc.username");
                 password=p.getProperty("jdbc.password");
                 System.out.println(driver);
            }catch(FileNotFoundException e){
                // TODO Auto-generated catch block
                e.printStackTrace();
            }catch(IOException e){
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //加载oracle驱动
            Class.forName(driver);
            Connection con =    
                    DriverManager.getConnection(url , username , password ) ; 
            return con;
        }catch(ClassNotFoundException e){  
            System.out.println("找不到驱动程序类 ,加载驱动失败!");  
            e.printStackTrace() ;  
        }catch(SQLException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

数据库配置文本内容datasource.properties
<p>jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@172.18.94.63/orcl
username=
password=


 
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值