Java ResultSet案例講解
ResultSet
ResultSet是我們使用jdbc連接時,查詢的一個返回結(jié)果集,ResultSet resultSet = stmt.executeQuery(sql),下面就使用例子介紹ResultSet的使用
例子是通過jdbc連接查account表中的數(shù)據(jù),然后用實體類Account封裝起來,返回這個類的集合。
jdbc工具類代碼
package com.lingaolu.Utils; import java.io.FileReader; import java.io.IOException; import java.net.URL; import java.sql.*; import java.util.Properties; /** * @author 林高祿 * @create 2020-06-23-11:12 */ 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/lingaolu/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); } }
Account實體類代碼
package com.lingaolu.jdbcConnector; /** * @author 林高祿 * @create 2020-06-24-8:28 */ public class Account { private int id; private String name; private double balance; private int myAge; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public int getMyAge() { return myAge; } public void setMyAge(int myAge) { this.myAge = myAge; } @Override public String toString() { return "Account{" + "id=" + id + ", name='" + name + '\'' + ", balance=" + balance + ", myAge=" + myAge + '}'; } }
測試Demo3的代碼
package com.lingaolu.jdbcConnector; import com.lingaolu.Utils.JdbcUtils; import java.sql.*; import java.util.ArrayList; import java.util.List; /** * @author 林高祿 * @create 2020-06-23-17:27 */ public class Demo3 { public static void main(String[] args) { String sql = "select * from account"; List<Account> 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<Account> fineAccount(String sql){ Connection con = null; Statement stmt = null; ResultSet resultSet = null; List<Account> 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; } }
表中的數(shù)據(jù)
運行輸出:
Account{id=1, name='張三', balance=500.0, myAge=17}
Account{id=2, name='李四', balance=1000.0, myAge=16}
Account{id=7, name='張三', balance=600.0, myAge=19}
Account{id=11, name='林帥', balance=20000.0, myAge=18}
----------------------------------
Account{id=1, name='張三', balance=500.0, myAge=17}
Account{id=7, name='張三', balance=600.0, myAge=19}
結(jié)果與預(yù)期的一致
到此這篇關(guān)于Java ResultSet案例講解的文章就介紹到這了,更多相關(guān)Java ResultSet講解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
idea2023設(shè)置啟動參數(shù)、單元測試啟動參數(shù)
在使用IDEA進行開發(fā)時,我們可以通過設(shè)置一些啟動參數(shù)來優(yōu)化開發(fā)環(huán)境的性能和體驗,具有一定的參考價值,感興趣的可以了解一下2023-11-11Java并發(fā)編程之CountDownLatch源碼解析
這篇文章主要介紹了Java并發(fā)編程之CountDownLatch源碼解析,文中有非常詳細的代碼示例,對正在學(xué)習(xí)java并發(fā)編程的小伙伴們有很好的幫助,需要的朋友可以參考下2021-04-04springboot?全局異常處理和統(tǒng)一響應(yīng)對象的處理方式
這篇文章主要介紹了springboot?全局異常處理和統(tǒng)一響應(yīng)對象,主要包括SpringBoot 默認的異常處理機制和SpringBoot 全局異常處理,本文給大家介紹的非常詳細,需要的朋友可以參考下2022-06-06SpringBoot系列教程JPA之基礎(chǔ)環(huán)境搭建的方法
這篇文章主要介紹了SpringBoot系列教程JPA之基礎(chǔ)環(huán)境搭建的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06