url = jdbc:MysqL:///db3 user = root password = admin driver = com.MysqL.cj.jdbc.Driver
JDBCUtils.java
package util; import java.io.FileReader; import java.io.IOException; import java.net.URL; import java.sql.*; import java.util.Properties; /** * 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(); System.out.println(path);*/ //2.加载文件 pro.load(new FileReader("src/cn/jdbc.properties")); //pro.load(new FileReader(path)); //3.获取数据,赋值 JDBCUtils.url = pro.getProperty("url"); user = pro.getProperty("user"); password = pro.getProperty("password"); driver = pro.getProperty("driver"); //4.注册驱动 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(); } } } 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(); } } } }
jdbcdemo.java
import cn.itcast.domain.Emp; import util.JDBCUtils; 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().findAll2(); 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; } public List<Emp> findAll2(){ //方法返回一个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"); conn=JDBCUtils.getConnection(); 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 (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(); } }*/ JDBCUtils.close(rs,stmt,conn); } return list; } }
运行结果:
小编说
以上是编程之家为你收集整理的使用JDBC工具类查询表格并展示全部内容。
原文地址:https://blog.csdn.net/weixin_45877026/article/details/121054128
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。