在数据库中建有名为ego的数据库,有一名为stalla的表。想通过A.jsp传一个stId给B.jsp,从数据库中查询出该stId的记录,显示在B.jsp的表单上。然后可以对stalla的部分值进行修改,再提交表单,修改数据库中记录。
但是现在只能实现A.jsp把stId的值传给B.jsp,而数据库中该表的记录没能显示出来。
我是先建一个stall类,该类的数据成员就是stalla表中的各项属性,并进行了对象初始化和构造set,get获取属性的方法。再通过stallOperation类的方法来进行对stall对象各种操作,另外有一个SqlData类,专门进行对数据库的操作。当我测试不显示在Jsp页面上时,建一个Java工程,通过Main函数调用stallOperation类的search方法时,能正常显示出数据库中stalla表stId的值。但在JSP页面中只能显示出stall构造函数中初始化的值。 连接数据库的SqlData.java部分代码如下:
public class SqlData {
String sDBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url ="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ego";
//ego为数据库名
String user = "yc";//用户名
String password = "123";//密码
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
public SqlData() {
try {
Class.forName(sDBDriver); //建立数据库连接
} catch (java.lang.ClassNotFoundException e) {
System.err.println("SqlData: " + e.getMessage());
}
}
public ResultSet executeQuery(String sql) { //查询
try {
conn = DriverManager.getConnection(url, user, password);
stmt =
conn.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
} catch (SQLException ex) {
System.err.println("SqlData.executeQuery:" + ex.getMessage());
}
return rs;
}
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
} public void closeConn() { //关闭数据库连接
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} //stallOperation.java 操作stall对象查询部分代码如下: public class stallOperation extends SqlData{
private stall aStall=new stall(); //建立档口对象aStall
private javax.servlet.http.HttpServletRequest request; //建立页面请求
private boolean sqlflag = true ; //对接收到的数据是否正确
public String sqlStr=""; //记录SQL语句
/**
* 查询档口信息
*/
public boolean search(HttpServletRequest res) throws Exception{
request=res; //这个参数传递不明白,是模仿一本书上写的!!!
String stId="1001";
try{
SqlData sqlBean=new SqlData();
String sql = "SELECT * FROM stalla where stId='"+stId+"'";//查询语句
ResultSet rs=null;
rs=sqlBean.executeQuery(sql); //执行查询
rs.next();
//从数据库中查询出数据,构造档口对象,设置各属性
aStall.setStId(rs.getString("stId"));
aStall.setStFloor(rs.getInt("stFloor"));
....
sqlBean.closeStmt();
sqlBean.closeConn(); //关闭连接
return true;
}catch(Exception e){
System.out.println(e.getMessage());
return false;
}
} B.jsp代码如下:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page session="true" %>
<%@ page import="java.sql.*"%>
<%@ page import="dbConnect.*"%>
<%@ page import="java.util.*"%><jsp:useBean id="sqlBean" scope="page" class="dbConnect.SqlData"/>
<jsp:useBean id="aStall" scope="page" class="dbConnect.stall"/>
<jsp:useBean id="stallOp" scope="page" class="dbConnect.stallOperation"/><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>修改档口信息</title>
<style type="text/css">
<!--
.style3 {font-size: 36px}
-->
</style>
</head><body>
<div align="center">
<p class="style3">档口信息修改
</p>
<% //从数据库中获取将要修改的档口信息 String mesg = "";
String stId=request.getParameter("stId"); //获得档口编号
out.println(stId); //test1 测试通过 ,能得到前一个页面传来的stId!
if(stallOp.search(request)) //查找记录
{
mesg = "档口信息读取成功!";
}
else
{
mesg = "档口信息读取失败!";
}
%> <form name="form1" method="post" action="B.jsp">
<table width="440" border="1" bordercolor="#666666" bgcolor="#99CCFF">
<tr>
<td width="199" bordercolor="#000000"><div align="center">档口号</div></td>
<td width="225"><div align="center"><%= aStall.getStId() %></div></td>
</tr>
<tr>
<td bordercolor="#000000"><div align="center">楼层</div></td>
<td><div align="center"><%= aStall.getStFloor() %></div></td>
</tr>
<tr>
<td bordercolor="#000000"><div align="center">建筑面积(平方米)</div></td>
<td><div align="center">
<div align="center"><%= aStall.getStStructArea() %></div></td>
</tr>
....(省略显示其他属性的JSP代码)...................................................................
小弟初学Java ,Jsp,我想Java中能正常查询,可能是写的JSP有问题,可不知道问题出在哪儿,查了一些资料,还未解决,特在此请求走过,路过的朋友帮帮忙,先谢谢了。
(问题解决后马上结帖给分,帮忙顶帖的朋友也有分)
但是现在只能实现A.jsp把stId的值传给B.jsp,而数据库中该表的记录没能显示出来。
我是先建一个stall类,该类的数据成员就是stalla表中的各项属性,并进行了对象初始化和构造set,get获取属性的方法。再通过stallOperation类的方法来进行对stall对象各种操作,另外有一个SqlData类,专门进行对数据库的操作。当我测试不显示在Jsp页面上时,建一个Java工程,通过Main函数调用stallOperation类的search方法时,能正常显示出数据库中stalla表stId的值。但在JSP页面中只能显示出stall构造函数中初始化的值。 连接数据库的SqlData.java部分代码如下:
public class SqlData {
String sDBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url ="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ego";
//ego为数据库名
String user = "yc";//用户名
String password = "123";//密码
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
public SqlData() {
try {
Class.forName(sDBDriver); //建立数据库连接
} catch (java.lang.ClassNotFoundException e) {
System.err.println("SqlData: " + e.getMessage());
}
}
public ResultSet executeQuery(String sql) { //查询
try {
conn = DriverManager.getConnection(url, user, password);
stmt =
conn.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
} catch (SQLException ex) {
System.err.println("SqlData.executeQuery:" + ex.getMessage());
}
return rs;
}
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
} public void closeConn() { //关闭数据库连接
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} //stallOperation.java 操作stall对象查询部分代码如下: public class stallOperation extends SqlData{
private stall aStall=new stall(); //建立档口对象aStall
private javax.servlet.http.HttpServletRequest request; //建立页面请求
private boolean sqlflag = true ; //对接收到的数据是否正确
public String sqlStr=""; //记录SQL语句
/**
* 查询档口信息
*/
public boolean search(HttpServletRequest res) throws Exception{
request=res; //这个参数传递不明白,是模仿一本书上写的!!!
String stId="1001";
try{
SqlData sqlBean=new SqlData();
String sql = "SELECT * FROM stalla where stId='"+stId+"'";//查询语句
ResultSet rs=null;
rs=sqlBean.executeQuery(sql); //执行查询
rs.next();
//从数据库中查询出数据,构造档口对象,设置各属性
aStall.setStId(rs.getString("stId"));
aStall.setStFloor(rs.getInt("stFloor"));
....
sqlBean.closeStmt();
sqlBean.closeConn(); //关闭连接
return true;
}catch(Exception e){
System.out.println(e.getMessage());
return false;
}
} B.jsp代码如下:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page session="true" %>
<%@ page import="java.sql.*"%>
<%@ page import="dbConnect.*"%>
<%@ page import="java.util.*"%><jsp:useBean id="sqlBean" scope="page" class="dbConnect.SqlData"/>
<jsp:useBean id="aStall" scope="page" class="dbConnect.stall"/>
<jsp:useBean id="stallOp" scope="page" class="dbConnect.stallOperation"/><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>修改档口信息</title>
<style type="text/css">
<!--
.style3 {font-size: 36px}
-->
</style>
</head><body>
<div align="center">
<p class="style3">档口信息修改
</p>
<% //从数据库中获取将要修改的档口信息 String mesg = "";
String stId=request.getParameter("stId"); //获得档口编号
out.println(stId); //test1 测试通过 ,能得到前一个页面传来的stId!
if(stallOp.search(request)) //查找记录
{
mesg = "档口信息读取成功!";
}
else
{
mesg = "档口信息读取失败!";
}
%> <form name="form1" method="post" action="B.jsp">
<table width="440" border="1" bordercolor="#666666" bgcolor="#99CCFF">
<tr>
<td width="199" bordercolor="#000000"><div align="center">档口号</div></td>
<td width="225"><div align="center"><%= aStall.getStId() %></div></td>
</tr>
<tr>
<td bordercolor="#000000"><div align="center">楼层</div></td>
<td><div align="center"><%= aStall.getStFloor() %></div></td>
</tr>
<tr>
<td bordercolor="#000000"><div align="center">建筑面积(平方米)</div></td>
<td><div align="center">
<div align="center"><%= aStall.getStStructArea() %></div></td>
</tr>
....(省略显示其他属性的JSP代码)...................................................................
小弟初学Java ,Jsp,我想Java中能正常查询,可能是写的JSP有问题,可不知道问题出在哪儿,查了一些资料,还未解决,特在此请求走过,路过的朋友帮帮忙,先谢谢了。
(问题解决后马上结帖给分,帮忙顶帖的朋友也有分)
private stall aStall=new stall(); //建立档口对象aStallaStall的作用范围只在stallOperation 类内部,而你查询出来的信息通过aStall.setStId(rs.getString("stId"));aStall.setStFloor(rs.getInt("stFloor"));
存进了aStall这个对象变量里,所以从数据库中取到了值
但是在JSP页面你不可能访问到aStall这个对象,所以自然显示不出来数据
而在JSP页面你通过<jsp:useBean id="aStall" scope="page" class="dbConnect.stall"/>
创建了一个新的对象,名字也叫aStall,作用域是scope="page",也就是当前JSP页面,而此处的
aStall并不是stallOperation 类内部的aStall,所以你在后边显示数据通过
<%= aStall.getStId() %>读取的是JSP页面从新定义的aStall对象,而你把数据库里读出来的数据却放在了stallOperation 类内部的aStall对象里,自然页面什么都显示不出来了。你可以这样修改:在stallOperation 类的search方法里
aStall.setStId(rs.getString("stId"));
aStall.setStFloor(rs.getInt("stFloor"));
...
存放完数据以后加上一句res.setAttribute("aStall",aStall);
这也是你的疑问:request=res; //这个参数传递不明白,是模仿一本书上写的!!!
这个参数就是用来让你存放aStall对象让页面使用的
最后你还需要把<jsp:useBean id="aStall" scope="page" class="dbConnect.stall"/>
这一段重新建立对象的代码删掉,OK问题解决!
代码如下:
package mypack;import java.sql.*;import javax.sql.*;
import javax.naming.*;
import java.util.*;
public class BookDB {
private ArrayList books;
//private String dbUrl = "jdbc:mysql://localhost:3306/BookDB";
//private String dbUser = "dbuser";
//private String dbPwd = "1234";
private DataSource ds = null;
public BookDB() throws Exception{
//Class.forName("com.mysql.jdbc.Driver");
Context ctx = new InitialContext();
if(ctx == null)
throw new Exception("No Context!");
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/BookDB");
}
public Connection getConnection() throws Exception{
//return DriverManager.getConnection(dbUrl,dbUser,dbPwd);
return ds.getConnection();
}
public void closeConnection(Connection con){
try{
if(con != null)
con.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void closePrepStmt(PreparedStatement prepStmt){
try{
if(prepStmt != null)
prepStmt.close();
}catch(Exception we){
we.printStackTrace();
}
}
public void closeResultSet(ResultSet rs){
try{
if(rs != null)
rs.close();
}catch(Exception gj){
gj.printStackTrace();
}
}
public int getNumberOfBooks() throws Exception{
Connection con = null;
PreparedStatement prepStmt = null;
ResultSet rs = null;
books = new ArrayList();
try{
con = getConnection();
String selectStatement = "select * " + "from books";
prepStmt = con.prepareStatement(selectStatement);
rs = prepStmt.executeQuery();
while(rs.next()){
BookDetails bd = new BookDetails(rs.getString(1),rs.getString(2),rs.getString(3),rs.getFloat(4),rs.getInt(5),rs.getString(6),rs.getInt(7));
books.add(bd);
}
}finally{
closeResultSet(rs);
closePrepStmt(prepStmt);
closeConnection(con);
}
return books.size();
}
public Collection getBooks() throws Exception{
Connection con = null;
PreparedStatement prepStmt = null;
ResultSet rs = null;
books = new ArrayList();
try{
con = this.getConnection();
String selectStatement = "select *" + "from books";
prepStmt = con.prepareStatement(selectStatement);
rs = prepStmt.executeQuery();
while(rs.next()){
BookDetails bd = new BookDetails(rs.getString(1),rs.getString(2),rs.getString(3),rs.getFloat(4),rs.getInt(5),rs.getString(6),rs.getInt(7));
books.add(bd);
}
}finally{
closeResultSet(rs);
closePrepStmt(prepStmt);
closeConnection(con);
}
Collections.sort(books);
return books;
}
public BookDetails getBookDetails(String bookId) throws Exception{
Connection con = null;
PreparedStatement prepStmt = null;
ResultSet rs = null;
try{
con =this.getConnection();
String selectStatement = "select *" + "from books where id = ?";
prepStmt = con.prepareStatement(selectStatement);
prepStmt.setString(1,bookId);
rs = prepStmt.executeQuery();
if(rs.next()){
BookDetails bd = new BookDetails(rs.getString(1),rs.getString(2),rs.getString(3),rs.getFloat(4),rs.getInt(5),rs.getString(6),rs.getInt(7));
prepStmt.close();
return bd;
}
else{
return null;
}
}finally{
closeResultSet(rs);
closePrepStmt(prepStmt);
closeConnection(con);
}
}
public void buyBooks(ShoppingCart cart) throws Exception{
Connection con = null;
Collection items = cart.getItems();
Iterator i = items.iterator();
try{
con = this.getConnection();
con.setAutoCommit(false);
while(i.hasNext()){
ShoppingCartItem sci = (ShoppingCartItem)i.next();
BookDetails db = (BookDetails)sci.getItem();
String id = db.getBookId();
int quantity = sci.getQuantity();
buyBook(id,quantity,con);
}
con.commit();
con.setAutoCommit(true);
}catch(Exception rj){
con.rollback();
throw rj;
}finally{
closeConnection(con);
}
}
public void buyBook(String bookId,int quantity,Connection con) throws Exception{
PreparedStatement prepStmt = null;
ResultSet rs = null;
try{
String selectStatement = "select *" + "from books where id = ?";
prepStmt = con.prepareStatement(selectStatement);
prepStmt.setString(1,bookId);
rs = prepStmt.executeQuery();
if(rs.next()){
prepStmt.close();
String updateStatement = "update books set saleamount = saleamount + ? where id = ?";
prepStmt = con.prepareStatement(updateStatement);
prepStmt.setInt(1,quantity);
prepStmt.setString(2,bookId);
prepStmt.executeUpdate();
prepStmt.close();
}
}finally{
closeResultSet(rs);
closePrepStmt(prepStmt);
}
}}你自己再看看那儿出了问题,好吗?我现在有点忙,所以就只能这样了哈!
谢谢你的帮助,我按你的方法试了一下,res.setAttribute("aStall",aStall); 这句放在了存放数据后,sqlBean.closeStmt();
sqlBean.closeConn(); //关闭连接
这2句前。
也去掉了<jsp:useBean id="aStall" scope="page" class="dbConnect.stall"/>后,但在Eclipse下会提示<%= aStall.getStId() %>中的“aStall不能解析”。运行时JSP报错:
HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException: Unable to compile class for JSPAn error occurred at line: 53 in the jsp file: /stallInfoModify.jspGenerated servlet error:
[javac] Compiling 1 source fileD:\Tomcat 5.0\work\Catalina\localhost\egoMS\org\apache\jsp\stallInfoModify_jsp.java:86: cannot find symbol
symbol : variable aStall
location: class org.apache.jsp.stallInfoModify_jsp
out.print( aStall.getStId() );
^An error occurred at line: 53 in the jsp file: /stallInfoModify.jspGenerated servlet error:
D:\Tomcat 5.0\work\Catalina\localhost\egoMS\org\apache\jsp\stallInfoModify_jsp.java:86: internal error; cannot instantiate print(boolean) at javax.servlet.jsp.JspWriter to (aStall.getStId)
out.print( aStall.getStId() );...(下面其他值的错误信息相同)
...................................................................................
那该如何修改呢,小弟虚心听你指点。先谢谢了
这个不能去掉,要不你怎么创建这个bean呢
出错就是因为找不到 aStall
在页面上加这个试试
dbConnect.stall aStall=request.getAttribute("aStall");
名字重复不知道会不会抱错
如果抱错就把
res.setAttribute("aStall",aStall); 改称res.setAttribute("aStall1",aStall);
再写
dbConnect.stall aStall=request.getAttribute("aStall1");
在JSP页面里再写这一段:
<%
stall aStall=(stall)request.getAttribute("aStall");
%>如果你用的Struts,那么就不用上边这一段,输出的时候不用<%= aStall.getStId() %>
可以用Struts的<bean:write>标签,可以这样写:
<bean:write name="aStall" property="stid"/>
property属性指定和aStall.getStId()对应得到的字段的名字
<%
stall aStall=(stall)request.getAttribute("aStall");
%>
这一段写在<%= aStall.getStId() %>之前
空指针异常的错误。
HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:358)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
root cause java.lang.NullPointerException
org.apache.jsp.stallInfoModify_jsp._jspService(stallInfoModify_jsp.java:100)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
note The full stack trace of the root cause is available in the Tomcat logs.
--------------------------------------------------------------------------------Apache Tomcat/5.0.16
..............................................................................
我以前遇到过这种错误时 是没有读取到数据库中的数据。
还有我把 String stId="1001"; 写在search方法内,是不是有问题啊,JSp页面接收的是前一个页面的stId,又在search里定义了局部变量stId.
这个只是想先测试一下能不能取出数据。最后是想通过参数来传递stId,可当我把search方法中的String stId="1001";去掉,在public boolean search(HttpServletRequest res,String stId) throws Exception{ 加上stId参数,JSP页面中把if(stallOp.search(request)) //查找记录
改为if(stallOp.search(request,stId)) //查找记录
后还是显示的java.lang.NullPointerException。这个问题不知道如何解决?小弟初学,还有很多地方不明白,望各位大虾不吝赐教,谢谢啦。
看看aStall是不是空