java實現(xiàn)將結果集封裝到List中的方法
更新時間:2016年07月18日 14:50:18 作者:wdfscp
這篇文章主要介紹了java實現(xiàn)將結果集封裝到List中的方法,涉及java數(shù)據(jù)庫查詢及結果集轉換的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了java實現(xiàn)將結果集封裝到List中的方法。分享給大家供大家參考,具體如下:
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class TestResultSet {
public static List query(){
Connection conn = new ConnectionUtil().openConnection();
try {
Statement stmt = conn.createStatement();
String sql = "select id,name,email from customertbl";
ResultSet rs = stmt.executeQuery(sql);
//將結果集封裝到List中
List list = new ArrayList();
while(rs.next()){
// 可以根據(jù)列名稱也可以根據(jù)列索引
int id = rs.getInt(1);
String name = rs.getString("name");
String email = rs.getString("email");
System.out.println(id+":"+name+":"+email);
Customer c = new Customer();
c.setId(id);
c.setName(name);
c.setEmail(email);
//將對象存放到list容器中
list.add(c);
}
return list;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
conn.close();
} catch (SQLException e) {
conn = null;
e.printStackTrace();
}
}
return null;
}
}
希望本文所述對大家java程序設計有所幫助。
相關文章
使用Spirng Boot Admin監(jiān)控Spring Cloud應用項目
這篇文章主要介紹了使用Spirng Boot Admin監(jiān)控Spring Cloud應用項目,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
spring boot 若依系統(tǒng)整合Ueditor部署時上傳圖片錯誤問題
這篇文章主要介紹了spring boot 若依系統(tǒng)整合Ueditor部署時上傳圖片錯誤問題,本文給大家分享問題解決方法,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10
Idea進行pull的時候Your local changes would be
這篇文章主要介紹了Idea進行pull的時候Your local changes would be overwritten by merge.具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11

