當前位置:首頁 » 操作系統 » 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-16 22:57:39 瀏覽:680
api開發php 發布:2025-09-16 22:06:15 瀏覽:594
mysql自動備份linux 發布:2025-09-16 21:58:33 瀏覽:942
怎麼用自己的伺服器ip做域名 發布:2025-09-16 21:49:57 瀏覽:915
vc為什麼能編譯不能用 發布:2025-09-16 21:48:03 瀏覽:742
編譯原理沖突圖的定義 發布:2025-09-16 21:26:45 瀏覽:808
安卓手機芯哪個牌子好 發布:2025-09-16 21:26:33 瀏覽:206
centos編譯安裝git 發布:2025-09-16 21:19:55 瀏覽:975
安卓系統如何使用手機優盤 發布:2025-09-16 21:14:01 瀏覽:333
在手機上注冊公積金如何設置密碼 發布:2025-09-16 21:07:01 瀏覽:824