Detailed description of the resutset interface operation instance in JDBC, jdbcresuset

Source: Internet
Author: User

Detailed description of the resutset interface operation instance in JDBC, jdbcresuset

This article mainly shows you how to use the resutset interface in the JDBC interface. Let's take a look at the specific content.

1. Details of ResultSet 1

Function: block result set data.

Operation: how to obtain (retrieve) Results

Package com. sjx. a; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. statement; import org. junit. test; // 1. next method, move down and determine whether there is any content // 2. the getXXX method obtains the public class Demo {@ Test public void fun1 () throws Exception {// 1 Registration Driver Class Based on the column index or column name. forName ("com. mysql. jdbc. driver "); // 2 get the Connection conn = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/day05", "root", "1234"); // 3 create Statement st = conn. createStatement (); // 4 Write SQL String SQL = "select * from t_user"; // 5 Execute SQL ResultSet rs = st.exe cuteQuery (SQL); // move one row down, and Judge while (rs. next () {// there is data // get data: getXXX int id = rs. getInt (1); // obtain the value of the first column // int id rs. getInt ("id"); // obtain the value of the id column String name = rs. getString (2); // obtain the value of int age = rs in the second column. getInt (3); // obtain the value of System in the third column. out. println (id + "=>" + name + "=>" + age); // rs. gettimestamp (columnIndex)} // 6 close the resource st. close (); conn. close ();}/* Database Type java type int double decimal double char String varchar String datetime Date timestamp Timestamp/Date */}

2. Details of ResultSet 2

Scroll of result set --> move the pointer of result set to scroll

Reverse modification of database in result set

Package com. sjx. a; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. statement; import org. junit. test; public class Demo2 {@ Test public void fun1 () throws Exception {// 1 register the driver Class. forName ("com. mysql. jdbc. driver "); // 2 get the Connection conn = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/day05", "root", "1234"); // 3 create Statement st = conn. createStatement (); // 4 Write SQL String SQL = "select * from t_user"; // 5 Execute SQL ResultSet rs = st.exe cuteQuery (SQL ); // traverse backwards // 1> move the cursor to the last row and then rs. afterLast (); // 2> traversal => while (rs. previous () {// move the cursor up and determine whether data int id = rs exists. getInt ("id"); // obtain the value of the id column String name = rs. getString ("name"); // obtain the value of int age = rs in the second column. getInt ("age"); // obtain the value of System in the third column. out. println (id + "=>" + name + "=>" + age);} // 6 close the resource st. close (); conn. close ();}/* Database Type java type int double decimal double char String varchar String datetime Date timestamp Timestamp/Date */}

3. Use ResultSet to modify records

Package com. sjx. a; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. statement; import org. junit. test; // ResultSet details // 2. the result set reversely modifies the database public class Demo3 {@ Test public void fun1 () throws Exception {// 1 registers the driver Class. forName ("com. mysql. jdbc. driver "); // 2 get the Connection conn = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/day05", "root", "1234"); // 3 create Statement st = conn. createStatement (ResultSet. TYPE_SCROLL_INSENSITIVE, ResultSet. CONCUR_UPDATABLE); // 4 Write SQL String SQL = "select * from t_user"; // 5 Execute SQL ResultSet rs = st.exe cuteQuery (SQL ); // use the result set to reverse modify the database rs. next (); // move the cursor to the first line rs. updateString ("name", "Tom"); // modify the value of the name column in the first row to Chinese Tom rs. updateRow (); // confirm the modification // 6 close the resource st. close (); conn. close ();}}

Summary

The above is all the details about the resutset interface operation instances in JDBC, and I hope to help you. If you are interested, please referJDBCArticles: BaseJDBC and CRUDDAO examples of writing code, examples of database operation object-oriented model in Spring jdbc, and examples of Java jdbc-based connection to mysql Databases, I also hope you will have more support for the help House website!

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.