java根据姓名检索信息功能怎么做
DAO:publicMessagesearchMessage(Stringname)throwsSQLException;DAOIMPL:publicMessagesea...
DAO:
public Message searchMessage(String name)throws SQLException;
DAOIMPL:
public Message searchMessage(String name) throws SQLException {
String sql="select * from message where name=?";
Message message=new Message();
PreparedStatement pstmt=null;
ResultSet rs=null;
Connection conn=ConnDB.getConnection();
try {
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, name);
rs=pstmt.executeQuery();
if(rs.next()){
message.setMessageid(rs.getRow());
message.setName(rs.getString("name"));
message.setQq(rs.getInt("qq"));
message.setTitle(rs.getString("title"));
message.setContent(rs.getString("content"));
message.setReply(rs.getString("reply"));
message.setTime(rs.getString("time"));
}
rs.close();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
return message;
}
servlet;(servlet要怎么写)
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
method = Integer.parseInt(request.getParameter("method"));
if(method==1){
search(request, response);
}
}
private void search(HttpServletRequest request,HttpServletResponse response)
throws IOException{
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out=response.getWriter();
Message message=new Message();
jsp:
<form name="searchForm" action="SearchServlet?method=1"method="post">
姓名:<input name="name" type="text">
<input name="search"type="submit"value="检索留言"> 展开
public Message searchMessage(String name)throws SQLException;
DAOIMPL:
public Message searchMessage(String name) throws SQLException {
String sql="select * from message where name=?";
Message message=new Message();
PreparedStatement pstmt=null;
ResultSet rs=null;
Connection conn=ConnDB.getConnection();
try {
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, name);
rs=pstmt.executeQuery();
if(rs.next()){
message.setMessageid(rs.getRow());
message.setName(rs.getString("name"));
message.setQq(rs.getInt("qq"));
message.setTitle(rs.getString("title"));
message.setContent(rs.getString("content"));
message.setReply(rs.getString("reply"));
message.setTime(rs.getString("time"));
}
rs.close();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
return message;
}
servlet;(servlet要怎么写)
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
method = Integer.parseInt(request.getParameter("method"));
if(method==1){
search(request, response);
}
}
private void search(HttpServletRequest request,HttpServletResponse response)
throws IOException{
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out=response.getWriter();
Message message=new Message();
jsp:
<form name="searchForm" action="SearchServlet?method=1"method="post">
姓名:<input name="name" type="text">
<input name="search"type="submit"value="检索留言"> 展开
1个回答
展开全部
servlet中:
String name =request.getPapameter("name");//接收页面信息
DAOIMPL d=new DAOIMPL();
ArrayList list=new ArrayList();
list=D.searchMessage(name);
reqeust.setAttribute("list",list);
request.getRequestDispatcher("/info.jsp").forward(request, response);
String name =request.getPapameter("name");//接收页面信息
DAOIMPL d=new DAOIMPL();
ArrayList list=new ArrayList();
list=D.searchMessage(name);
reqeust.setAttribute("list",list);
request.getRequestDispatcher("/info.jsp").forward(request, response);
更多追问追答
追答
兄弟,你这报错是因为不能用int型接收,你查询出来返回的是个Massage,你应该用这个接收,如果想用int,那你应该在impl中返回一个int型的值
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询