Java Study Notes-JDBC2 (17), study notes jdbc2

Source: Internet
Author: User

Java Study Notes-JDBC2 (17), study notes jdbc2

1. Review: six fixed steps of jdbc * (take query as an example)

// 1. register the driver (which can be connected to the database) DriverManager. registerDriver (new Driver (); // 2. obtain the Connection con = DriverManager object connected to the database. getConnection ("jdbc: mysql: // 127.0.0.1: 3306/mydb", "root", "root"); // 3. get an execution SQL object Statement st = con. createStatement (); // 4. execute SQL statements. take the query as an example. After execution, a ResultSet result set is returned. (It actually contains the queried information) ResultSet rs1_st.exe cuteQuery ("SQL _select statement"); // 5. iterate the content in the result set. while (rs. next () {rs. getObject ("field name");} // 6. resource needs to be released xxx. close ();

2. jdbc CRUD operations

Create a CRUDTest test class

The method used for executing the select statement is executeQuery.

ExecuteUpdate is used to execute insert delete update.

The jdbcUtil class used in the following method is shown in the previous article.

2.1 Query

// @ Test public void select () {Connection con = null; Statement st = null; ResultSet rs = null; try {con = JdbcUtil. getConnection (); st = con. createStatement (); rs = st.exe cuteQuery ("select * from emp"); while (rs. next () {// output the second column System. out. println (rs. getInt (1) ;}} catch (SQLException e) {e. printStackTrace ();} finally {// disable JdbcUtil. close (rs); JdbcUtil. close (st); JdbcUtil. close (con );}}

2.2 add

@ Test public void add () {// add information name = james to the Users table // define the SQL statement to be executed String SQL = "insert into users (id, name) values (2, 'fox') "; // obtain the Connection con = JdbcUtil. getConnection (); Statement st = null; try {st = con. createStatement (); // use executeUpdate int row = st.exe cuteUpdate (SQL); // determine whether the operation is successful if (row = 0) {System. out. println ("add failed");} else {System. out. println ("add succeeded") ;}} catch (SQLException e) {e. printStackTrace ();} finally {JdbcUtil. close (st); JdbcUtil. close (con );}}

2.3 modify

@ Test public void update () {String SQL = "update users set name = 'Tony 'where id = 2"; // obtain Connection con = JdbcUtil. getConnection (); Statement st = null; try {st = con. createStatement (); // use executeUpdate int row = st.exe cuteUpdate (SQL); // determine whether the operation is successful if (row = 0) {System. out. println ("update failed");} else {System. out. println ("update successful") ;}} catch (SQLException e) {e. printStackTrace ();} finally {JdbcUtil. close (st); JdbcUtil. close (con );}}

2.4 Delete

@ Test public void delete () {String SQL = "delete from users where id = 2"; // obtain Connection con = JdbcUtil. getConnection (); Statement st = null; try {st = con. createStatement (); // use executeUpdate int row = st.exe cuteUpdate (SQL); // determine whether the operation is successful if (row = 0) {System. out. println ("failed to delete");} else {System. out. println ("deleted successfully") ;}} catch (SQLException e) {e. printStackTrace ();} finally {JdbcUtil. close (st); JdbcUtil. close (con );}}

 

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.