struts2中action里面怎么写一个方法直接查询数据库数据,

struts2中action里面怎么写一个方法直接查询数据库数据,,第1张

先写的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

}

}

我不知道你说的Struts2查询 *** 作是啥?

Struts是界面和后台交互的中间控制器,假设你页面上提交了一个查询的请求,流程会进入Action,并进入相应方法,你可以在Action定义一个查询的方法,再调用service,service再调用dao,dao和数据库交互。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/6935549.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-03-30
下一篇2023-03-30

发表评论

登录后才能评论

评论列表(0条)

    保存