當前位置:首頁 » 操作系統 » struts2獲取資料庫數據

struts2獲取資料庫數據

發布時間: 2023-08-07 23:33:05

㈠ struts2從資料庫中讀取數據問題

在action中取到你需要的數據,放在action的類屬性中,再加上public的訪問方法, action執行完畢後跳到頁面, 就可以直接用${}或者<s:property>方式訪問這個屬性

感覺你還沒有基本概念...

㈡ struts2中action裡面怎麼寫一個方法直接查詢資料庫數據,

先寫的DAO:public List<FileModel> findAll()
{
Connection con = null ;
PreparedStatement ps = null ;
FileModel file = null ;
ResultSet rs = null;
List<FileModel> set = null ;
try
{
con = DBconnection.getConnection();
String sql = "select * from file ;
ps = con.prepareStatement(sql);

set = new LinkedList<FileModel>();
rs = ps.executeQuery() ;

while(rs.next())
{
file = new FileModel();
file.setFilepath(rs.getString(1));
file.setFiletime(rs.getTimestamp(2));
file.setFileintroce(rs.getString(3));
file.setFilediscuss(rs.getString(4));
file.setFilescore(rs.getFloat(5));
file.setFiletype(rs.getString(6));
file.setDirection(rs.getString(7));
file.setFileid(rs.getInt(8));
file.setFilename(rs.getString(9));
set.add(file);
}

}
catch(SQLException e)
{
throw new RuntimeException(e.getMessage(),e);
}
finally
{
DBconnection.free(rs, ps, con);
}

return set;
}
在action中調用DAO:
public class FindAction extends ActionSupport {
private Dao = new Dao();

@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
LinkedList<FileModel> modelSet = (LinkedList<FileModel>) .findAll();

if (modelSet!=null){
System.out.println(modelSet);

ActionContext.getContext().getSession().put("msg", modelSet);
return SUCCESS;}
else
return ERROR;
}
}

熱點內容
近親繁殖演算法 發布:2025-09-17 00:48:47 瀏覽:695
安卓如何關閉浮屏球 發布:2025-09-17 00:23:23 瀏覽:921
下列存儲器中存取周期最短的是 發布:2025-09-17 00:16:20 瀏覽:243
如何查詢域名用的伺服器地址 發布:2025-09-17 00:04:01 瀏覽:807
php過濾非中文 發布:2025-09-17 00:02:13 瀏覽:519
來源碼 發布:2025-09-16 23:57:00 瀏覽:858
yeah郵箱的伺服器地址 發布:2025-09-16 23:36:52 瀏覽:703
c的引用java 發布:2025-09-16 23:36:48 瀏覽:309
的n次方編程 發布:2025-09-16 23:25:34 瀏覽:287
python安卓版 發布:2025-09-16 23:01:04 瀏覽:825