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