Mysql鸡础(从数据库中导入学生数据用list集合存储emp成员)

  • Post author:
  • Post category:mysql



在此使用statement类后面文章讲述为什么不用statement而用preparestatement

数据库中数据

主函数

package cs.itcast.jdbc;

import jdk.nashorn.internal.ir.JumpToInlinedFinally;

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

/*
将emp封为对象,然后打印,然后装在集合返回
 */
public class jdbcptc {
    public List<emp> findall() {
        //注册驱动
        Statement stmt = null;
        ResultSet rs = null;
        Connection conn = null;
        List<emp> list = new ArrayList<emp>();
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/zzt?serverTimezone=GMT&useSSL=false", "root", "root");
            String sql = "select *from emp";
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            emp e= new emp();

            while (rs.next()) {
                int id = rs.getInt("id");
                String ename = rs.getString("name");
                String gender = rs.getString("gender");
                double salary = rs.getDouble("salary"); 
                int dept_id = rs.getInt("dept_id");
                e = new emp();
                e.setDept_id(dept_id);
                e.setEname(ename);
                e.setGender(gender);
                e.setId(id);
                e.setSalary(salary);
                list.add(e);

            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                if (stmt != null) {
                    try {
                        rs.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    if (conn != null) {
                        try {
                            rs.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }

                }
            }
        }
    return list;
    }

    public static void main(String[] args) {
        List<emp> list = new jdbcptc().findall();
        System.out.println(list);
    }
}





emp类

package cs.itcast.jdbc;

import java.util.Date;

/*、
封装emp表的数据
 */
public class emp {
    private int id;
    private String ename;
    private String  gender;
    private double salary;
    private int 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 String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    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 +
                ", ename='" + ename + '\'' +
                ", gender='" + gender + '\'' +
                ", salary=" + salary +
                ", dept_id=" + dept_id +
                '}'+'\n';
    }
}



版权声明:本文为giaogege666原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。