當前位置:首頁 » 操作系統 » 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;
}
}

熱點內容
python的注釋符號 發布:2025-07-04 08:29:19 瀏覽:126
守望領域門禁密碼是多少 發布:2025-07-04 08:16:22 瀏覽:333
存儲晶元價格 發布:2025-07-04 08:16:20 瀏覽:356
大地球源碼 發布:2025-07-04 08:10:29 瀏覽:165
棋牌手游源碼 發布:2025-07-04 08:10:18 瀏覽:818
為啥編程廣告 發布:2025-07-04 07:30:01 瀏覽:570
資料庫備機 發布:2025-07-04 07:30:00 瀏覽:533
靜態內部類java 發布:2025-07-04 07:25:45 瀏覽:235
玉林電信dns伺服器地址 發布:2025-07-04 07:17:34 瀏覽:438
用鏡像壓縮 發布:2025-07-04 07:17:31 瀏覽:636