Java Universal Oracle Database connection

Source: Internet
Author: User

Oracle database connection in Java write a generic class Ubutil () {}

ImportJava.io.InputStream;ImportJava.sql.*;Importjava.util.Properties; Public classDbutil {Private StaticConnection con; Private StaticString URL; Private StaticString User; Private StaticString pwd;  PublicDbutil () {}Static {        Try{class.forname ("Oracle.jdbc.driver.OracleDriver"); InputStream is= Dbutil.class. getResourceAsStream ("/db.properties");//db.properties is a user profile to pass user name password Properties prop=NewProperties ();            Prop.load (IS); URL=prop.getproperty ("url"); User=prop.getproperty ("User"); PWD=prop.getproperty ("Password"); Con=drivermanager.getconnection (URL, user, pwd); }Catch(Exception e) {}} Public StaticResultSet Find (String sql) {con=Getcon (); Try{Statement SMT=con.createstatement (); ResultSet RS=smt.executequery (SQL); returnrs; } Catch(SQLException e) {e.printstacktrace (); return NULL; }    }     Public StaticResultSet find (String sql,object ... pram) {//... pram Arraycon=Getcon (); Try{PreparedStatement SMT=con.preparestatement (SQL);  for(inti=0;i<pram.length;i++) {Smt.setobject (i+1, Pram[i]); } ResultSet RS=Smt.executequery (); returnrs; } Catch(SQLException e) {e.printstacktrace (); return NULL; }    }     Public Static voidInsert (String sql,object ... pram) {//... pram Arraycon=Getcon (); Try{PreparedStatement SMT=con.preparestatement (SQL);  for(inti=0;i<pram.length;i++) {Smt.setobject (i+1, Pram[i]);        } smt.executeupdate (); } Catch(SQLException e) {e.printstacktrace (); }    }     Public StaticConnection Getcon () {Try {            if(con==NULL||con.isclosed ()) Con=drivermanager.getconnection (URL, user, pwd); } Catch(SQLException e) {e.printstacktrace (); }        returncon; }}

Java Universal Oracle Database connection

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.