Topic: 一个JavaBean的问题,大家进来看一下! |
Print this page |
1.一个JavaBean的问题,大家进来看一下! | Copy to clipboard |
Posted by: zrwl Posted on: 2006-03-22 13:00 各位好~小弟初学JSP+JAVABEAN,在编写一个数据库查询的JavaBean的时候出现了一个问题(使用别的JAVABEAN的时候没问题),望大家指教!我把代码跟错误提示贴出来: JavaBean代码: package test; import java.io.UnsupportedEncodingException; import java.sql.*; public class QueryBean { public String query_statement; /*定义SQL语句*/ public String param[]; /*查询条件*/ public ResultSet result=null; /*查询结果*/ public Connection conn; //设置查询参数 public void setParam(String param[]) { this.param=param; } //设置SQL查询语句 public void setQueryStatemnet(String query_statement) { this.query_statement=query_statement; } //设置连接参数 public void setConnection(String drivername,String jdbcURL) throws Exception { Connection conn1; Class.forName(drivername); conn1=DriverManager.getConnection(jdbcURL); conn1.setAutoCommit(false); this.conn=conn1; } //获取查询结果 public ResultSet getResult() { try { PreparedStatement select_stm=conn.prepareCall(query_statement, ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); if(param!=null) { for(int i=0;i<=param.length;i++) { select_stm.setString(i+1,param[i]); result=select_stm.executeQuery(); } } }catch(Exception e){e.printStackTrace();} return result; } //对数据库进行增加记录 public void insertRecord() throws SQLException,UnsupportedEncodingException { try { PreparedStatement insert_stm=conn.prepareStatement(query_statement); if(param!=null) { for(int i=0;i<=param.length;i++) { insert_stm.setString(i+1,param[i]); insert_stm.executeUpdate(); insert_stm.close(); conn.commit(); } } } catch(Exception e) { e.printStackTrace(); conn.rollback(); } } //对数据库进行更新操作 public void updateRecord() throws SQLException,UnsupportedEncodingException { try { PreparedStatement update_stm=conn.prepareStatement(query_statement); if(param!=null) { for(int i=0;i<=param.length;i++) { update_stm.setString(i+1,param[i]); update_stm.executeUpdate(); conn.close(); conn.commit(); } } } catch(Exception e) { e.printStackTrace(); conn.rollback(); } } //删除数据记录 public void deleteRecord() throws SQLException,UnsupportedEncodingException { try { PreparedStatement delete_stm=conn.prepareStatement(query_statement); if(param!=null) { for(int i=0;i<=param.length;i++) { delete_stm.setString(i+1,param[i]); delete_stm.executeUpdate(); conn.close(); conn.commit(); } } } catch(Exception e) { e.printStackTrace(); conn.rollback(); } } } 下面是JSP代码: <%@ page contentType="text/html; charset=gb2312" language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ page import="java.sql.*"%> <%@ page import="test.QueryBean"%> <jsp:useBean id="query" class="test.QueryBean" scope="page"> </jsp:useBean> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'queryjavaBeanjap.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <%//查询数据库 ResultSet rs=null; String sqlString="select * from userTable"; try { query.setConnection("sun.jdbc.odbc.JdbcOdbcDriver","jdbc:odbc:testDB"); //设置SQL语句 query.setQueryStatemnet(sqlString); rs=query.getResult(); } catch(Exception e) { System.out.print("SQL异常!"); } %> <%//显示数据 out.print("<table border=1 width=700 align=center bgcolor=#33FFFF>"); out.print("<tr>"); out.print("<td colspan=7 align=center>用QueryBean查询得到的用户数据</td></tr>"); out.print("<tr>"); out.print("<td>"+"用户ID"); out.print("<td>"+"用户名"); out.print("<td>"+"用户真实姓名"); out.print("<td>"+"年龄"); out.print("<td>"+"性别"); out.print("<td>"+"联系地址"); out.print("<td>"+"联系电话"); out.print("</tr>"); rs.beforeFirst(); while(rs.next()) { out.print("<tr>"); out.print("<td>"+rs.getLong("ID")+"</td>"); out.print("<td>"+rs.getString("username")+"</td>"); out.print("<td>"+rs.getString("truename")+"</td>"); out.print("<td>"+rs.getInt("age")+"</td>"); out.print("<td>"+rs.getString("sex")+"</td>"); out.print("<td>"+rs.getString("address")+"</td>"); out.print("<td>"+rs.getString("tel")+"</td>"); out.print("</tr>"); } out.print("</table>"); %> </body> </html> 下面是错误提示: type Exception report message description The server encountered an internal error () that prevented it from fulfilling this request. exception org.apache.jasper.JasperException org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264) javax.servlet.http.HttpServlet.service(HttpServlet.java:802) root cause java.lang.NullPointerException org.apache.jsp.queryjavaBeanjsp_jsp._jspService(org.apache.jsp.queryjavaBeanjsp_jsp:121) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97) javax.servlet.http.HttpServlet.service(HttpServlet.java:802) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264) javax.servlet.http.HttpServlet.service(HttpServlet.java:802) note The full stack trace of the root cause is available in the Apache Tomcat/5.5.15 logs. 非常感谢各位能帮我解决这个问题~~谢谢!! |
2.Re:一个JavaBean的问题,大家进来看一下! [Re: zrwl] | Copy to clipboard |
Posted by: xiaoqiang Posted on: 2006-03-24 22:50 rs=query.getResult(); 没有正确的返回值 |
3.Re:一个JavaBean的问题,大家进来看一下! [Re: zrwl] | Copy to clipboard |
Posted by: chunming_buaa Posted on: 2006-03-26 11:04 //设置SQL查询语句 public void setQueryStatemnet(String query_statement) { this.query_statement=query_statement; } 这里Statemnet 拼正确了 ?!! |
4.Re:一个JavaBean的问题,大家进来看一下! [Re: chunming_buaa] | Copy to clipboard |
Posted by: zrwl Posted on: 2006-03-26 20:29 多谢~~虽然这里写成了setQueryStatemnet,但是在JSP页面跟JAVABEAN中是一致的,不受影响。 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |