struts2獲取資料庫數據
㈠ 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;
}
}