1. Enquiry
Public Static voidRead ()throwsSQLException {Connection conn=NULL; Statement stmt=NULL; ResultSet RS=NULL; Try{conn=jdbcutils.getconnection (); stmt=conn.createstatement (); RS= Stmt.executequery ("SELECT * from User"); while(Rs.next ()) {System.out.println (Rs.getint ("id") + "\ T" + rs.getstring ("name") + "\ T" + rs.getdate ("birthday") + "\ T" + rs.getfloat ("Money")); } } finally{jdbcutils.close (RS, stmt, conn); } }
"Run result": 1 zhangs 1985-01-01 100.02 Lisi 1986-01-01 200.03 Wangwu 1987-01-01 300.0 2. Add or subtract a record, because the ID is Self-increment, so you don't need to insert the data repeatedly:
Public Static voidCreate ()throwsSQLException {Connection conn=NULL; Statement stmt=NULL; ResultSet RS=NULL; Try{conn=jdbcutils.getconnection (); stmt=conn.createstatement (); String SQL= "INSERT into user (Name,birthday,money) VALUES (' Zhaoliu ', ' 1990-01-01 ', 400)"; inti =stmt.executeupdate (SQL); System.out.println ("I=" +i); } finally{jdbcutils.close (RS, stmt, conn); } }
"Run result": I=1//Indicates successful insertion of a record "query database Results": one more record 3. Change Id=1 's money to 400
Public Static voidUpdate ()throwsSQLException {Connection conn=NULL; Statement stmt=NULL; ResultSet RS=NULL; Try{conn=jdbcutils.getconnection (); stmt=conn.createstatement (); String SQL= "Update user set money=400 where id=1"; inti =stmt.executeupdate (SQL); System.out.println ("I =" +i); } finally{jdbcutils.close (RS, stmt, conn); } }
"Run result": i = 1 "Query Database Results": 4. Delete id=4 Records
Public Static voidDelete ()throwsSQLException {Connection conn=NULL; Statement stmt=NULL; ResultSet RS=NULL; Try{conn=jdbcutils.getconnection (); stmt=conn.createstatement (); String SQL= "Delete from user where id=4"; inti =stmt.executeupdate (SQL); System.out.println ("i=====" +i); } finally{jdbcutils.close (RS, stmt, conn); } }
"Run result": i=====1 "Query Database Results":
JDBC Learning Note (5): Basic operational CRUD for databases