Java Processing Database crud

Source: Internet
Author: User
Tags stmt

 PackageCom.lhy.jdbc.util;Importjava.sql.Connection;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement;/*** * Delete and change *@authorhy **/ Public classCRUD { Public Static voidMain (string[] args) {//Create (); //read (); //update ();Delete (); }    /*** Enquiry*/    Static voidRead () {Connection conn=NULL; Statement stmt=NULL; ResultSet RS=NULL; Try {            //Establish a connection, Jdbcutil tool class please see my other blogconn =jdbcutil.getconnection (); //Create statementstmt =conn.createstatement (); /*** Execute the statement, generally do not recommend directly write SELECT *, readability is not good. */RS= Stmt.executequery ("SELECT * from User"); //Processing Results             while(Rs.next ()) {System.out.println (rs.getstring ("username") + "\ T" + rs.getstring ("password"))); }        } Catch(SQLException e) {e.printstacktrace (); } finally{jdbcutil.close (RS);            Jdbcutil.close (stmt);        Jdbcutil.close (conn); }    }    /*** INSERT INTO inserts a record*/    Static voidCreate () {Connection conn=NULL; Statement stmt=NULL; Try {            //Establish a connectionconn =jdbcutil.getconnection (); //Create statementstmt =conn.createstatement (); String SQL= "INSERT into user values (' Hello ', ' 147 ', 1000)"; //executes the statement, the return value is an int several lines are insertedstmt.executeupdate (SQL); //int i = stmt.executeupdate (sql); //System.out.println ("i=" +i);        } Catch(SQLException e) {e.printstacktrace (); } finally{jdbcutil.close (stmt);        Jdbcutil.close (conn); }    }                    /*** Update*/        Static voidUpdate () {Connection conn=NULL; Statement stmt=NULL; Try {            //Establish a connectionconn =jdbcutil.getconnection (); //Create statementstmt =conn.createstatement (); //EXECUTE StatementString sql = "Update user Set money = money + 100"; //    inti =stmt.executeupdate (SQL); System.out.println ("I=" +i); } Catch(SQLException e) {e.printstacktrace (); } finally{jdbcutil.close (stmt);        Jdbcutil.close (conn); }    }        Static voidDelete () {Connection conn=NULL; Statement stmt=NULL; Try {            //Establish a connectionconn =jdbcutil.getconnection (); //Create statementstmt =conn.createstatement (); //EXECUTE StatementString sql = "Delete from user where Money <600"; inti =stmt.executeupdate (SQL); System.out.println ("I=" +i); } Catch(SQLException e) {e.printstacktrace (); } finally{jdbcutil.close (stmt);        Jdbcutil.close (conn); }    }}

Java Processing Database crud

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.