Connection con = null;
PreparedStatement statement = null;
ResultSet res = null;

List<Student> students = new ArrayList<Student>();

List<Student> getAll() {

try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
statement = (PreparedStatement) con.createStatement();
res = statement.executeQuery("SELECT * FROM students");
} catch (Exception e) {
e.printStackTrace();
}
try {
while(res.next()){
String name = res.getString(1);
int age = res.getInt(2);
Student student = new Student(name,age);
students.add(student);
}
} catch (SQLException e1) {
e1.printStackTrace();
}
if (res != null) {
try {
res.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return students;
}

最新文章

  1. Linux下MySQL忘记密码
  2. sdoi 2009 &amp; 状态压缩
  3. MongoVUE的使用
  4. 学习python得到方向与主体
  5. const的全面理解
  6. SVN安装笔记
  7. SharePoint 2013 如何使用TaxonomyWebTaggingControl 控件
  8. win7下设置 WiFi AP
  9. mongoDB rs和sh关键字的作用
  10. 4.hadoop的安装与配置
  11. curl 向远程服务器传输file文件
  12. yum upgrade和yum update的区别
  13. VS code 配置C++编译环境
  14. 在Node.js使用Promise的方式操作Mysql
  15. PAT甲题题解-1130. Infix Expression (25)-中序遍历
  16. 收藏:SQL Server 数据库改名
  17. Integer诡异特性
  18. 在Mac上快速Kill掉Tomcat
  19. delphi shr和shl的作用
  20. java AES加密、解密(兼容windows和linux)

热门文章

  1. Java分割中英文,并且中文不能分割一半?
  2. 分布式流处理框架 Apache Storm —— 编程模型详解
  3. SpringBoot系列——加载自定义配置文件
  4. Map集合的4种遍历方式
  5. SPOJ INTSUB - Interesting Subset(数学)
  6. iOS组件化开发一使用source管理远端库升级(四)
  7. scrapy基础知识之防止爬虫被反的几个策略::
  8. MYSQL5.7---ONLY_FULL_GROUP_BY 异常处理
  9. 【UR #7】水题走四方 题解
  10. 无法在&lt;fastCGI&gt;应用程序配置中找到&lt;handler&gt; scriptProcessor