菜鸟上路 请各位帮我看一下这个数据库调用好吗
我的目标是 调用数据库中的一个商品表 但是根本调不出来 得到的是商品是一个null
我的Servlet是:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
StringBuffer str = new StringBuffer();
ShowGoods sh = new ShowGoods();
HttpSession session = request.getSession(true);
session.setAttribute("s", sh);
try
{
Statement sta = dbcon.getcon().createStatement();
ResultSet rs = sta.executeQuery("select * from goods");
str = tablegoods.getTable(rs);
sh.setQueryResult(str);
}
catch(Exception e)
{
str.append(e);
}
RequestDispatcher dis = request.getRequestDispatcher("GoodsList.jsp");
dis.forward(request, response);
}
调用表的java是
public class tablegoods {
public static StringBuffer getTable(ResultSet rs) throws Exception
{
StringBuffer str = new StringBuffer();
str.append("<form action = addInServelet>");
str.append("<table border=1 width=790 bordercolor=pink background=pictures/tableback.jpg>");
str.append("<th width=80>"+"图片");
str.append("<th width=80>"+"商品名");
str.append("<th width=80>"+"价格");
str.append("<th width=80>"+"购买数量");
str.append("<th width=80>"+"操作");
while(rs.next())
{
String s = "<img src= "+rs.getString(1)+" width=150 height=150 />";
String q = "<select name="+rs.getString(7)+"quantity><option value=1>1</option><option value=2>2</option><option value=3>3</option><option value=4>4</option><option value=5>5</option></select>";
str.append("<tr>");
str.append("<td>"+s+"</td>");
str.append("<td>"+rs.getString(2)+"</td>");
str.append("<td>"+rs.getString(3)+"</td>");
str.append("<td>"+q+"</td>");
str.append("<td>"+"加入购物车<input type=checkbox name=buy value="+rs.getString(7)+"/>"+"</td>");
str.append("</tr>");
}
str.append("<table border=1>");
str.append("<br><input type=submit value=将选中商品加入购物车 /></form>");
return str;
}
}
Bean文件是:
public class ShowGoods {
StringBuffer queryResult;
public StringBuffer getQueryResult()
{
return queryResult;
}
public void setQueryResult(StringBuffer str)
{
queryResult = str;
}
}
调用的jsp是:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="zkgo1.ShowGoods"%>
<jsp:useBean id="s" type="zkgo1.ShowGoods" scope="session"/>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>商品列表</title>
</head>
<body background= "images/ymm.jpg"><center>
<br><br><br><h3>商品有:</h3>
<jsp:getProperty name="s" property="queryResult"/>
</center></body>
</html>
真心求 已经做不下去了 新手上路好难 找不到错误 数据库 HTML JSP
------解决方案--------------------
ResultSet rs = sta.executeQuery("select * from goods");,首先判断这个是否为空,依次向上找,控制台打印或者调试
[解决办法]
你是报错,还是画面打印null
session.setAttribute("s", sh);
写在RequestDispatcher dis = request.getRequestDispatcher("GoodsList.jsp");前面。
[解决办法]
建议在catch块中把异常打出来,不可以看到哪出错了