Because often in the evening back to the dorm to write code, no net, so no teacher provided the Oracle database server, installed on their own computer a MySQL. And Java with MySQL is very convenient.
Database connection:
Referring to the http://blog.csdn.net/superit401/article/details/51585620, the talk is very comprehensive.
Here the database connection is dedicated as a tool class, connected to its own MYQL database. Because like login, top-up such key transactions need to pay attention to ensure consistency, so the display specifies the transaction, and add row-level locks, so there are some database transactions related to the operations are encapsulated in this class.
/*** Database Tools class *@authorAdministrator **/ Public classDbutil {PrivateString dburl= "Jdbc:mysql://localhost:3306/db_book";//Database connection Address://202.38.88.99:1434/student PrivateString dbusername= "root";//User name Student PrivateString dbpassword= "Student";//Password PrivateString jdbcname= "Com.mysql.jdbc.Driver";//driver name /*** Get database connection *@return * @throwsException*/ PublicConnection Getcon ()throwsexception{Class.forName (jdbcname); Connection Con=drivermanager.getconnection (Dburl, Dbusername, Dbpassword); returncon; } /*** Close Database connection *@paramCon *@throwsException*/ Public voidClosecon (Connection con)throwsexception{if(con!=NULL) {con.close (); } } /*** Close Connection Table Object *@parampstmt*/ Public Static voidClose (java.sql.PreparedStatement pstmt) {if(pstmt!=NULL) { Try{pstmt.close (); } Catch(SQLException e) {e.printstacktrace (); } } } /*** Close result set, release resources *@paramRS*/ Public Static voidClose (ResultSet rs) {if(rs!=NULL){ Try{rs.close (); } Catch(SQLException e) {e.printstacktrace (); } } } /*** Turn off Auto-commit transactions *@paramConn*/ Public Static voidBeginTransaction (Connection conn) {if(conn!=NULL) { Try { if(Conn.getautocommit ()) {Conn.setautocommit (false); } } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } } /*** Submit a transaction *@paramConn*/ Public Static voidCommitTransaction (Connection conn) {Try { if(conn!=NULL){ if(!Conn.getautocommit ()) {Conn.commit (); } } } Catch(Exception e) {//Todo:handle Exception } } /*** ROLLBACK TRANSACTION *@paramConn*/ Public Static voidRollbackTransaction (Connection conn) {Try { if(conn!=NULL){ if(!Conn.getautocommit ()) {Conn.rollback (); } } } Catch(Exception e) {//Todo:handle Exception } } /*** Whether to automatically commit transaction permissions to reverse *@paramConn*/ Public Static voidresettransaction (Connection conn) {Try { if(conn!=NULL) { if(Conn.getautocommit ()) {Conn.setautocommit (false); }Else{conn.setautocommit (true); } } } Catch(Exception e) {//Todo:handle Exception } } /*** Main function, test using *@paramargs*/ Public Static voidMain (string[] args) {dbutil dbutil=NewDbutil (); Try{Dbutil.getcon (); System.out.println ("The database connection was successful!" "); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); System.out.println ("Database connection Failed"); } } }
Summary of book Management system--database operation (i)