Black Horse day10 Add & delete & update to database MySQL

Source: Internet
Author: User

The following is the table user operation under the DAY10 database.

If the data is changed then use the Executeupdate () method in statement. Use the ExecuteQuery () method if you are querying.

Case:

Package Cn.itheima.jdbc;import Java.sql.connection;import Java.sql.resultset;import java.sql.sqlexception;import Java.sql.statement;import Org.junit.test;import Cn.itheima.utils.jdbcutils;public class JDBCDemo4 {private static Connection con=null;private static Statement sta=null;private static ResultSet rs=null; @Testpublic void Add () {try {con= Jdbcutils.getconnection (); Sta=con.createstatement (); Sta.executeupdate ("INSERT into user values (3, ' Anlu ', 0, ' 1996-02-01 ') "); catch (SQLException e) {e.printstacktrace (); throw new RuntimeException ();} Finally{jdbcutils.closeresource (RS, STA, con);}} @Testpublic void Find () {try {con=jdbcutils.getconnection (); Sta=con.createstatement (); Rs=sta.executequery ("SELECT * From user where name= ' Li Weikang '), while (Rs.next ()) {String name = rs.getstring ("name"); SYSTEM.OUT.PRINTLN (name);}} catch (SQLException e) {e.printstacktrace (); throw new RuntimeException ();} Finally{jdbcutils.closeresource (RS, STA, con);}} @Testpublic void Update () {try {con=jdbcutils.getconnection (); STA=con.createstatement (); Sta.executeupdate ("Update user set gender=0 where name= ' Li Weikang '");} catch (SQLException e) {e.printstacktrace (); throw new RuntimeException ();} Finally{jdbcutils.closeresource (RS, STA, con);}} @Testpublic void Delete () {try {con=jdbcutils.getconnection (); Sta=con.createstatement (); Sta.executeupdate ("Delete From user where name= ' Cheng ');} catch (SQLException e) {e.printstacktrace (); throw new RuntimeException ();} Finally{jdbcutils.closeresource (RS, STA, con);}}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Black Horse day10 Add & delete & update to database MySQL

Related Article

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.