Importjava.sql.Connection;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importorg.junit.Test;Importutil. Jdbcutil;/*** PreparedStatement Execute SQL sentence *@authorAPPle **/ Public classDemo1 {/*** Increase*/@Test Public voidTestinsert () {Connection conn=NULL; PreparedStatement stmt=NULL; Try { //1. Get the connectionconn =jdbcutil.getconnection (); //2. Prepare the precompiled SQLString sql = "INSERT into Student2 (name,gender) VALUES (?,?)";//? Represents a placeholder for a parameter//3. Executing precompiled SQL statements (check syntax)stmt =conn.preparestatement (SQL); //4. Setting parameter values /*** Parameter one: parameter position starting from 1*/stmt.setstring (1, "John Doe"); Stmt.setstring (2, "Male"); //5. Send parameters, execute SQL intCount =stmt.executeupdate (); System.out.println ("Affected" +count+ "line"); } Catch(Exception e) {e.printstacktrace (); Throw NewRuntimeException (e); } finally{jdbcutil.close (conn, stmt); } } /*** Modify*/@Test Public voidtestupdate () {Connection conn=NULL; PreparedStatement stmt=NULL; Try { //1. Get the connectionconn =jdbcutil.getconnection (); //2. Prepare the precompiled SQLString sql = "UPDATE student2 SET name=?" WHERE id=? ";//? Represents a placeholder for a parameter//3. Executing precompiled SQL statements (check syntax)stmt =conn.preparestatement (SQL); //4. Setting parameter values /*** Parameter one: parameter position starting from 1*/stmt.setstring (1, "Harry"); Stmt.setint (2, 9); //5. Send parameters, execute SQL intCount =stmt.executeupdate (); System.out.println ("Affected" +count+ "line"); } Catch(Exception e) {e.printstacktrace (); Throw NewRuntimeException (e); } finally{jdbcutil.close (conn, stmt); } } /*** Delete*/@Test Public voidTestdelete () {Connection conn=NULL; PreparedStatement stmt=NULL; Try { //1. Get the connectionconn =jdbcutil.getconnection (); //2. Prepare the precompiled SQLString sql = "DELETE from Student2 WHERE id=?";//? Represents a placeholder for a parameter//3. Executing precompiled SQL statements (check syntax)stmt =conn.preparestatement (SQL); //4. Setting parameter values /*** Parameter one: parameter position starting from 1*/Stmt.setint (1, 9); //5. Send parameters, execute SQL intCount =stmt.executeupdate (); System.out.println ("Affected" +count+ "line"); } Catch(Exception e) {e.printstacktrace (); Throw NewRuntimeException (e); } finally{jdbcutil.close (conn, stmt); } } /*** Enquiry*/@Test Public voidTestquery () {Connection conn=NULL; PreparedStatement stmt=NULL; ResultSet RS=NULL; Try { //1. Get the connectionconn =jdbcutil.getconnection (); //2. Prepare the precompiled SQLString sql = "SELECT * FROM Student2"; //3. Pre-compilationstmt =conn.preparestatement (SQL); //4. Execute SQLrs =Stmt.executequery (); //5. Traverse RS while(Rs.next ()) {intid = rs.getint ("id"); String name= rs.getstring ("name"); String Gender= Rs.getstring ("Gender"); System.out.println (ID+ "," +name+ "," +gender); } } Catch(Exception e) {e.printstacktrace (); Throw NewRuntimeException (e); } finally { //Close ResourceJdbcutil.close (CONN,STMT,RS); } }}
PreparedStatement Execute SQL sentence