当前位置:首页 » 操作系统 » 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-07-04 15:59:31 浏览:128
android开发sd卡 发布:2025-07-04 15:50:28 浏览:949
离歌脚本 发布:2025-07-04 15:50:13 浏览:415
距估计算法 发布:2025-07-04 15:48:50 浏览:814
安卓手机的号码在哪里看 发布:2025-07-04 15:36:53 浏览:27
蒲公英路由器服务器端ip 发布:2025-07-04 15:20:30 浏览:678
python学习中 发布:2025-07-04 15:20:26 浏览:257
linux查看cuda版本 发布:2025-07-04 15:15:49 浏览:44
反编译浏览器 发布:2025-07-04 15:15:45 浏览:453
java直播网站源码 发布:2025-07-04 14:46:35 浏览:170