Java-JDBC-查詢獲取數據庫數據
- JDBC對數據庫的操作也叫做CURD: 它代表創建(Create)、更新(Update)、讀取(Retrieve)和刪除(Delete)操作
- 在JDBC中,使用查詢語句獲取到的是一個
resultSet
數據集
- 這個數據集可以想象為一個表格,表頭就是
SQL
語句查詢的字段,每一行就是查詢出來的每一條數據
resultSet
數據集提供了一系列的get
方法,可以獲取數據集中的數據
resultSet
數據集也有一個next()
方法,這個方法實現的效果如果有下一行,就返回true
並且游標往下移動一行,如果沒有下一行數據,就返回false
如何使用JDBC完成數據庫查詢操作
- 在使用JDBC進行查詢的時候,一般都先寫一個實體類,這個實體類是為了完成將每一行數據封裝為一個對象而創建,實體類中的每一個屬性都對應
SQL
查詢出來的每一個字段(實體類的具體注意事項見代碼注釋)
- 在包裝實體類的時候,屬性推薦使用包裝類進行封裝,避免空值異常
package com.shanlei.entity;
import java.io.Serializable;
import java.util.Date;
/**
* @author: shanlei
* @version: 1.0
*/
/*
* 實體類:
* 和數據庫表格名稱和字段是一一對應的類
* 該類的對象主要用處是存儲從數據庫中查詢出來的數據
* 除此之外,該類沒有任何的其他功能
* 要求
* 1類名和表名保持一致 (見名知意)
* 2屬性個數和數據庫的表的列數保持一致
* 3屬性的數據類型和列的數據類型保持一致
* 4屬性名和數據庫表格的列名要保持一致
* 5所有的屬性必須都是私有的 (出於安全考慮)
* 6實體類的屬性推薦寫成包裝類
* 7日期類型推薦寫成java.util.Date
* 8所有的屬性都要有get和set方法
* 9必須具備空參構造方法
* 10實體類應當實現序列化接口 (mybatis緩存 分布式需要 )
* 11實體類中其他構造方法可選
* */
public class Emp implements Serializable {
private Integer empno;
private String ename;
private String job;
private Integer mgr;
private Date hiredate;
private Double sal;
private Double comm;
private Integer deptno;
@Override
public String toString() {
return "emp{" +
"empno=" + empno +
", ename='" + ename + '\'' +
", job='" + job + '\'' +
", mgr=" + mgr +
", hiredate=" + hiredate +
", sal=" + sal +
", comm=" + comm +
", deptno=" + deptno +
'}';
}
// 空參構造器
public Emp() {
}
// 有參構造器
public Emp(int empno, String ename, String job, int mgr, Date hiredate, double sal, double comm, int deptno) {
this.empno = empno;
this.ename = ename;
this.job = job;
this.mgr = mgr;
this.hiredate = hiredate;
this.sal = sal;
this.comm = comm;
this.deptno = deptno;
}
// 下面都是set與get方法
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getMgr() {
return mgr;
}
public void setMgr(int mgr) {
this.mgr = mgr;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public double getComm() {
return comm;
}
public void setComm(double comm) {
this.comm = comm;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
}
package com.shanlei.test01;
import com.shanlei.entity.Emp;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @author: shanlei
* @version: 1.0
*/
public class Test04 {
private static String driver = "com.mysql.cj.jdbc.Driver";
private static String url = "jdbc:mysql://localhost/mytestdb?useSSL=false&useUnicode=ture&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
private static String user = "root", password = "123456";
// 這是main方法,實現程序主要邏輯
public static void main(String[] args) {
List<Emp> emps = testQuery();
for (Emp emp : emps) {
System.out.println(emp);
}
}
public static List<Emp> testQuery(){
Connection connection = null;
// Statement statement = null; 直接使用Statement執行查詢語句可能遭到SQL注入攻擊,推薦使用PreparedStatement
PreparedStatement preparedstatement = null;
ResultSet resultSet = null;
List<Emp> emps = null;
try {
// 加載注冊驅動
Class.forName(driver);
//2.獲得鏈接 Connection
connection = DriverManager.getConnection(url, user, password);
// 3.准備SQL語句並執行
/*
* 1使用PreparedStatement語句對象防止SQL注入攻擊
* 2PreparedStatement 可以使用 ? 作為參數的占位符
* 3使用?作為占位符,即使是字符串和日期類型,也不使用單獨再添加 ''
* 4connection.createStatement();獲得的是普通語句對象 Statement
* 5connection.prepareStatement(sql);可以獲得一個預編譯語句對象PreparedStatement
* 6如果SQL語句中有?作為參數占位符號,那么要在執行CURD之前先設置參數
* 7通過set***(問號的編號,數據) 方法設置參數
* */
String sql = "select * from emp";
preparedstatement = connection.prepareStatement(sql);
resultSet = preparedstatement.executeQuery();// 這里不需要再傳入SQL語句
// 如果不怕SQL注入的話也可以直接使用statement.executeQuery(sql)查詢語句,代碼如下
/*
// 3.獲取語句對象
statement = connection.createStatement();
// 4.執行語句
String sql = "select * from emp";
resultSet = statement.executeQuery(sql);
*/
// 4.獲取結果並對結果進行遍歷封裝
emps = new ArrayList<Emp>();
while(resultSet.next()){
int empno = resultSet.getInt("empno");
String ename = resultSet.getString("ename");
String job = resultSet.getString("job");
int mgr = resultSet.getInt("mgr");
Date hiredate = resultSet.getDate("hiredate");
double sal = resultSet.getDouble("sal");
double comm = resultSet.getDouble("comm");
int deptno = resultSet.getInt("deptno");
emps.add(new Emp(empno, ename, job, mgr, hiredate, sal ,comm, deptno));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
// 5.關閉資源
if(null != resultSet){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(null != preparedstatement){
try {
preparedstatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(null != connection){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return emps;
}
}