Summary of book Management system--database operation (i)

Source: Internet
Author: User

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)

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.