完整使用JDBC访问数据库
2024-10-02 04:30:40
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;
}
最新文章
- Linux下MySQL忘记密码
- sdoi 2009 &; 状态压缩
- MongoVUE的使用
- 学习python得到方向与主体
- const的全面理解
- SVN安装笔记
- SharePoint 2013 如何使用TaxonomyWebTaggingControl 控件
- win7下设置 WiFi AP
- mongoDB rs和sh关键字的作用
- 4.hadoop的安装与配置
- curl 向远程服务器传输file文件
- yum upgrade和yum update的区别
- VS code 配置C++编译环境
- 在Node.js使用Promise的方式操作Mysql
- PAT甲题题解-1130. Infix Expression (25)-中序遍历
- 收藏:SQL Server 数据库改名
- Integer诡异特性
- 在Mac上快速Kill掉Tomcat
- delphi shr和shl的作用
- java AES加密、解密(兼容windows和linux)
热门文章
- Java分割中英文,并且中文不能分割一半?
- 分布式流处理框架 Apache Storm —— 编程模型详解
- SpringBoot系列——加载自定义配置文件
- Map集合的4种遍历方式
- SPOJ INTSUB - Interesting Subset(数学)
- iOS组件化开发一使用source管理远端库升级(四)
- scrapy基础知识之防止爬虫被反的几个策略::
- MYSQL5.7---ONLY_FULL_GROUP_BY 异常处理
- 【UR #7】水题走四方 题解
- 无法在<;fastCGI>;应用程序配置中找到<;handler>; scriptProcessor