Package com. hanchao. jdbc; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. preparedStatement; import java. SQL. resultSet;/*** jdbc learning Summary 2 * @ author hanlw * 2012-07-09 */public class TestJdbcNew {/*** in the previous article, we had a preliminary understanding of JDBC, and know how to use JDBC. * However, it is only a JDBC-aware operation instance. In actual development, we cannot do that !! **★Let's take a look at the usage of the placeholder for JDBC. We recommend that you do it in the future !! ** Note: Our exceptions should be caught instead of thrown out !! ** SQLException is a non-runtime exception and must be captured or thrown up !!★*/Public static void main (String [] args) throws Exception {/*** 1. jdbc insert operations on Mysql * // insert ("cherry", "shanghai");/*** 2. jdbc update operation on mysql * // update ("update", 12);/*** 3. jdbc's delete operation on mysql * // delete (12);/*** 4. jdbc retrieve operation on mysql */retrieve (15 );} /*** jdbc insert operation on mysql ** @ param username * @ param address */public static void insert (String username, String address) throws Exception {Class. forName ("com. mysql. jdbc. driver "); Connection con = DriverManager. getConnection ("jdbc: mysql: // 127.0.0.1: 3306/mydb", "root", "root"); // pay attention to the following lines★String SQL = "insert into t_user (username, address) values (?,?) ";//★PreparedStatement sta = con. prepareStatement (SQL); sta. setString (1, username); sta. setString (2, address); int rows = sta.exe cuteUpdate (); if (rows> 0) {System. out. println ("operate successfully! ");} Sta. close (); con. close ();} /*** jdbc update operation on mysql ** @ param address * @ param id primary key value * @ throws Exception */public static void update (String address, int id) throws Exception {Class. forName ("com. mysql. jdbc. driver "); Connection con = DriverManager. getConnection ("jdbc: mysql: // 127.0.0.1: 3306/mydb", "root", "root ");//★Note the following lines of code: String SQL = "update t_user set address =? Where id =? "; PreparedStatement sta = con. prepareStatement (SQL); sta. setString (1, address); sta. setInt (2, id); int rows = sta.exe cuteUpdate (); if (rows> 0) {System. out. println ("operate successfully");} sta. close (); con. close ();}/*** jdbc delete mysql operation ** @ param id * @ throws Exception */public static void delete (int id) throws Exception {Class. forName ("com. mysql. jdbc. driver "); Connection con = DriverManager. getConnection ("jdbc: mysql: // 127.0.0.1: 3306/mydb", "root", "root ");//★Note: String SQL = "delete from t_user where id =? "; PreparedStatement sta = con. prepareStatement (SQL); sta. setInt (1, id); int rows = sta.exe cuteUpdate (); if (rows> 0) {System. out. println ("operate successfully !! ");} Sta. close (); con. close ();}/*** jdbc retrieve operation on mysql ** @ param id * @ throws Exception */public static void retrieve (int id) throws Exception {Class. forName ("com. mysql. jdbc. driver "); Connection con = DriverManager. getConnection ("jdbc: mysql: // 127.0.0.1: 3306/mydb", "root", "root"); // note★String SQL = "select id, username, address from t_user where id =? "; PreparedStatement sta = con. prepareStatement (SQL); sta. setInt (1, id); ResultSet rs = sta.exe cuteQuery (); // Note: When a record is generated, the value of while can be changed to if.★If (rs. next () {int did = rs. getInt ("id"); String username = rs. getString ("username"); String address = rs. getString ("address"); System. out. println (did + "\ t" + username + "\ t" + address);} rs. close (); sta. close (); con. close ();}}