Working with the MySQL database in the Java language in MyEclipse

Source: Internet
Author: User

Package Operatemysql;import java.sql.*; Public classMysqltest { Public Static voidMain (string[] args) {//TODO auto-generated Method StubBasedao Basedao =NewBasedao (); Connection Conn=basedao.getconnection ();        BASEDAO.ADD (conn);        BASEDAO.DELETE (conn);        Basedao.update (conn);        Basedao.query (conn);    Basedao.close (); }}classBasedao {Private StaticString URL ="jdbc:mysql://localhost:3306/mysqltest"; Private StaticString user ="Root"; Private StaticString Password ="123456"; PrivateConnection Conn; Private StaticStatement SM; Private StaticResultSet rs; Private StaticString SQL; //Connect Database Functions     PublicConnection getconnection () {Try {            //initializing the driver packageClass.forName ("Com.mysql.jdbc.Driver"); //according to the database connection character, name, password to connSystem. out. println ("start trying to connect to the database! "); Conn=drivermanager.getconnection (URL, user, password); } Catch(Exception e) {e.printstacktrace (); }        returnConn; }    //Query Function     Public voidquery (Connection conn) {SQL="SELECT * from EMP"; Try{SM=conn.createstatement (); RS=sm.executequery (SQL);  while(Rs.next ()) {System. out. println ("ID:"+ rs.getstring (1) +"\tname:"+ rs.getstring (2) +"\tage:"+ rs.getstring (3)); }        } Catch(Exception e) {e.printstacktrace (); }    }    //Add Table Data     Public voidAdd (Connection conn) {SQL="INSERT INTO EMP (id,name,age)"+"values (' 0004 ', ' lucyyyy ', ' + ')"; Try{SM=conn.createstatement ();            Sm.executeupdate (SQL); System. out. println ("Add Success"); } Catch(Exception e) {e.printstacktrace (); }    }    //Delete Data     Public voidDelete (Connection conn) {SQL="Delete from EMP"+"where id= ' 6 '"; Try{SM=conn.createstatement ();            Sm.executeupdate (SQL); System. out. println ("Delete succeeded"); } Catch(Exception e) {e.printstacktrace (); }    }    //Modifying Data     Public voidUpdate (Connection conn) {SQL="Update EMP set age= ' where id= ' 2 '"; Try{SM=conn.createstatement ();            Sm.executeupdate (SQL); System. out. println ("Update Successful"); } Catch(Exception e) {e.printstacktrace (); }    }     Public voidClose () {//6. Releasing Resources        Try{//Catching exceptions            Try {                if(rs! =NULL) {//when an instance of the ResultSet object RS is not emptyRs.close ();//close the ResultSet object                }            } finally {                Try {                    if(SM! =NULL) {//When an instance of statement object SM is not emptySm.close ();//Close the Statement object                    }                } finally {                    if(Conn! =NULL) {//When an instance of the connection object conn is not emptyConn.close ();//Close the Connection object                    }                }            }        } Catch(Exception e) {e.printstacktrace (system.err);//Output Exception Information        }    }}

Working with the MySQL database in the Java language in MyEclipse

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.