ORACLE数据库JDBC连接

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

    public static ResultSet getResult(String sql){
        Connection con = getConnection();//<span style="font-family: Arial, Helvetica, sans-serif;">获取驱动
</span>        try{
            Statement stmt = con.createStatement() ;   
            ResultSet rs = stmt.executeQuery(sql) ;  //获取结果
            //con.close();
            return rs;
//            while(rs.next()){
//                String pass = rs.getString(1) ; // 此方法比较高效(列是从左到右编号的,并且从列1开始)   
//            }
        }catch(SQLException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        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;
    }

--数据库配置文本内容(<span style="font-family: Arial, Helvetica, sans-serif;">datasource.properties</span>)
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
#jdbc.url=jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=10.91.76.42)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=10.91.76.43)(PORT=1521)))(FAILOVER=yes)(LOAD_BALANCE=yes)(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)(instance_name=orcl1)(instance_name=orcl2)))
#jdbc.username=drm_pfm
#jdbc.password=drm_pfm1010

jdbc.url=jdbc:oracle:thin:@172.18.94.63/orcl
jdbc.username=csg_mdm
jdbc.password=csg_mdm



你可能感兴趣的:(ORACLE数据库JDBC连接)