【1030练习】JDBC查询表格并展示

表格样式:

在这里插入图片描述
在这里插入图片描述

代码:

Emp.java

package cn.itcast.domain;

import java.util.Date;

/**
 * 封装emp表数据的JavaBean
 */
public class Emp {
    private int id;
    private String ename;
    private int job_id;
    private int mgr;
    private Date joindate;
    private double salary;
    private double bonus;
    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 int getJob_id() {
        return job_id;
    }

    public void setJob_id(int job_id) {
        this.job_id = job_id;
    }

    public int getMgr() {
        return mgr;
    }

    public void setMgr(int mgr) {
        this.mgr = mgr;
    }

    public Date getJoindate() {
        return joindate;
    }

    public void setJoindate(Date 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 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 + '\'' +
                ", job_id=" + job_id +
                ", mgr=" + mgr +
                ", joindate=" + joindate +
                ", salary=" + salary +
                ", bonus=" + bonus +
                ", dept_id=" + dept_id +
                '}';
    }
}

jdbcdemo.java

import cn.itcast.domain.Emp;

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

/**
 * 定义一个方法,查询emp表的数据并将其封装为对象,然后装载集合,返回。
 */

public class jdbcdemo {

    public static void main(String[] args) {
        List<Emp> list = new jdbcdemo().findAll();
        System.out.println(list);
    }
    /**
     * 查询所有emp对象
     */

    public List<Emp> findAll(){ //方法返回一个List集合,集合里面为Emp对象
        Connection conn =null;
        Statement stmt = null;
        ResultSet rs = null;
        List<Emp> list = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql:///db3", "root", "admin");
            String sql = "select * from emp";
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            Emp emp = null;
            list = new ArrayList<Emp>();
            while(rs.next()){
                int id = rs.getInt(1);
                String ename = rs.getString(2);
                int job_id = rs.getInt(3);
                int mgr = rs.getInt(4);
                Date joindate = rs.getDate(5);
                double salary = rs.getDouble(6);
                double bonus = rs.getDouble(7);
                int dept_id = rs.getInt(8);
                //创建对象,并赋值
                emp = new Emp();
                emp.setId(id);
                emp.setEname(ename);
                emp.setJob_id(job_id);
                emp.setMgr(mgr);
                emp.setJoindate(joindate);
                emp.setSalary(salary);
                emp.setBonus(bonus);
                emp.setDept_id(dept_id);

                //装载集合
                list.add(emp);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(stmt!=null){
                try {
                    stmt.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

            }
            if(rs!=null){
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        return list;
    }
}

运行结果:
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这个问题属于技术问题。针对你的问题,我可以给你提供以下的 Java 代码参考: ```java import java.sql.*; public class JDBCExample { static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/ie2121"; static final String USER = "yourusername"; static final String PASS = "yourpassword"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; try { // 注册 JDBC 驱动器 Class.forName("com.mysql.jdbc.Driver"); // 打开一个连接 System.out.println("连接数据库..."); conn = DriverManager.getConnection(DB_URL, USER, PASS); // 执行查询 System.out.println("实例化statement对象..."); stmt = conn.createStatement(); String sql; sql = "INSERT INTO student (`id`, `name`, `phone`, `score`) VALUES (1, 'John', '123-456-7890', 90)"; stmt.executeUpdate(sql); // 查询数据并输出 System.out.println("查询数据..."); sql = "SELECT id, name, phone, score FROM student"; ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { // 通过字段检索 int id = rs.getInt("id"); String name = rs.getString("name"); String phone = rs.getString("phone"); int score = rs.getInt("score"); // 输出数据 System.out.print("ID: " + id); System.out.print(", 姓名: " + name); System.out.print(", 电话: " + phone); System.out.println(", 成绩: " + score); } // 修改数据 System.out.println("修改 John 的成绩..."); sql = "UPDATE student SET score = 85 WHERE name='John'"; stmt.executeUpdate(sql); // 再次查询数据并输出 System.out.println("再次查询数据..."); sql = "SELECT id, name, phone, score FROM student WHERE name='John'"; rs = stmt.executeQuery(sql); while (rs.next()) { // 通过字段检索 int id = rs.getInt("id"); String name = rs.getString("name"); String phone = rs.getString("phone"); int score = rs.getInt("score"); // 输出数据 System.out.print("ID: " + id); System.out.print(", 姓名: " + name); System.out.print(", 电话: " + phone); System.out.println(", 成绩: " + score); } // 清理环境 rs.close(); stmt.close(); conn.close(); } catch (SQLException se) { // 处理 JDBC 错误 se.printStackTrace(); } catch (Exception e) { // 处理 Class.forName 错误 e.printStackTrace(); } finally { // 关闭资源 try { if (stmt != null) stmt.close(); } catch (SQLException se2) { } // 什么也不做 try { if (conn != null) conn.close(); } catch (SQLException se) { se.printStackTrace(); } System.out.println("Goodbye!"); } } } ``` 运行这个程序后,你将在控制台输出以下内容: ``` 连接数据库... 实例化statement对象... 查询数据... ID: 1, 姓名: John, 电话: 123-456-7890, 成绩: 90 修改 John 的成绩... 再次查询数据... ID: 1, 姓名: John, 电话: 123-456-7890, 成绩: 85 Goodbye! ``` 请注意,这个程序需要你填写你自己电脑的 MySQL 数据库的地址、用户名和密码。如果你执行程序的时候不能连接到数据库,请检查这些信息是否正确。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值