In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail how to operate the resutset interface in JDBC. The content of the article is of high quality. Therefore, Xiaobian shares it with you as a reference. I hope that after reading this article, you will have a certain understanding of relevant knowledge.
1. ResultSet Details 1
Function: Block Result Set Data
Operation: How to obtain (extract) 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 if there is content//2. getXXX method, gets the contents of a column based on its index or column name public class Demo { @Test public void fun1() throws Exception{ //1 Register driver Class.forName("com.mysql.jdbc.Driver"); //2 Get connected Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/day05", "root", "1234"); //3 Creating a Statement Statement st = conn.createStatement(); //4 Write sql String sql = "select * from t_user" ; //5 Execute sql ResultSet rs = st.executeQuery(sql); //Move down one line and judge while(rs.next()){ //Data available //Get data:getXXX int id = rs.getInt(1);//Get the first column value //int id rs.getInt("id");//Get the value of the id column String name = rs.getString(2);//Get the value of the second column int age = rs.getInt(3);//Get the value of the third column System.out.println(id+"==>"+name+"==>"+age); //rs.gettimestamp(columnIndex) } //6 Close Resources st.close(); conn.close(); } /* Database type Java types int int double double decimal double char String varchar String datetime Date timestamp Timestamp/Date */}
2. ResultSet Details 2
Scrolling of the result set--> Moving the pointer of the result set is scrolling
Result Set Reverse Modify Database
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 driver Class.forName("com.mysql.jdbc.Driver"); //2 Get connected Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/day05", "root", "1234"); //3 Creating a Statement Statement st = conn.createStatement(); //4 Write sql String sql = "select * from t_user" ; //5 Execute sql ResultSet rs = st.executeQuery(sql); //Back to back //1> Move the cursor to the last line rs.afterLast(); //2> Traverse => while(rs.previous()){//Move the cursor up and determine if there is data int id = rs.getInt("id");//Get the value of the id column String name = rs.getString("name");//get the value of the second column int age = rs.getInt("age");//Get the value of the third column System.out.println(id+"==>"+name+"==>"+age); } //6 Close Resources st.close(); conn.close(); } /* Database type Java types int int double double decimal double char String varchar String datetime Date timestamp Timestamp/Date */}
3. Modify records using ResultSet
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. ResultSet Reverse Modify Database public class Demo3 { @Test public void fun1() throws Exception{ //1 Register driver Class.forName("com.mysql.jdbc.Driver"); //2 Get connected Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/day05", "root", "1234"); //3 Creating a Statement 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.executeQuery(sql); //modify database reversely using result set 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 modification //6 Close Resources st.close(); conn.close(); }} About how to operate the resutset interface in JDBC to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.