JDBC Learning Note (5): Basic operational CRUD for databases

Source: Internet
Author: User
Tags stmt

1. Enquiry

 Public Static voidRead ()throwsSQLException {Connection conn=NULL; Statement stmt=NULL; ResultSet RS=NULL; Try{conn=jdbcutils.getconnection (); stmt=conn.createstatement (); RS= Stmt.executequery ("SELECT * from User");  while(Rs.next ()) {System.out.println (Rs.getint ("id") + "\ T" + rs.getstring ("name") + "\ T" + rs.getdate ("birthday") + "\ T" + rs.getfloat ("Money")); }        } finally{jdbcutils.close (RS, stmt, conn); }    }
"Run result": 1 zhangs 1985-01-01 100.02 Lisi 1986-01-01 200.03 Wangwu 1987-01-01 300.0 2. Add or subtract a record, because the ID is Self-increment, so you don't need to insert the data repeatedly:
 Public Static voidCreate ()throwsSQLException {Connection conn=NULL; Statement stmt=NULL; ResultSet RS=NULL; Try{conn=jdbcutils.getconnection (); stmt=conn.createstatement (); String SQL= "INSERT into user (Name,birthday,money) VALUES (' Zhaoliu ', ' 1990-01-01 ', 400)"; inti =stmt.executeupdate (SQL); System.out.println ("I=" +i); } finally{jdbcutils.close (RS, stmt, conn); }    }
"Run result": I=1//Indicates successful insertion of a record "query database Results": one more record 3. Change Id=1 's money to 400
 Public Static voidUpdate ()throwsSQLException {Connection conn=NULL; Statement stmt=NULL; ResultSet RS=NULL; Try{conn=jdbcutils.getconnection (); stmt=conn.createstatement (); String SQL= "Update user set money=400 where id=1"; inti =stmt.executeupdate (SQL); System.out.println ("I =" +i); } finally{jdbcutils.close (RS, stmt, conn); }            }
"Run result": i = 1 "Query Database Results": 4. Delete id=4 Records
 Public Static voidDelete ()throwsSQLException {Connection conn=NULL; Statement stmt=NULL; ResultSet RS=NULL; Try{conn=jdbcutils.getconnection (); stmt=conn.createstatement (); String SQL= "Delete from user where id=4"; inti =stmt.executeupdate (SQL); System.out.println ("i=====" +i); } finally{jdbcutils.close (RS, stmt, conn); }    }
"Run result": i=====1 "Query Database Results":

JDBC Learning Note (5): Basic operational CRUD for databases

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.