JDBC练习select语句和JDBC工具类
JDBC练习select语句
定义一个方法,查询emp表的数据将其封装为对象,然后装载对象,返回
1、定义emp类
2、定义方法public List<Emp> findAll(){}
3、实现方法 select * from emp;
Emp类:
/** * 封装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 bound; private int dept_id; public Emp() { } public Emp(int id, String ename, int job_id, int mgr, Date joindate, double salary, double bound, int dept_id) { this.id = id; this.ename = ename; this.job_id = job_id; this.mgr = mgr; this.joindate = joindate; this.salary = salary; this.bound = bound; 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 + ", bound=" + bound + ", dept_id=" + 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 getBound() { return bound; } public void setBound(double bound) { this.bound = bound; } public int getDept_id() { return dept_id; } public void setDept_id(int dept_id) { this.dept_id = dept_id; } }
测试类:
/** * 定义一个方法,查询emp表的数据将其封装为对象,然后装载对象,返回 */ public class JDBCDemo8 { public static void main(String[] args) { List<Emp> list = findAll(); System.out.println(list); } /** * 查询所有emp对象 * @return */ public static List<Emp> findAll(){ Connection conn = null; Statement stmt = null; ResultSet rs = null; List<Emp> list = null; try { //1.注册驱动 Class.forName("com.mysql.cj.jdbc.Driver"); //2.获取连接 conn = DriverManager.getConnection("jdbc:mysql:///db1", "root", "root"); //3.定义sql String sql = "select * from emp"; //4.获取执行sql对象 stmt = conn.createStatement(); //5.执行sql rs = stmt.executeQuery(sql); //6.遍历结果集,封装对象,装载集合 Emp emp = null; list = new ArrayList<>(); while (rs.next()){ //获取数据 int id = rs.getInt("id"); String ename = rs.getString("ename"); int job_id = rs.getInt("job_id"); int mgr = rs.getInt("mgr"); Date joindate = rs.getDate("joindate"); double salary = rs.getDouble("salary"); double bouns = rs.getDouble("bonus"); int dept_id = rs.getInt("dept_id"); //创建Emp对象 emp = new Emp(id,ename,job_id,mgr,joindate,salary,bouns,dept_id); //装载集合 list.add(emp); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException throwables) { throwables.printStackTrace(); }finally { if (rs!=null){ try { rs.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (stmt!=null){ try { stmt.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (conn!=null){ try { conn.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } } return list; } }
JDBC工具类
抽取JDBC工具类:JDBCUtils
目的:简化书写
分析:
1.注册驱动
2.抽取一个方法获取连接对象
需求:不想传递参数(麻烦),还得保证工具类的通用性
解决:配置文件
jdbc.properties
url=
user=
password=
3.抽取一个方式释放资源
JDBCUtils类:
/** * JDBC工具类 */ public class JDBCUtils { private static String url; private static String user; private static String password; private static String driver; /** * 文件的读取,只需要读取一次即可拿到这些值,使用静态代码块 */ static { try { //读取资源文件,获取值 //1.创建Properties对象 Properties pro = new Properties(); //获取src路径下的文件方式--->ClassLoader 类加载器 ClassLoader classLoader = JDBCUtils.class.getClassLoader(); URL res = classLoader.getResource("jdbc.properties"); String path = res.getPath(); //2.加载文件 pro.load(new FileReader(path)); //3.获取数据,赋值 url = pro.getProperty("url"); user = pro.getProperty("user"); password = pro.getProperty("password"); driver = pro.getProperty("driver"); Class.forName(driver); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } /** * 获取连接 * @return */ public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url,user,password); } /** * 释放资源 * @param stmt * @param conn */ public static void close(Statement stmt,Connection conn){ if (stmt!=null){ try { stmt.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (conn!=null){ try { conn.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } } /** * 释放资源 * @param stmt * @param conn */ public static void close(ResultSet rs, Statement stmt, Connection conn){ if (rs!=null){ try { rs.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (stmt!=null){ try { stmt.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (conn!=null){ try { conn.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } } }
测试类:
public static void main(String[] args) { List<Emp> list = findAll2(); System.out.println(list); } /** * 演示JDBC工具类 * @return */ public static List<Emp> findAll2(){ Connection conn = null; Statement stmt = null; ResultSet rs = null; List<Emp> list = null; try { // //1.注册驱动 // Class.forName("com.mysql.cj.jdbc.Driver"); // //2.获取连接 // conn = DriverManager.getConnection("jdbc:mysql:///db1", "root", "root"); conn = JDBCUtils.getConnection(); //3.定义sql String sql = "select * from emp"; //4.获取执行sql对象 stmt = conn.createStatement(); //5.执行sql rs = stmt.executeQuery(sql); //6.遍历结果集,封装对象,装载集合 Emp emp = null; list = new ArrayList<>(); while (rs.next()){ //获取数据 int id = rs.getInt("id"); String ename = rs.getString("ename"); int job_id = rs.getInt("job_id"); int mgr = rs.getInt("mgr"); Date joindate = rs.getDate("joindate"); double salary = rs.getDouble("salary"); double bouns = rs.getDouble("bonus"); int dept_id = rs.getInt("dept_id"); //创建Emp对象 emp = new Emp(id,ename,job_id,mgr,joindate,salary,bouns,dept_id); //装载集合 list.add(emp); } } catch (SQLException throwables) { throwables.printStackTrace(); }finally { JDBCUtils.close(rs,stmt,conn); } return list; }
运行结果:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix