The following is the table user operation under the DAY10 database.
If the data is changed then use the Executeupdate () method in statement. Use the ExecuteQuery () method if you are querying.
Case:
Package Cn.itheima.jdbc;import Java.sql.connection;import Java.sql.resultset;import java.sql.sqlexception;import Java.sql.statement;import Org.junit.test;import Cn.itheima.utils.jdbcutils;public class JDBCDemo4 {private static Connection con=null;private static Statement sta=null;private static ResultSet rs=null; @Testpublic void Add () {try {con= Jdbcutils.getconnection (); Sta=con.createstatement (); Sta.executeupdate ("INSERT into user values (3, ' Anlu ', 0, ' 1996-02-01 ') "); catch (SQLException e) {e.printstacktrace (); throw new RuntimeException ();} Finally{jdbcutils.closeresource (RS, STA, con);}} @Testpublic void Find () {try {con=jdbcutils.getconnection (); Sta=con.createstatement (); Rs=sta.executequery ("SELECT * From user where name= ' Li Weikang '), while (Rs.next ()) {String name = rs.getstring ("name"); SYSTEM.OUT.PRINTLN (name);}} catch (SQLException e) {e.printstacktrace (); throw new RuntimeException ();} Finally{jdbcutils.closeresource (RS, STA, con);}} @Testpublic void Update () {try {con=jdbcutils.getconnection (); STA=con.createstatement (); Sta.executeupdate ("Update user set gender=0 where name= ' Li Weikang '");} catch (SQLException e) {e.printstacktrace (); throw new RuntimeException ();} Finally{jdbcutils.closeresource (RS, STA, con);}} @Testpublic void Delete () {try {con=jdbcutils.getconnection (); Sta=con.createstatement (); Sta.executeupdate ("Delete From user where name= ' Cheng ');} catch (SQLException e) {e.printstacktrace (); throw new RuntimeException ();} Finally{jdbcutils.closeresource (RS, STA, con);}}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Black Horse day10 Add & delete & update to database MySQL