PackageCom.xk.demotest.tools;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.SQLException;Importjava.sql.Statement;Importjava.util.Properties; Public classDaodbconecttools {/*//Traditional JDBC Connection private static final String drivername = "Com.mysql.jdbc.Driver"; Private static final String URL = "JDBC:MYSQL://LOCALHOST:3306/20170626JAVAWEB01"; Private static final String user = "root"; Private static final String password = "root"; Public Daodbconecttools () {Connection connect = null; Statement state = null; try {class.forname (drivername); Connect = drivermanager.getconnection (url, user, password); State = Connect.createstatement (); String sql = ""; State.executequery (SQL); } catch (ClassNotFoundException | SQLException e) {e.printstacktrace (); } finally {try {state.close (); Connect.close (); } catch (SQLException e) {e.printstacktrace (); } } }*/ Private Static FinalProperties Pro =NewProperties ();//① Creating a Properties Object//load configuration files and drivers Static{InputStream IStream= Thread.CurrentThread (). Getcontextclassloader (). getResourceAsStream ("config.properties");//② introducing a configuration file Try{pro.load (iStream);//② introducing a configuration fileString drivername = Pro.getproperty ("drivername");//③ Load DriverClass.forName (drivername); } Catch(IOException |classnotfoundexception e) {E.printstacktrace (); } } //get the connection to the image and create the connection Public StaticConnection Connection () {String URL= Pro.getproperty ("url"); String User= Pro.getproperty ("UserName"); String Password= Pro.getproperty ("Password"); Connection Connect=NULL; Try{Connect=drivermanager.getconnection (URL, user, password); } Catch(SQLException e) {e.printstacktrace (); } returnConnect; } //To close a database connection Public Static voidClose (Connection Connect, Statement state) {if(state! =NULL) { Try{state.close (); } Catch(SQLException e) {e.printstacktrace (); } } if(Connect! =NULL) { Try{connect.close (); } Catch(SQLException e) {e.printstacktrace (); } } } }
View Code
PackageCom.xk.demotest.tools;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.SQLException;Importjava.sql.Statement;Importjava.util.Properties; Public classDaodbconecttools {/*//Traditional JDBC Connection private static final String drivername = "Com.mysql.jdbc.Driver"; Private static final String URL = "JDBC:MYSQL://LOCALHOST:3306/20170626JAVAWEB01"; Private static final String user = "root"; Private static final String password = "root"; Public Daodbconecttools () {Connection connect = null; Statement state = null; try {class.forname (drivername); Connect = drivermanager.getconnection (url, user, password); State = Connect.createstatement (); String sql = ""; State.executequery (SQL); } catch (ClassNotFoundException | SQLException e) {e.printstacktrace (); } finally {try {state.close (); Connect.close (); } catch (SQLException e) {e.printstacktrace (); } } }*/ Private Static FinalProperties Pro =NewProperties ();//① Creating a Properties Object//load configuration files and drivers Static{InputStream IStream= Thread.CurrentThread (). Getcontextclassloader (). getResourceAsStream ("config.properties");//② introducing a configuration file Try{pro.load (iStream);//② introducing a configuration fileString drivername = Pro.getproperty ("drivername");//③ Load DriverClass.forName (drivername); } Catch(IOException |classnotfoundexception e) {E.printstacktrace (); } } //get the connection to the image and create the connection Public StaticConnection Connection () {String URL= Pro.getproperty ("url"); String User= Pro.getproperty ("UserName"); String Password= Pro.getproperty ("Password"); Connection Connect=NULL; Try{Connect=drivermanager.getconnection (URL, user, password); } Catch(SQLException e) {e.printstacktrace (); } returnConnect; } //To close a database connection Public Static voidClose (Connection Connect, Statement state) {if(state! =NULL) { Try{state.close (); } Catch(SQLException e) {e.printstacktrace (); } } if(Connect! =NULL) { Try{connect.close (); } Catch(SQLException e) {e.printstacktrace (); } } } }
JDBC from base to optimization