Java-JDBC-ResultSet及查詢記錄對象(JavaBean)封裝
目錄
內容
1、ResultSet
ResultSet結果集對象,用於封裝查詢結果。
-
boolean next():遊標向下移動一行,判斷當前行是否是最後一行(末尾,是否還有數據),有則返回true;否則返回false;
-
getXxx(參數):獲取數據
- Xxx:代碼數據類型
- 參數:
- int :代表列的編號,從1開始
- String:代碼列的名稱
-
使用步驟
- 遊標向下移動一行
- 判斷是否有數據
- 獲取數據
-
示例:遍歷表account並打印
-
代碼1-1:
package cn.gaogzhen.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JDBCDemo4 { public static void main(String[] args) { Connection conn = null; Statement statement = null; ResultSet ret = null; // 2. 註冊驅動 try { Class.forName("com.mysql.cj.jdbc.Driver"); // 3. 獲取連接對象Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/db2?serverTimezone=Asia/Shanghai", "root", "root"); // 4. sql語句 String sql = "select * from account"; // 5. 獲取執行sql語句的對象statement statement = conn.createStatement(); // 6. 執行sql操作,獲取結果 ret = statement.executeQuery(sql); // 7. 解析結果 System.out.println("ID\t姓名\t餘額"); while(ret.next()) { int id = ret.getInt("id"); String name = ret.getString("name"); double balance = ret.getDouble("balance"); System.out.println(id + "\t" + name + "\t" + balance); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { // 8. 釋放資源 if (ret != null) { try { ret.close(); } catch (SQLException e) { e.printStackTrace(); } } if (statement != null) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
測試結果:
ID 姓名 餘額
1 張三 500.0
2 李四 1000.0
2、JavaBean
JavaBean 爲Java的標準類,即屬性爲私有,並且提供相應的get和set方法的類。
3、表 -> 對象集合
- 表名:對應類名
- 一條記錄:對應一個類對象
- 列名:對應屬性名
- 列類型:對應屬性類型
- 數據集:對應集合
4、查詢表emp數據將其封裝爲對象,並裝入集合返回
-
表emp圖示4-1:
-
JavaBean Emp類代碼4-1:
package cn.gaogzhen.domain; import java.util.Date; /** * 員工類 * @author gaogzhen * */ public class Emp { private int id; private String ename; private int job_id; private int mgr; private Date joindate; private double salary; private double bonus; private int dept_id; public Emp(int id, String ename, int job_id, int mgr, Date joindate, double salary, double bonus, int dept_id) { super(); this.id = id; this.ename = ename; this.job_id = job_id; this.mgr = mgr; this.joindate = joindate; this.salary = salary; this.bonus = bonus; this.dept_id = dept_id; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public int getJob_id() { return job_id; } public void setJob_id(int job_id) { this.job_id = job_id; } public int getMgr() { return mgr; } public void setMgr(int mgr) { this.mgr = mgr; } public Date getJoindate() { return joindate; } public void setJoindate(Date joindate) { this.joindate = joindate; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public double getBonus() { return bonus; } public void setBonus(double bonus) { this.bonus = bonus; } public int getDept_id() { return dept_id; } public void setDept_id(int dept_id) { this.dept_id = dept_id; } @Override public String toString() { return "Emp [id=" + id + ", name=" + ename + ", job_id=" + job_id + ", mgr=" + mgr + ", joindate=" + joindate + ", salary=" + salary + ", bonus=" + bonus + ", dept_id=" + dept_id + "]"; } }
-
JDBCDemo05類代碼4-2:
package cn.gaogzhen.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.Date; import java.util.List; import cn.gaogzhen.domain.Emp; public class JDBCDemo5 { public static void main(String[] args) { List<Emp> list = new JDBCDemo5().findAll(); System.out.println(list); } public List<Emp> findAll() { Connection conn = null; Statement statement = null; ResultSet ret = null; List<Emp> list = new ArrayList<>(); // 2. 註冊驅動 try { Class.forName("com.mysql.cj.jdbc.Driver"); // 3. 獲取連接對象Connection conn = DriverManager.getConnection("jdbc:mysql:///db1?serverTimezone=Asia/Shanghai", "root", "root"); // 4. sql語句 String sql = "select * from emp"; // 5. 獲取執行sql語句的對象statement statement = conn.createStatement(); // 6. 執行sql操作,獲取結果 ret = statement.executeQuery(sql); // 7. 解析結果 while(ret.next()) { int id = ret.getInt("id"); String ename = ret.getString("ename"); int job_id = ret.getInt("job_id"); int mgr = ret.getInt("mgr"); Date joindate = ret.getDate("joindate"); double salary = ret.getDouble("salary"); double bonus = ret.getDouble("bonus"); int dept_id = ret.getInt("dept_id"); list.add(new Emp(id, ename, job_id, mgr, joindate, salary, bonus, dept_id)); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { // 8. 釋放資源 if (ret != null) { try { ret.close(); } catch (SQLException e) { e.printStackTrace(); } } if (statement != null) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } return list; } }
-
測試結果:
[Emp [id=1001, name=孫悟空, job_id=4, mgr=1004, joindate=2000-12-17, salary=8000.0, bonus=0.0, dept_id=20], Emp [id=1002, name=盧俊義, job_id=3, mgr=1006, joindate=2001-02-20, salary=16000.0, bonus=3000.0, dept_id=30], Emp [id=1003, name=林沖, job_id=3, mgr=1006, joindate=2001-02-22, salary=12500.0, bonus=5000.0, dept_id=30], Emp [id=1004, name=唐僧, job_id=2, mgr=1009, joindate=2001-04-02, salary=29750.0, bonus=0.0, dept_id=20], Emp [id=1005, name=李逵, job_id=4, mgr=1006, joindate=2001-09-28, salary=12500.0, bonus=14000.0, dept_id=30], Emp [id=1006, name=宋江, job_id=2, mgr=1009, joindate=2001-05-01, salary=28500.0, bonus=0.0, dept_id=30], Emp [id=1007, name=劉備, job_id=2, mgr=1009, joindate=2001-09-01, salary=24500.0, bonus=0.0, dept_id=10], Emp [id=1008, name=豬八戒, job_id=4, mgr=1004, joindate=2007-04-19, salary=30000.0, bonus=0.0, dept_id=20], Emp [id=1009, name=羅貫中, job_id=1, mgr=0, joindate=2001-11-17, salary=50000.0, bonus=0.0, dept_id=10], Emp [id=1010, name=吳用, job_id=3, mgr=1006, joindate=2001-09-08, salary=15000.0, bonus=0.0, dept_id=30], Emp [id=1011, name=沙僧, job_id=4, mgr=1004, joindate=2007-05-23, salary=11000.0, bonus=0.0, dept_id=20], Emp [id=1012, name=李逵, job_id=4, mgr=1006, joindate=2001-12-03, salary=9500.0, bonus=0.0, dept_id=30], Emp [id=1013, name=小白龍, job_id=4, mgr=1004, joindate=2001-12-03, salary=30000.0, bonus=0.0, dept_id=20], Emp [id=1014, name=關羽, job_id=4, mgr=1007, joindate=2002-01-23, salary=13000.0, bonus=0.0, dept_id=10]]
後記 :
本項目爲參考某馬視頻開發,相關視頻及配套資料可自行度娘或者聯繫本人。上面爲自己編寫的開發文檔,持續更新。歡迎交流,本人QQ:806797785
前端項目源代碼地址:https://gitee.com/gaogzhen/vue-leyou
後端JAVA源代碼地址:https://gitee.com/gaogzhen/JAVA