JDBC类详解_ResultSet_遍历结果集和JDBC练习_select语句
JDBC类详解_ResultSet_遍历结果集
Boolean next() : 游标向下移动一行,判断当前行是否是最后一行末尾(是否有数据),
如果是,则返回false,如果不是则返回true
注意:
使用步骤:
1.游标向下移动一行
2.判断是否有数据
3.获取数据
private static void ResultSet1() { Connection conn = null; Statement stat = null; ResultSet rs = null; try { //注册驱动 Class.forName("com.mysql.cj.jdbc.Driver"); //获取Connection对象 conn = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root"); //定义sql String sql = "select * from account"; //获取执行sql的对象 Statement stat = conn.createStatement(); //执行sql rs = stat.executeQuery(sql); //处理结果
//循环判断游标是否是最后一行末尾 while (rs.next()){ //获取数据 int id = rs.getInt(1); String name = rs.getString("name"); double balance = rs.getDouble(3); System.out.println(id+"-"+name+"-"+balance); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException throwables) { throwables.printStackTrace(); }finally { if (rs!=null){ try { rs.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (stat!=null){ try { stat.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (conn!=null){ try { conn.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } } }
JDBC练习_select语句
定义一个方法,查询emp表的数据将其封装为对象,然后装载集合,返回。
1.定义Emp类
2.定义方法 public List<Emp> findAll(){}
3.实现方法 select * from emp;
public static void main(String[] args) throws Exception { List<Emp> list = new JDBCD01().findAll(); System.out.println(list); System.out.println(list.size()); } /* 定义一个方法,查询emp表的数据将其封装为对象,然后装载集合,返回。 */ public List<Emp> findAll(){ Connection conn = null; Statement stat = null; ResultSet rs = null; List<Emp> list = null; try { //注册驱动 Class.forName("com.mysql.cj.jdbc.Driver"); //获取Connection对象 conn = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root"); //定义sql String sql = "select * from emp"; //获取执行sql的对象 Statement stat = conn.createStatement(); rs = stat.executeQuery(sql); 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 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) { e.printStackTrace(); } catch (SQLException throwables) { throwables.printStackTrace(); }finally { if (rs!=null){ try { rs.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (stat!=null){ try { stat.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (conn!=null){ try { conn.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } } 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