JDBC练习-select语句
定义一个方法,查询emp表中的数据将其封装为对象,然后装载集合,然后打印
1.定义emp类
2.定义方法public List<Emp> findAll(){}
3.实现方法select * from emp;
Emp类
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; //toString @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 + '}'; } //get和set 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; }
测试类:
public static void main(String[] args) { List<Emp> all = new JdbcPractice6().findAll(); for (Emp emp : all) { System.out.println(emp); } } /** * 查询所有emp对象 * @return */ public List<Emp> findAll(){ Connection conn = null; Statement state = null; ResultSet rs =null; List<Emp> list = null; try { //1.注册驱动 Class.forName("com.mysql.cj.jdbc.Driver"); //2.获取连接 conn = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root"); //3.定义sql String sql = "select * from emp"; //4.获取执行sql对象 state = conn.createStatement(); //5.执行sql(结果集对象) rs = state.executeQuery(sql); //6.遍历结果集,封装对象,装载集合 Emp emp= null; list = new ArrayList<>(); while (rs.next()){ //6.1获取数据 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 bonus = rs.getDouble("bonus"); int dept_id = rs.getInt("dept_id"); //创建emp对象,并赋值 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) { throw new RuntimeException(e); } catch (SQLException e) { throw new RuntimeException(e); }finally { if (rs != null){ try { rs.close(); } catch (SQLException e) { throw new RuntimeException(e); } } if (state != null){ try { state.close(); } catch (SQLException e) { throw new RuntimeException(e); } } if (conn != null){ try { conn.close(); } catch (SQLException e) { throw new RuntimeException(e); } } } return list; }
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 prop = new Properties(); //2.加载文件 prop.load(new FileReader("src/main/resources/jdbc.properties")); //3.获取数据,赋值(于配置文件中的属性名要一致) url = prop.getProperty("url"); user = prop.getProperty("user"); password = prop.getProperty("password"); driver = prop.getProperty("driver"); //注册驱动 Class.forName(driver); } catch (IOException e) { throw new RuntimeException(e); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } /** * 获取连接 * @return 连接对象 */ public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url, user, password); } /** * 释放资源 * @param stmt * @param conn */ public static void close(ResultSet rs, Statement stmt, Connection conn){ 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(); } } } }
测试类
public static void main(String[] args) { //创建方法引用方法 List<Emp> all = new JdbcPractice7().findAll2(); for (Emp emp : all) { System.out.println(emp); } } /** * 查询所有emp对象 * @return */ public List<Emp> findAll2(){ Connection conn = null; Statement state = null; ResultSet rs =null; List<Emp> list = null; try { // //1.注册驱动 // Class.forName("com.mysql.cj.jdbc.Driver"); // //2.获取连接 // conn = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root"); conn = JDBCUtils.getConnection(); //3.定义sql String sql = "select * from emp"; //4.获取执行sql对象 state = conn.createStatement(); //5.执行sql(结果集对象) rs = state.executeQuery(sql); //6.遍历结果集,封装对象,装载集合 Emp emp= null; list = new ArrayList<>(); while (rs.next()){ //6.1获取数据 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 bonus = rs.getDouble("bonus"); int dept_id = rs.getInt("dept_id"); //创建emp对象,并赋值 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 e) { throw new RuntimeException(e); }finally { //释放资源 JDBCUtils.close(rs, state, conn); } return list; }
标签:语句,JDBC,rs,int,public,emp,new,id,select From: https://www.cnblogs.com/qihaokuan/p/16812991.html