當前位置:首頁 » 操作系統 » 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-03-12 14:12:35 瀏覽:520
神奇青蛙安卓版在哪裡下載 發布:2025-03-12 14:05:12 瀏覽:529
曙光牌路由器登錄密碼是多少 發布:2025-03-12 13:51:36 瀏覽:99
舞蹈培訓源碼 發布:2025-03-12 13:34:04 瀏覽:741
電腦與伺服器之間快速傳輸文件 發布:2025-03-12 13:26:55 瀏覽:955
ftp上的文件在伺服器 發布:2025-03-12 13:26:53 瀏覽:416
linux關閉445埠 發布:2025-03-12 13:20:47 瀏覽:219
加密語句表情 發布:2025-03-12 13:17:36 瀏覽:821
電腦伺服器響應不了 發布:2025-03-12 13:16:02 瀏覽:897
vmwarenat外網訪問 發布:2025-03-12 13:06:31 瀏覽:47