Recently this time has been using the SSM framework to do additions and deletions, and suddenly want to use the framework before, integrated Baidu and a variety of materials combined with the API documents copied from the data layer of the package to share to everyone. This side first encapsulates a dbutil.
My package is the kind of rotten street, nothing special.
//dbutil.java
Packagecom.yck.database;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.sql.*;Importjava.util.Properties; Public classdbutil{Private StaticString username; Private StaticString password; Private StaticString URL; Private StaticString driver; Static{getdatabaseinfo ("Jdbc.properties"); Try{class.forname (driver); } Catch(ClassNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } Public Static voidgetdatabaseinfo (String path) {InputStream in= Dbutil.class. getClassLoader (). getResourceAsStream (path); Properties P=NewProperties (); Try{p.load (in); Username= P.getproperty ("Jdbc.username"); Password= P.getproperty ("Jdbc.password"); URL= P.getproperty ("Jdbc.url"); Driver= P.getproperty ("Jdbc.driver"); } Catch(IOException e) {e.printstacktrace (); } } Public StaticConnection getconnection () {Connection conn=NULL; Try{conn=drivermanager.getconnection (Url,username,password); }Catch(SQLException e) {e.printstacktrace (); } returnConn; } Public Static voidcloseconnection (Connection Connection) {Try { if(Connection! =NULL) {connection.close (); Connection=NULL; } } Catch(SQLException e) {e.printstacktrace (); } } Public Staticpreparedstatement preparestatement (Connection connection,string sql) {PreparedStatement PreparedStatement =NULL; Try{PreparedStatement=connection.preparestatement (SQL); } Catch(SQLException e) {e.printstacktrace (); } returnPreparedStatement; } Public Static voidclosepreparedstatement (PreparedStatement preparedstatement) {Try { if(PreparedStatement! =NULL) {preparedstatement.close (); PreparedStatement=NULL; } } Catch(SQLException e) {e.printstacktrace (); } } Public StaticStatement createstatement (Connection Connection) {Statement Statement=NULL; Try{statement=connection.createstatement (); } Catch(SQLException e) {e.printstacktrace (); } returnstatement; } Public Static voidclosestatement (Statement Statement) {Try { if(Statement! =NULL) {statement.close (); Statement=NULL; } } Catch(SQLException e) {e.printstacktrace (); } } Public StaticResultSet getresultset (Statement statement,string sql) {ResultSet ResultSet=NULL; Try{ResultSet=statement.executequery (SQL); } Catch(SQLException e) {e.printstacktrace (); } returnResultSet; } Public Static voidCloseresultset (ResultSet ResultSet) {Try { if(ResultSet! =NULL) {resultset.close (); ResultSet=NULL; } } Catch(SQLException e) {e.printstacktrace (); } }}
Here is the database connection needs jdbc.properties, we are through the Dbutil class read the properties file method to instantiate the Dbutil.
Jdbc.driver=com.mysql.jdbc. driverjdbc. Url=jdbc: MySQL://localhost:3306/test?useunicode=true&characterencoding=utf-8 &usessl=truejdbc. username=rootjdbc. password=8888888
The database wrote a very simple test.
CREATE DATABASEtest; Usetest;CREATE TABLET_user (IDINTAuto_increment not NULL, nameVARCHAR( -), ageINT,PRIMARY KEY(ID)) ENGINE=InnoDBDEFAULTCHARSET=Utf8
Manually inserted two data, the contents of the database are as follows
Let's test it and create a new class
Packagecom.yck.database;ImportJava.sql.*; Public classtest{ Public Static voidMain (string[] args) {Connection conn=dbutil.getconnection (); Statement Stam=dbutil.createstatement (conn); String SQL= "SELECT * FROM T_user"; ResultSet RS=NULL; Try{RS=stam.executequery (SQL); while(Rs.next ()) {System.out.println (rs.getstring ("Name")); } } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } dbutil.closeresultset (RS); Dbutil.closestatement (Stam); Dbutil.closeresultset (RS); }}
The results of the operation are as follows, indicating that our encapsulated database is capable of connecting
Packaging of the Dbutil database connection for the data layer of the hands-on package