美文网首页
【Java】【JDBC】使用实体类保存结果集

【Java】【JDBC】使用实体类保存结果集

作者: JerichoPH | 来源:发表于2017-04-08 09:56 被阅读82次

    使用实体类保存结果集

    public class Demo_JDBC {
        public static void main(String[] args) throws Exception {
            // 1. 注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 2. 获取链接Connection
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/yiyousu", "root", "mysqlmysql");
            // 3. 得到执行SQL语句的对象Satatement
            Statement stmt = (Statement) conn.createStatement();
            // 4. 执行SQL语句,并返回结果
            ResultSet rs = stmt.executeQuery("select * from `test`");
            List<Test> list = new ArrayList<>();
            // 5. 处理结果
            while (rs.next()) {
                Test t = new Test();
                t.setId(rs.getInt("id"));
                t.setName(rs.getString("name"));
                t.setAge(rs.getInt("age"));
                list.add(t);
            }
            // 6. 关闭资源
            rs.close();
            stmt.close();// 关闭读取器
            conn.close();// 关闭数据库链接
            for (Test test : list) {
                System.out.println(test);
            }
        }
    }
    
    /**
     * Test模型实体
     * 
     * @author JerichoPH
     */
    class Test {
        private int id;
        private String name;
        private int age;
        
        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 int getAge() {
            return age;
        }
        
        public void setAge(int age) {
            this.age = age;
        }
        
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + age;
            result = prime * result + id;
            result = prime * result + ((name == null) ? 0 : name.hashCode());
            return result;
        }
        
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Test other = (Test) obj;
            if (age != other.age)
                return false;
            if (id != other.id)
                return false;
            if (name == null) {
                if (other.name != null)
                    return false;
            } else if (!name.equals(other.name))
                return false;
            return true;
        }
        
        @Override
        public String toString() {
            return "Test [id=" + id + ", name=" + name + ", age=" + age + "]";
        }
    
    }
    

    相关文章

      网友评论

          本文标题:【Java】【JDBC】使用实体类保存结果集

          本文链接:https://www.haomeiwen.com/subject/hwclattx.html