当前位置:首页 » 操作系统 » 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;
}
}

热点内容
java返回this 发布:2025-10-20 08:28:16 浏览:649
制作脚本网站 发布:2025-10-20 08:17:34 浏览:940
python中的init方法 发布:2025-10-20 08:17:33 浏览:635
图案密码什么意思 发布:2025-10-20 08:16:56 浏览:824
怎么清理微信视频缓存 发布:2025-10-20 08:12:37 浏览:734
c语言编译器怎么看执行过程 发布:2025-10-20 08:00:32 浏览:1070
邮箱如何填写发信服务器 发布:2025-10-20 07:45:27 浏览:302
shell脚本入门案例 发布:2025-10-20 07:44:45 浏览:164
怎么上传照片浏览上传 发布:2025-10-20 07:44:03 浏览:855
python股票数据获取 发布:2025-10-20 07:39:44 浏览:766