Oracledbinfo.properties
DRIVER == Jdbc:oracle:thin: @localhost: 1521==m123
Oracleconnect.java
PackageCom.yiwei.util;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement;Importjava.util.Properties; Public classOracleconnect { Public StaticString DRIVER =NULL ; Public StaticString URL =NULL; Public StaticString USER =NULL ; Public StaticString PASSWORD =NULL; Static{Properties Properties=NULL; InputStream InputStream=NULL; Try{InputStream= Classloader.getsystemresourceasstream ("Com/yiwei/util/oracledbinfo.properties"); Properties=NewProperties (); Properties.load (InputStream); DRIVER= Properties.getproperty ("DRIVER"); URL= Properties.getproperty ("URL"); USER= Properties.getproperty ("USER"); PASSWORD= Properties.getproperty ("PASSWORD"); } Catch(Exception e) {Throw NewRuntimeException ("Database Property loading exception!")); }finally{ Try { if(InputStream! =NULL) {inputstream.close (); InputStream=NULL; } } Catch(IOException e) {e.printstacktrace (); } } Try{class.forname (DRIVER); } Catch(Exception e) {Throw NewRuntimeException ("Database driver loading exception!")); } }; /*** Get Connections *@returnExamples of Oracle's connection*/ Public StaticConnection getoracleconnection () {Connection ct=NULL; Try{CT=drivermanager.getconnection (Url,user,password); } Catch(SQLException e) {Throw NewRuntimeException ("Get Database Connection Exception!")); } returnCT; }; /*** Close Database resources *@paramRs ResultSet Object *@paramSM Statement Object PreparedStatement Object Callablesatement Object *@paramCT Connection Object*/ Public Static voidClose (ResultSet RS, Statement Sm,connection ct) {Try { if(rs! =NULL) {Rs.close (); rs =NULL;} if(SM! =NULL) {sm.close (); sm =NULL;} if(ct! =NULL) {ct.close (); ct =NULL;} } Catch(SQLException e) {Throw NewRuntimeException ("Close Database resource Exception"); } }; Public Static voidmain (String [] args) {NewOracleconnect (); System.out.println (DRIVER+ "," +url+ "," +user+ "," +PASSWORD); }
}
Java database connection reads the properties file