PreparedStatement Execute SQL sentence

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.