A simple small test case:
Package Cn.itheima.jdbc;import Java.sql.connection;import Java.sql.date;import java.sql.preparedstatement;import Java.sql.resultset;import Org.junit.test;import Cn.itheima.utils.jdbcutils;public class JDBCDemo6 {Connection con = Null PreparedStatement PS = null; ResultSet rs = null, @Testpublic void Update () {try {con=jdbcutils.getconnection ();p s=con.preparestatement ("Update user Set name= ' Cheng ' where name=? "); Ps.setstring (1, "Li Weikang");p s.executeupdate ();} catch (Exception e) {e.printstacktrace (); throw new RuntimeException ();} finally {Jdbcutils.closeresource (RS, ps, con);}} @Testpublic void Delete () {try {con=jdbcutils.getconnection ();p s=con.preparestatement ("Delete from user where name=?"); Ps.setstring (1, "Cheng");p s.executeupdate ();} catch (Exception e) {e.printstacktrace (); throw new RuntimeException ();} finally {Jdbcutils.closeresource (RS, ps, con);}} @Testpublic void Add () {try {con=jdbcutils.getconnection ();p s=con.preparestatement ("INSERT into user values (2,?,?,?)"); Ps.setstring (1, "Li Weikang");Ps.setbyte (2, (byte) 1);p s.setdate (3, New Date (1992, 3, 4));p s.executeupdate (); catch (Exception e) {e.printstacktrace (); throw new RuntimeException ();} finally {Jdbcutils.closeresource (RS, ps, con);}}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Dark Horse Day10 use Preparestatement to add & delete & Change