Read *.properties File connection database

Source: Internet
Author: User

Requirement: system A needs to insert data for system B databases.

Application Scenario:The core system calculates the payroll data and sends the voucher for the financial system.

Implementation process:

  1. Create a new Db.properties file and write the database configuration file:
    driver=oracle.jdbc.driver.OracleDriverurl=*********************username=******password =******

  2. Read the file and connect to the database: (read via ClassLoader):

    Package Cn.com.cis.acic.util;import java.io.ioexception;import java.io.inputstream;import java.sql.Connection; Import Java.sql.drivermanager;import java.sql.resultset;import java.sql.sqlexception;import java.sql.Statement; Import java.util.Properties;/** * Define a tool class that is responsible for connecting to the database and shutting down the data resources*/ Public classDbutil {Private StaticFinal String location="dboracle.properties"; Private StaticProperties props=NewProperties (); Static{ ClassLoader loader =dbutil.class.getclassloader ();        InputStream Is=loader.getresourceasstream (location);        try {props.load (IS); } catch (IOException e) {System.out.println ("Populate the Properties object failed!"));        E.printstacktrace (); }    }        PrivateDbutil () {} Public StaticConnection getconnection () { String driver =props.getproperty ("Driver");        String url=props.getproperty ("url");        String Username=props.getproperty ("username");        String password=props.getproperty ("password"); Try{class.forname (driver); } Catch(ClassNotFoundException e) {System. out. println ("database driver class failed to load!");        E.printstacktrace (); } Connection Conn=NULL; Try{conn=drivermanager.getconnection (URL, username, password); } Catch(SQLException e) {System. out. println ("failed to connect to database!");        E.printstacktrace (); }        returnConn; }         Public Static voidClose (ResultSet rs,statement stmt, Connection conn) {if(rs!=NULL){            Try{rs.close (); } Catch(SQLException e) {System. out. println ("failed to close result set Object!");            E.printstacktrace (); }        }        if(stmt!=NULL){            Try{stmt.close (); } Catch(SQLException e) {System. out. println ("closing Statement object failed!");            E.printstacktrace (); }        }        if(conn!=NULL){            Try{conn.close (); } Catch(SQLException e) {System. out. println ("failed to close connection Object!");            E.printstacktrace (); }        }    }     Public Static voidClose (ResultSet rs, Statement stmt) {Close (rs,stmt,NULL); }     Public Static voidClose (Statement stmt, Connection conn) {Close (NULL, Stmt,conn); }     Public Static voidClose (Connection conn) {Close (NULL,NULL, conn); }}

    can also be read directly, such as:

                //To read a database link:InputStreaminch= Feemanageservicespringimpl.class. getResourceAsStream ("/accountjdbc.properties");//file nameInputStreaminch=NewBufferedinputstream (NewFileInputStream ("d:\\tomcat-7\\webapps\\sales\\web-inf\\classes\\accountjdbc.proerties"));//Detailed AddressProperties p =NewProperties (); P.load (inch); String className2= P.getproperty ("DRIVER"); String URL= P.getproperty ("URL"); String User= P.getproperty ("USER"); String Password= P.getproperty ("PASSWORD"); Class.forName (className2);

Read *.properties File connection database

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.