Note: Web run read to put the configuration file under Web-inf
Driver=com.microsoft.sqlserver.jdbc.sqlserverdriver
url=jdbc:sqlserver://192.168.33.170;databasename=platform_u_v3.0
Username=xxzx
Password=xxzx
Drive database connection account password from top to bottom
PackageCom.bowei.utl;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement;Importjava.util.Properties; Public classJdbcutl {//Database-driven Private StaticString Driver = ""; //Database Address Private StaticString url = ""; //Database user name Private StaticString userName = ""; //Database Password Private StaticString Password = ""; Static{Properties P=NewProperties (); Try { //loading files with relative pathsString Path=thread.currentthread (). Getcontextclassloader (). GetResource (""). toString (); Path=path.replace ('/', ' \ \ ');//Convert/replace \Path=path.replace ("File:", "");//Remove File:Path=path.replace ("Classes\\", "" ");//Remove Class\Path=path.substring (1);//remove the first \, such as \d:\javaweb ...path+= "/sof/jdbc.properties"; P.load (NewFileInputStream (NewFile (path)); //use the GetProperty method to get information by keywordDriver = P.getproperty ("Driver"); URL= P.getproperty ("url"); UserName= P.getproperty ("UserName"); Password= P.getproperty ("Password"); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } } Public StaticString Getdriver () {returnDriver; } Public Static voidsetdriver (String driver) {driver=driver; } Public StaticString GetUrl () {returnURL; } Public Static voidseturl (String url) {jdbcutl.url=URL; } Public StaticString GetUserName () {returnUserName; } Public Static voidsetusername (String userName) {jdbcutl.username=UserName; } Public StaticString GetPassword () {returnpassword; } Public Static voidSetPassword (String password) {Jdbcutl.password=password; } //Get database connection Public StaticConnection Getconn () {Connection conn=NULL; Try { //Load DriverClass.forName (Driver); //Get database connectionconn =drivermanager.getconnection (URL, userName, password); } Catch(SQLException e) {e.printstacktrace (); } Catch(ClassNotFoundException e) {e.printstacktrace (); } returnConn; } //Close Database Resources Public Static voidCloseAll (ResultSet rs, Statement Stat, Connection conn) {/*shut down the database's result set resources sequentially, Statement object resources, and connection connection database objects, respectively*/ if(rs! =NULL) { Try{rs.close (); } Catch(SQLException e) {e.printstacktrace (); } } if(Stat! =NULL) { Try{stat.close (); } Catch(SQLException e) {e.printstacktrace (); } } if(Conn! =NULL) { Try{conn.close (); } Catch(SQLException e) {e.printstacktrace (); } } }}
How SQL Server database connection configuration is read outside