JDBC Learning Note (4): A single-case toolkit

Source: Internet
Author: User

For ease of operation, connect the database, close the resource operation using the toolkit to complete:

1  PackageCom.xxyh.jdbc;2 Importjava.sql.Connection;3 ImportJava.sql.DriverManager;4 Importjava.sql.SQLException;5  Public classJdbcutilssingleton {6     Private FinalString url = "Jdbc:mysql://localhost:3306/jdbc";7     Private FinalString user = "root";8     Private FinalString password = "1234";9     Ten     Private StaticJdbcutilssingleton instance =NULL; One      A     PrivateJdbcutilssingleton () {} -      -     Static { the         Try { -Class.forName ("Com.mysql.jdbc.Driver"); -}Catch(ClassNotFoundException e) { -             Throw NewExceptionininitializererror (e); +         } -     } +      A     /*Lazy Type*/ at      Public Static synchronizedJdbcutilssingleton getinstance () { -         if(Instance = =NULL)  -Instance =NewJdbcutilssingleton (); -         returninstance; -     } -      in      PublicConnection getconnection ()throwsSQLException { -         returndrivermanager.getconnection (URL, user, password); to     } +}

Lazy-style improvements:

/*Lazy Type*/     Public Static synchronizedJdbcutilssingleton getinstance () {if(Instance = =NULL)             synchronized(Jdbcutilssingleton.class) {                if(Instance = =NULL) Instance=NewJdbcutilssingleton (); }        returninstance; }

Test Kit:

1  Packagecom.xxyh.jdbc.test;2 Importjava.sql.Connection;3 ImportJava.sql.ResultSet;4 Importjava.sql.SQLException;5 Importjava.sql.Statement;6 Importcom.xxyh.jdbc.JdbcUtils;7 ImportCom.xxyh.jdbc.JdbcUtilsSingleton;8  Public classJdbcutilssingletontest {9      Public Static voidMain (string[] args)throwsSQLException {TenConnection conn =NULL; OneStatement stmt =NULL; AResultSet rs =NULL; -          -         Try {  theconn =jdbcutilssingleton.getinstance (). getconnection (); -stmt =conn.createstatement (); -rs = Stmt.executequery ("SELECT * from User"); -              +              while(Rs.next ()) { -System.out.println (Rs.getobject ("id") + "\ T" + rs.getobject ("name") + "\ T" + +Rs.getobject ("Birthday") + "\ T" + rs.getobject ("Money"))); A             } at}finally { - Jdbcutils.close (RS, stmt, conn); -         } -     } -}
"Test Result": 1 zhangs 1985-01-01 100.02 Lisi 1986-01-01 200.03 Wangwu 1987-01-01 300.0

JDBC Learning Note (4): A single-case toolkit

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.