Java database connection reads the properties file

Source: Internet
Author: User

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

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.