怎样查询出数据库的帖子标题?遇到点问题,看下面具体代码,求大神赐教啊···
这里涉及到两张表,一张帖子,一张用户表。下面主要是想查询出用户发的帖子的标题.数据库通过登录时session等到的userName来查询用户所发的帖子。下面是我写的查询代码。(帖子表中的PostSender和用户表中的UserName 是相等的)
但是运行时会报错,错误代码在下面。我不知道怎么错的,请大家教教我啊···
public List<Poster> listpraPost(String userName) throws SQLException {
ArrayList<Poster> postList = new ArrayList<Poster>();
Connection conn = null;
PreparedStatement stat = null;
ResultSet rst = null;
Poster poster = new Poster();
User user=new User();
try {
conn = db.getConnection();
stat = conn.prepareStatement("select PostTitle from Bbs_Post,tbl_Assistant where Bbs_Post.PostSender=tbl_Assistant.UserName and UserName=? ");
stat.setString(1,poster.getPostTitle());
stat.setString(2,userName);
rst=stat.executeQuery();
while (rst.next()) {
poster.setPostTitle(rst.getString(1));
user.setUserName(rst.getString(2));
//poster.setPostSender(rst.getString(2));
postList.add(poster);
}
} finally {
DB.release(conn, stat, rst);
}
return postList;
}
JSP页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" import="java.sql.*"
pageEncoding="UTF-8"%>
<%@page import="valuebean.User" %>
<%@page import="dao.UsersDao" %>
<%@page import="valuebean.Poster" %>
<%@page import="dao.PostDao" %>
<jsp:useBean id="connBean" scope="page" class="db.connDB"/>
<%-- <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> --%>
<%
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ request.getContextPath() + "/";
/* User user=new User();
Poster poster=new Poster(); */
%>
<%
String userName=(String)session.getAttribute("userName");
User user=new User();
UsersDao usersdao=new UsersDao();
user=usersdao.selectInfo(userName);
String userName1=(String)user.getUserName();
int grade=user.getGrade();
Poster poster=new Poster();
PostDao postdao=new PostDao();
postdao.listpraPost(userName);
String posttitle=poster.getPostTitle();
%>
下面的代码省略···
错与提示:org.apache.jasper.JasperException: An exception occurred processing JSP page /front/myplace.jsp at line 26
23: int grade=user.getGrade();
24: Poster poster=new Poster();
25: PostDao postdao=new PostDao();
26: postdao.listpraPost(userName);
27: String posttitle=poster.getPostTitle();
28: %>
29:
Stacktrace:
at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:567)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:456)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:471)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:402)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)
at servlet.placearea.doPost(placearea.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:279)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: javax.servlet.ServletException: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Invalid parameter binding(s).
at org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:911)
at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:840)
at org.apache.jsp.front.myplace_jsp._jspService(myplace_jsp.java:290)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
... 28 more
Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Invalid parameter binding(s).
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.validateParameterIndex(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.setObjectInternal(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.setString(Unknown Source)
at dao.PostDao.listpraPost(PostDao.java:54)
at org.apache.jsp.front.myplace_jsp._jspService(myplace_jsp.java:101)
... 31 more
[解决办法]
select PostTitle from Bbs_Post,tbl_Assistant where Bbs_Post.PostSender=tbl_Assistant.UserName and UserName=?
看看你的 Sql 语句是不是有问题 ,其他真看不出来 了
[解决办法]
看了你的listpraPost类,发现几个问题1. select PostTitle from Bbs_Post,tbl_Assistant where Bbs_Post.PostSender=tbl_Assistant.UserName and UserName=? 。。别名用错了。。改为:select PostTitle from Bbs_Post bp,tbl_Assistant ta where bp.PostSender=ta.UserName and ta.UserName=? 2. while (rst.next()) {poster.setPostTitle(rst.getString(1));user.setUserName(rst.getString(2));//poster.setPostSender(rst.getString(2));postList.add(poster);} 。。。查出的数据存入对象时每个都要实例化。。不然存入list中的始终只有一个。。。改为:while (rst.next()) {Poster poster = new Poster();poster.setPostTitle(rst.getString(1));user.setUserName(rst.getString(2));//poster.setPostSender(rst.getString(2));postList.add(poster);}3. jsp中:Poster poster=new Poster(); //这里new了一个Poster以后啥都没做。。 PostDao postdao=new PostDao(); postdao.listpraPost(userName); String posttitle=poster.getPostTitle(); // 上面posternew出来啥没做。这里何来值取?改为:Poster poster=null;ArrayList<Poster> postList =postdao.listpraPost(userName);for(...) // 自己写一个循环取出Poster对象// 自己写一个取出Poster对象为poster赋值。。。String posttitle=poster.getPostTitle(); // 这样取就有值了。。。
[解决办法]
stat = conn.prepareStatement("select PostTitle from Bbs_Post,tbl_Assistant where Bbs_Post.PostSender=tbl_Assistant.UserName and UserName=? ");stat.setString(1,poster.getPostTitle());stat.setString(2,userName);
[解决办法]