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