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 );}}