struts2中action里面怎么写一个方法直接查询数据库数据,
struts2中action里面怎么写一个方法直接查询数据库数据,并且用ajax调用返回,然后再jsp用select选框来展示,...
struts2中action里面怎么写一个方法直接查询数据库数据,并且用ajax调用返回,然后再jsp用select选框来展示,
展开
展开全部
先写的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.setFileintroduce(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 dao = new Dao();
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
LinkedList<FileModel> modelSet = (LinkedList<FileModel>) dao.findAll();
if (modelSet!=null){
System.out.println(modelSet);
ActionContext.getContext().getSession().put("msg", modelSet);
return SUCCESS;}
else
return ERROR;
}
}
{
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.setFileintroduce(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 dao = new Dao();
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
LinkedList<FileModel> modelSet = (LinkedList<FileModel>) dao.findAll();
if (modelSet!=null){
System.out.println(modelSet);
ActionContext.getContext().getSession().put("msg", modelSet);
return SUCCESS;}
else
return ERROR;
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询