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:
- Create a new Db.properties file and write the database configuration file:
driver=oracle.jdbc.driver.OracleDriverurl=*********************username=******password =******
- 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