时间:2021-07-01 10:21:17 帮助过:11人阅读
package cn.itcast.jdbc; import java.sql.*; /* * DDL语句 * */ public class JdbcDemo6 { public static void main(String[] args) { //声明数据库连接对象 Connection conn = null; //声明数据库执行对象 Statement stmt = null; //声明结果集对象 ResultSet rs = null; try { //1.注册驱动 Class.forName("com.mysql.jdbc.Driver"); //2.获取数据库连接对象 conn = DriverManager.getConnection("jdbc:mysql:///myemployees", "root", "ROOT"); //3.定义SQL String sql ="select * from job_grades;"; //4.获取执行SQL的对象 stmt = conn.createStatement(); //5.执行SQL rs= stmt.executeQuery(sql); //6.处理返回结果 //6.1让游标向下移动一行 rs.next(); //6.2获取数据 String str = rs.getString(1); int i = rs.getInt(2); double d = rs.getDouble(3); System.out.println(str+" "+i+" "+d); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { //7.释放资源,最后用的最先释放 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(); } } } } }
package cn.itcast.jdbc; import java.sql.*; public class JdbcDemo7 { public static void main(String[] args) { //声明数据库连接对象 Connection conn = null; //声明数据库执行对象 Statement stmt = null; //声明结果集对象 ResultSet rs = null; try { //1.注册驱动 Class.forName("com.mysql.jdbc.Driver"); //2.获取数据库连接对象 conn = DriverManager.getConnection("jdbc:mysql:///myemployees", "root", "ROOT"); //3.定义SQL String sql ="select * from job_grades;"; //4.获取执行SQL的对象 stmt = conn.createStatement(); //5.执行SQL rs= stmt.executeQuery(sql); //6.处理返回结果 //循环判断游标是否是最后一行末尾 while (rs.next()){ //获取数据 String str = rs.getString(1); int i = rs.getInt(2); double d = rs.getDouble(3); System.out.println(str+" "+i+" "+d); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { //7.释放资源,最后用的最先释放 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(); } } } } }
package cn.itcast.domain; import java.util.Date; /* * 封装Beauty表数据的JavaBean类 * */ public class Beauty { private int id; private String name; private char sex; private Date borndate; private double phone; private int boyfriend_id; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } public Date getBorndate() { return borndate; } public void setBorndate(Date borndate) { this.borndate = borndate; } public double getPhone() { return phone; } public void setPhone(int phone) { this.phone = phone; } public int getBoyfriend_id() { return boyfriend_id; } public void setBoyfriend_id(int boyfriend_id) { this.boyfriend_id = boyfriend_id; } @Override public String toString() { return "Beauty{" + "id=" + id + ", name=‘" + name + ‘\‘‘ + ", sex=" + sex + ", borndate=" + borndate + ", phone=" + phone + ", boyfriend_id=" + boyfriend_id + ‘}‘; } }
封装类用的util包下的date
项目中返回的sql下的date
sql下的date是util下date的子类,父类可以接受子类对象。直接封装就行了
package cn.itcast.jdbc; import cn.itcast.domain.Beauty; import java.sql.*; import java.util.ArrayList; import java.util.List; /** * 定义一个方法,查询beauty表的数据将其封装为对象,然后装载集合返回 */ public class JDBCDemo8 { public static void main(String[] args) { List<Beauty> list = new JDBCDemo8().findAll(); System.out.println(list); System.out.println(list.size()); } /** * 查询所有beauty对象 */ public List<Beauty> findAll() { Connection conn = null; Statement stmt = null; ResultSet rs = null; List<Beauty> list = null; try { //1.注册驱动 Class.forName("com.mysql.jdbc.Driver"); //2.获取连接 conn = DriverManager.getConnection("jdbc:mysql:///girls", "root", "ROOT"); //3.第一SQL String sql = "select * from beauty"; //4.获取执行SQL的对象 stmt = conn.createStatement(); //5.执行SQL rs = stmt.executeQuery(sql); //7.封装对象 Beauty beauty = null; //6.遍历结果集,封装对象,装载集合 list = new ArrayList<Beauty>(); while (rs.next()) {//判断是否有下一行,有就执行,没有就结束执行下面的语句 //获取数据 //名字要和数据库保持一致,和封装类没有关系 int id = rs.getInt("id"); String name = rs.getString("name"); Date borndate = rs.getDate("borndate"); int boyfriend_id = rs.getInt("boyfriend_id"); // Beauty beauty = new Beauty(); /* * 如果写在这就会占用很多栈内存(new的对象都在栈内存) * 写在第七步的位置 * */ //创建对象并赋值 beauty = new Beauty(); beauty.setId(id); beauty.setName(name); beauty.setBorndate(borndate); beauty.setBoyfriend_id(boyfriend_id); //装载集合 list.add(beauty); } } 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; } }
JDBC——ResultSet结果集对象
标签:创建 表格 rgs png 怎么 trace new roo 索引