Dark Horse Day10 use Preparestatement to add & delete & Change

Source: Internet
Author: User

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

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.