How SQL Server database connection configuration is read outside

Source: Internet
Author: User

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

Related Article

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.