JDResultSetBC各个类详解-ResultSet-遍历结果集
Boolean next() : 游标向下移动一行,判断当前行是否是最后一行末尾(是否有数据),
如果是,则返回false,如果不是则返回true
注意:
使用步骤:
1.游标向下移动一行
2.判断是否有数据
3.获取数据
public static void main(String[] args) throws Exception { Connection conn = null; Statement stat = null; ResultSet rs = null; try { //注册驱动 Class.forName("com.mysql.jdbc.Driver"); //获取Connection对象 conn = DriverManager.getConnection("jdbc:mysql:///db3", "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表 */ public class emp { private int id; private String ename; private int job_id; private int mgt; 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 getMgt() { return mgt; } public void setMgt(int mgt) { this.mgt = mgt; } 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 + ", mgt=" + mgt + ", joindate=" + joindate + ", salary=" + salary + ", bouns=" + bonus + ", dept_id=" + dept_id + '}'; } }
/* 定义一个方法,查询emp表的数据将其封装为对象,然后装载集合,返回。 */ public class JDBC_demo01lx { public static void main(String[] args) { List<emp> list = new JDBC_demo01lx().findAll(); System.out.println(list ); } /* 查询所有emp对象 */ ResultSet rs = null; Statement stmt = null; Connection conn=null; List<emp> list=null; public List<emp> findAll(){ try { //注册驱动 Class.forName("com.mysql.jdbc.Driver"); //获取连接 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/practice?useUnicode=true&characterEncoding=utf-8&useSSL=false", "root", "root"); //定义sql String sql = "select * from emp"; //获取sql执行的对象 stmt = conn.createStatement(); //执行sql rs = stmt.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 mgt = rs.getInt("mgt"); Date joindata = 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.setMgt(mgt); emp.setJoindate(joindata); emp.setSalary(salary); emp.setBonus(bonus); emp.setDept_id(dept_id); //装载集合 list.add(emp); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally { 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(); } } } } } return list; } }
原文地址:http://www.cnblogs.com/agzq/p/16797153.html
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请务用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员!
8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载
声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性