package cn.itcast.jdbc;

import cn.itcast.domain.User;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
* @author newcityman
* @date 2019/8/13 - 23:27
*/
public class JDBCDemo06 {

public static void main(String[] args) {
JDBCDemo06 demo06 = new JDBCDemo06();
demo06.findAll();
}

public List<User> findAll() {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
List<User> list = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/day13", "root", "123");
stmt = conn.createStatement();
String sql = "select * from user";
rs = stmt.executeQuery(sql);
list = new ArrayList<User>();
User user = null;
//遍历集合
while (rs.next()) {
int id = rs.getInt("id");
String username = rs.getString("username");
String password = rs.getString("password");
String sex = rs.getString("sex");
Date birthday = rs.getDate("birthday");
String hobbys = rs.getString("hobbys");
String des = rs.getString("des");
//封装对象
user = new User();
user.setId(id);
user.setUsername(username);
user.setPassword(password);
user.setSex(sex);
user.setBirthday(birthday);
user.setHobbys(hobbys);
user.setDes(des);
//装载集合
list.add(user);
}
System.out.println(list);
} 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;
}
}

最新文章

  1. html与html5
  2. VS2012 还原默认设置
  3. 为什么&lt;b&gt;&lt;/b&gt;不推荐使用
  4. 75篇关于Tomcat源码和机制的文章
  5. Html中代码换行造成空格间距的问题
  6. [MongDB] 主从架构--官方极力不推荐
  7. bigdecimal类型数据的min方法
  8. robotframework笔记11
  9. 浅谈学习C++时用到的【封装继承多态】三个概念
  10. H264/AVC视频解码时AVC1和H264的区别
  11. iOS 浅复制、深复制、完全复制的知识点梳理验证(附加归档解档)
  12. MVC Controller 与 View 传值
  13. 解题的小问题(C++)
  14. CTF 字符统计1
  15. C/C++生成静态库动态库及语言交互
  16. P2360 地下城主(BFS)
  17. VS插件File Nesting
  18. git 常见命令 和 git 原理图
  19. HDU5086:Revenge of Segment Tree(规律题)
  20. css预处理器和后处理器

热门文章

  1. storm在windows下本地调试报错java.lang.UnsatisfiedLinkError cannot find rocksdbjnixxxxxxxxxx.dll
  2. 大爽Python入门教程 2-1 认识容器
  3. C#中指针的使用(转)
  4. [loj3284]Exercise
  5. Furion分表分库我也要happy coding
  6. 交叉编译环境的linaro-gdb可以用了,结果打开core文件,显示堆栈都是??
  7. Redis 源码简洁剖析 02 - SDS 字符串
  8. 【JavaSE】格式化输出
  9. Python画一个四点连线并计算首尾距离
  10. [WC2021] 表达式求值