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

在此使用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';
    }
}

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值