This article mainly shows youJDBC interfaceExamples of usage of the resutset interface, let’s take a look at the specific content below.
1. ResultSet Detail 1
Function: Block the result set data
Operation: How to obtain (take out) results
package ; import ; import ; import ; import ; import ; //1. next method, move down and determine whether there is any content //2. getXXX method, obtain the content of the column based on the column index or column namepublic class Demo { @Test public void fun1() throws Exception{ //1 Register driver (""); //2 Get the connection Connection conn = ("jdbc:mysql://localhost:3306/day05", "root", "1234"); //3 Create Statement Statement st = (); //4 Writing sql String sql = "select * from t_user" ; //5 Execute sql ResultSet rs = (sql); //Move one line down and judge while(()){ //There is data //Get data: getXXX int id = (1);//Get the value of the first column //int id ("id");// Get the value of the id column String name = (2);//Get the value of the second column int age = (3);//Get the value of the third column (id+"==>"+name+"==>"+age); //(columnIndex) } //6 Close the resource (); (); } /* Database type java type int int double double decimal double char String varchar String datetime Date timestamp Timestamp/Date */ }
Detail 2
Scroll of the result set --> Move the pointer of the result set is scrolling
Reversely modify the database
package ; import ; import ; import ; import ; import ; public class Demo2 { @Test public void fun1() throws Exception{ //1 Register driver (""); //2 Get the connection Connection conn = ("jdbc:mysql://localhost:3306/day05", "root", "1234"); //3 Create Statement Statement st = (); //4 Writing sql String sql = "select * from t_user" ; //5 Execute sql ResultSet rs = (sql); //Travel backwards //1> After the cursor moves to the last line (); //2> traversal=> while(()){//Move the cursor upward and determine whether there is data int id = ("id");// Get the value of the id column String name = ("name");//Get the value of the second column int age = ("age");//Get the value of the third column (id+"==>"+name+"==>"+age); } //6 Close the resource (); (); } /* Database type java type int int double double decimal double char String varchar String datetime Date timestamp Timestamp/Date */ }
3. Use ResultSet to modify records
package ; import ; import ; import ; import ; import ; //ResultSet details// 2. Reversely modify the databasepublic class Demo3 { @Test public void fun1() throws Exception{ //1 Register driver (""); //2 Get the connection Connection conn = ("jdbc:mysql://localhost:3306/day05", "root", "1234"); //3 Create Statement Statement st = (ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE); //4 Writing sql String sql = "select * from t_user" ; //5 Execute sql ResultSet rs = (sql); //Use the result set to reversely modify the database ();//Move the cursor to the first line ("name", "Tom");// Modify the value of the first row name column to Chinese Tom ();// Confirm the modification //6 Close the resource (); (); } }
Summarize
The above is all the contents of this article about the operation example of the resutset interface in JDBC. I hope it will be helpful to everyone. Friends who are interested are welcome to refer to more informationJDBCArticles:BaseJDBC and CRUDDAO writing example code、Detailed explanation of the instance of the database operation objectified model in Spring jdbc、Java connection mysql database operation example based on jdbcWait, I hope everyone will support my website!