其他分享
首页 > 其他分享> > JDBC_查询练习

JDBC_查询练习

作者:互联网

### 1.查询class表中的数据并将其封装为对象,之后打印输出。

### 2.代码实现
package cn.itcast.jdbc;

public class Emp {
    private int id;
    private String sname;
    private char gender;
    private String company;
    private double salary;
    private int fanbu;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public char getGender() {
        return gender;
    }

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

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public double getSalary() {
        return salary;
    }

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

    public int getFanbu() {
        return fanbu;
    }

    public void setFanbu(int fanbu) {
        this.fanbu = fanbu;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "id=" + id +
                ", name='" + sname + '\'' +
                ", gender=" + gender +
                ", company='" + company + '\'' +
                ", salary=" + salary +
                ", fanbu=" + fanbu +
                '}';
    }
}


package cn.itcast.jdbc;

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

public class JdbcEmp {
    public static void main(String[] args) {
        List<Emp> all = new JdbcEmp().findAll();
        System.out.println(all);
        System.out.println(all.size());
    }
    public List<Emp> findAll(){
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        List<Emp> list = null;
        try {
            //1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //获取数据库连接对象
             conn= DriverManager.getConnection("jdbc:mysql:///test", "root", "123456");
            //定义sql语句
            String sql = "select * from class";
            //获取执行sql对象
            stmt = conn.createStatement();
            //执行sql
            rs = stmt.executeQuery(sql);
            Emp emp = null;
            list = new ArrayList<Emp>();
            while (rs.next()){
                int id = rs.getInt("id");
                String sname = rs.getString("sname");
                String company = rs.getString("company");
                double salary = rs.getDouble("salary");
                int fanbu = rs.getInt("fanbu");
                emp = new Emp();
                emp.setId(id);
                emp.setSname(sname);
                emp.setCompany(company);
                emp.setSalary(salary);
                emp.setFanbu(fanbu);


                list.add(emp);

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

标签:JDBC,String,rs,company,练习,查询,id,public,fanbu
来源: https://www.cnblogs.com/lcc-lv/p/16488806.html