1. 程式人生 > 其它 >ubuntu系統使用命令列統計資料夾中檔案的個數

ubuntu系統使用命令列統計資料夾中檔案的個數

技術標籤:javamysqlsql資料庫jdbc

JDBC-最簡單實現-如何將mysql資料庫表中的資料查詢出來並存入集合中

  1. 展示資料庫emp的資料:
    在這裡插入圖片描述

  2. 建立員工類Emp:

package ALL_JDBC.day0115;

/**
 * 將emp表中的資料封裝成一個類
 * */
public class Emp {
    //員工id
    private int id;
    //員工姓名
    private String name;
    //員工職位名稱
    private int jobId;
    //員工的領導
    private int mgr;
    //員工加入的時間
private String joinDate; //員工薪水 private double salary; //員工獎金 private double bonus; //員工的部門id private int deptId; public Emp() { } public Emp(int id, String name, int jobId, int mgr, String joinDate, double salary, double bonus, int deptId) { this.id = id;
this.name = name; this.jobId = jobId; this.mgr = mgr; this.joinDate = joinDate; this.salary = salary; this.bonus = bonus; this.deptId = deptId; } //get/set方法 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 int getJobId() { return jobId; } public void setJobId(int jobId) { this.jobId = jobId; } public int getMgr() { return mgr; } public void setMgr(int mgr) { this.mgr = mgr; } public String getJoinDate() { return joinDate; } public void setJoinDate(String 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 getDeptId() { return deptId; } public void setDeptId(int deptId) { this.deptId = deptId; } /* 重寫toString()方便檢視 */ @Override public String toString() { return "Emp{" + "id=" + id + ", name='" + name + '\'' + ", jobId=" + jobId + ", mgr=" + mgr + ", joinDate='" + joinDate + '\'' + ", salary=" + salary + ", bonus=" + bonus + ", deptId=" + deptId + '}'; } }
  1. 建立實體類:
package ALL_JDBC.day0115;

import java.sql.*;
import java.util.ArrayList;

/* 將emp表中的資料查詢出來  封裝成一個方法  然後儲存到集合裡面  這裡使用ArrayList集合 */
public class JdbcDemo04 {
    public static void main(String[] args) {

        //建立一個ArrayList集合來接收查詢方法的返回值;
        ArrayList<Emp> query = query();
        for ( Emp e : query ){
            System.out.println(e);
        }
    }

    /* 封裝一個方法來查詢資料庫的emp表 */
    public static ArrayList<Emp> query(){
        ArrayList<Emp> array = new ArrayList<>();

        //建立連線資料庫的物件
        Connection con = null;
        //建立執行sql語句的物件
        Statement sate = null;

        try{
            //註冊驅動
            Class.forName("com.mysql.cj.jdbc.Driver");
            //定義引數url、user、password(因為我這裡是最新版本 url與上課的有些不同)
            String url = "jdbc:mysql://localhost:3306/zsj?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
            String root = "root";
            String pass = "123456";
            //獲取連線物件
            con = DriverManager.getConnection( url,root,pass );

            String sql = " select * from emp ";
            //獲取執行sql語句的物件
            Statement stat = con.createStatement();
            //執行sql語句
            ResultSet res = stat.executeQuery(sql);

            //遍歷查詢結果集
            while( res.next() ){
                /*int index = 0;
                emp.setId( res.getInt("id") );
                emp.setName( res.getString("ename") );
                emp.setJobId( res.getInt( "job_id" ) );
                emp.setMgr( res.getInt( "mgr" ) );*/
                //先建立員工Emp物件
                Emp emp = new Emp(
                        res.getInt(1) ,
                        res.getString(2) ,
                        res.getInt(3),
                        res.getInt(4),
                        res.getString(5),
                        res.getDouble(6),
                        res.getDouble(7),
                        res.getInt(8)
                );
                array.add(emp);
            }


        }catch (SQLException e){
            e.printStackTrace();
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }finally {
            if (sate != null) {
                try {
                    sate.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        return array;
    }
}