JDBC from base to optimization

Source: Internet
Author: User

 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

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.