JDBC-database update Operation Programming (3)

Source: Internet
Author: User

JDBC-database update Operation Programming (3)

First, create a static method. The Code is as follows:

public static Statement getStatement(){        Statement st = null;        try {            Class.forName("com.mysql.jdbc.Driver");            Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/jsp_dbb", "root", "");            st = (Statement) conn.createStatement();        } catch (Exception e) {            // TODO: handle exception        }        return st;    }

This static class obtains the Statement and uses the Statement class to implement addition, update, and deletion operations, respectively implemented by static code. The Code is as follows:

Public static Statement getStatement () {Statement st = null; try {Class. forName ("com. mysql. jdbc. driver "); Connection conn = (Connection) DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/jsp_dbb", "root", ""); st = (Statement) conn. createStatement ();} catch (Exception e) {// TODO: handle exception} return st;} public static void insert () {try {String SQL = "INSERT INTO tbl_user (name, password, email)" + "VALUES ('Tom ', '123', 'Tom @ qq.com ')"; statement st = getStatement (); int count = st.exe cuteUpdate (SQL); System. out. println ("inserted" + count + "row data");} catch (Exception e) {// TODO: handle exception} public static void update () {try {String SQL = "UPDATE tbl_user SET email = 'Tom @ 136.com 'WHERE name = 'Tom'"; Statement st = getStatement (); int count = st.exe cuteUpdate (SQL); System. out. println ("updated" + count + "row data");} catch (Exception e) {// TODO: handle exception} public static void delete () {try {String SQL = "delete from tbl_user WHERE name = 'Tom '"; Statement st = getStatement (); int count = st.exe cuteUpdate (SQL); System. out. println ("deleted" + count + "row data");} catch (Exception e) {//} public static void main (String [] args) {// insert (); // update (); delete ();}

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.