java,resultset
<link rel="stylesheet" href="https://js.how234.com/559000b9bd/4c9a02a4bec20fc1fc2f0e60a2782b5ffb/4c9715bcbac9/4c8b2fbfaddf.css" type="text/css" /><link rel="stylesheet" href="https://js.how234.com/559000b9bd/4c9a02a4bec20fc1fc2f0e60a2782b5ffb/4c9715bcbac9/4c8b38b8bad702ecfe21037ca964.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><style>pre{overflow-x: auto}</style>
<link rel="stylesheet" href="https://js.how234.com/third-party/SyntaxHighlighter/shCoreDefault.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><script type="text/javascript"> SyntaxHighlighter.all(); </script>
java resultset是什麼?讓我們一起來了解一下吧!
java resultset是我們在運用jdbc進行對接的時候,查詢出的一個返回結果集合。Resultset的功能就是完成了存儲查詢結果,但是它只能讀取一次,不能做到滾動讀取。
ResultSetMetaData:
我們可以應用 ResultSet.getMetaData() 方法來得到 ResultSetMetaData。通過該信息能夠得到表的結構,比如說列名,列的個數,列數據類型等。
一.獲取列名
ResultSetMetaData.getColumnName(m);
獲取第m位的列名
二.獲取列個數
ResultSetMetaData.getColumnCount();
獲取列的個數
三.獲得列類型
1.ResultSetMetaData.getColumnType(m);
獲取第m位的列類型,對應java.sql.Types中的數據信息
2.ResultSetMetaData.getColumnTypeName(m);
獲取第m位的列類型名稱
實戰演練,具體步驟如下:
package com.lingaolu.Utils; import java.io.FileReader;import java.io.IOException;import java.net.URL;import java.sql.*;import java.util.Properties; public class JdbcUtils { private static String driver; private static String url; private static String userName; private static String pw; static{ try { Properties p = new Properties(); ClassLoader classLoader = JdbcUtils.class.getClassLoader(); // 這個路徑相對於src的路徑來說 URL resource = classLoader.getResource("com/file/jdbc.properties"); String path = resource.getPath(); p.load(new FileReader(path)); driver = p.getProperty("driver"); url = p.getProperty("url"); userName = p.getProperty("user"); pw = p.getProperty("password"); Class.forName(driver); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection createConnection() throws SQLException { return DriverManager.getConnection(url, userName, pw); } public static void close(Statement stmt,Connection con){ if(null != stmt){ try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if(null != con){ try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(ResultSet set,Statement s,Connection con){ if(null != set){ try { set.close(); } catch (SQLException e) { e.printStackTrace(); } } close(s,con); }}package com.lingaolu.jdbcConnector; import com.lingaolu.Utils.JdbcUtils; import java.sql.*;import java.util.ArrayList;import java.util.List; public class Demo3 { public static void main(String[] args) { String sql = "select * from account"; List accounts = fineAccount(sql); accounts.forEach(System.out::println); System.out.println("----------------------------------"); sql = "select * from account where name='張三'"; accounts = fineAccount(sql); accounts.forEach(System.out::println); } public static List fineAccount(String sql){ Connection con = null; Statement stmt = null; ResultSet resultSet = null; List rerurnList = new ArrayList<>(); try { con = JdbcUtils.createConnection(); stmt = con.createStatement(); resultSet = stmt.executeQuery(sql); Account acc = null; while(resultSet.next()){ // 引號裏的字段要與表裏的一樣 int id = resultSet.getInt("id"); String name = resultSet.getString("name"); double balance = resultSet.getDouble("balance"); int age = resultSet.getInt("age"); acc = new Account(); acc.setId(id); acc.setName(name); acc.setBalance(balance); acc.setMyAge(age); rerurnList.add(acc); } } catch (SQLException e) { e.printStackTrace(); }finally { JdbcUtils.close(resultSet,stmt,con); } return rerurnList; }}
-
微信怎麼建
1、微信的創建方法:打開微信。點擊更多。選擇註冊。輸入暱稱,手機號和密碼。勾選同意協議。點擊註冊即可。2、微信推薦使用手機號註冊,並支持100餘個國家的手機號。微信不可以通過QQ號直接登錄註冊或者通過郵箱帳號註冊。第一次使用QQ號登陸時,是登陸不了的,只能用...
-
藍牙耳機連接不上手機怎麼辦
1、首先,確保藍牙耳機與手機都有電且處於開機狀態,手機藍牙功能處於打開狀態。2、藍牙耳機進入配對模式:在藍牙耳機上會有一個多功能的按鍵,這個多功能的按鍵有一個作用是進行設備與藍牙耳機之間的匹配,在與手機連接時,需要長按這個功能鍵,同時需要打開手機的藍牙功能...
-
蘋果相機前置攝像頭自拍是真實的自己嗎
用蘋果前置攝像頭拍出來的照片不是真實的自己,這是因爲蘋果手機的前置攝像頭的像素非常高,拍出來的照片就像照鏡子,人臉上的各種小斑點以及小痘印和細小的皺紋都看得清清楚楚,但也只是感覺像自己而已。但是也有一部份人認爲不像真實的自己,好像五官都沒有立體感了,當...
-
微信怎麼建,微信的創建方法
1、微信的創建方法:打開微信。點擊更多。選擇註冊。輸入暱稱,手機號和密碼。勾選同意協議。點擊註冊即可。2、微信推薦使用手機號註冊,並支持100餘個國家的手機號。微信不可以通過QQ號直接登錄註冊或者通過郵箱帳號註冊。第一次使用QQ號登陸時,是登陸不了的,只能用...