首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

jsp连接数据库时发生异常求解

2013-09-11 
jsp连接数据库时发生错误求解JAVABEANpackage beanimport java.sql.*public class QueryBean {public St

jsp连接数据库时发生错误求解
JAVABEAN
package bean;

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 setQuery_statement(String query_statement) {
this.query_statement = query_statement;
}

// 设置连接参数
public void setConnection(String driverName, String jdbcURL,
String username, String passwd) throws Exception {
Connection conn1;
Class.forName(driverName);
conn1 = DriverManager.getConnection(jdbcURL, username, passwd);
conn1.setAutoCommit(false);
this.conn = conn1;
}

// 获取查询结果
public ResultSet getResult() {
try {
PreparedStatement select_stm = conn.prepareStatement(
query_statement,
java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,
java.sql.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) {
System.out.println(e);
}
return result;
}

public void insertRecord()throws SQLException,java.io.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)
{
System.out.println(e);
conn.rollback();
}
}

/* 对数据记录进行更新操作 */
public void updateRecord() throws SQLException,java.io.UnsupportedEncodingException{
try{
PreparedStatement update_stm=conn.prepareStatement(query_statement);
if(param!=null)
for(int i=0;i<param.length;i++)
update_stm.executeUpdate();
update_stm.close();
conn.commit();
}
catch(Exception e){
System.out.println(e);
conn.rollback();
}
}
/* 删除数据记录 */
public void deleteRecord() throws SQLException,java.io.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();
delete_stm.close();
conn.commit();
}
catch(Exception e){
System.out.println(e);
conn.rollback();
}
}
}
查询界面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>


<jsp:useBean id="query" class="bean.QueryBean" scope="page" />

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>用javabean查询数据库</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 stringSqlString = "SELECT * FROM userTable";
try {
query.setConnection(
"com.microsoft.jdbc.sqlserver.SQLServerDriver",
"jdbc:microsoft:sqlserver://localhost:1433;DatebaseName=auto_parts",
"sa", "123");
query.setQuery_statement(stringSqlString);
rs = query.getResult();
} catch (Exception e) {
out.print("SQL异常!");
}
%>
<%

//显示查询结果
out.print("<table Border>");
out
.print("<tr><td colspan=8 align=center>用QueryBean查询得到的用户数据库</td></tr>");
out.print("<tr>");
out.print("<td width=100>" + "用户ID号");
out.print("<td width=50>" + "用户名");
out.print("<td width=100>" + "用户真实姓名");
out.print("<td width=50>" + "年龄");
out.print("<td width=50>" + "性别");
out.print("<td width=100>" + "联系地址");
out.print("<td width=100>" + "联系电话");
out.print("<td width=100>" + "添加时间");
out.print("</tr>");
while (rs.next()) {
out.print("<tr>");
out.print("<td>" + rs.getInt(1) + "</td>");
out.print("<td>" + rs.getString(2) + "</td>");
out.print("<td>" + rs.getString(3) + "</td>");
out.print("<td>" + rs.getInt("user_age") + "</td>");
out.print("<td>" + rs.getString("user_sex") + "</td>");
out.print("<td>" + rs.getString("user_adress") + "</td>");
out.print("<td>" + rs.getString("user_telephone") + "</td>");
out.print("<td>" + rs.getString("add_time") + "</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: An exception occurred processing JSP page /queryJavaBean.jsp at line 56

53: out.print("<td width=100>" + "鑱旂郴鐢佃瘽");
54: out.print("<td width=100>" + "娣诲姞鏃堕棿");
55: out.print("</tr>");
56: while (rs.next()) {
57: out.print("<tr>");
58: out.print("<td>" + rs.getInt(1) + "</td>");
59: out.print("<td>" + rs.getString(2) + "</td>");


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:505)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


root cause 

java.lang.NullPointerException
org.apache.jsp.queryJavaBean_jsp._jspService(queryJavaBean_jsp.java:121)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)


org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


note The full stack trace of the root cause is available in the Apache Tomcat/6.0.18 logs.


[解决办法]
java.lang.NullPointerException rs有值没
[解决办法]
jdbc:microsoft:sqlserver://localhost:1433;DatebaseName=auto_parts

我觉得应该是:

jdbc:sqlserver://localhost:1433;DatebaseName=auto_parts
[解决办法]
java.lang.NullPointerException
org.apache.jsp.queryJavaBean_jsp._jspService(queryJavaBean_jsp.java:121)
但是确实是这里报错,楼主在调试下
[解决办法]
我没试过在页面里写后台代码的习惯,所以看的比较迷茫,可能是你那个bean文件在页面没起到作用吧,所以后面才会报错,空指针异常,queryJavaBean_jsp,你好像没这个类吧
[解决办法]
空指针的问题
你可以是试试,打印出来rs.next()是什么值,试试
[解决办法]
rs是空的,在while哪里改成这样while(null != rs && rs.next())
[解决办法]
你到Tomcat的这个目录下面 work\Catalina\localhost下面找到queryJavaBean_jsp.java文件,看下第121行

把代码贴出来下


我的异常网推荐解决方案:An exception occurred processing JSP page,http://www.myexception.cn/j2se/33144.html
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html

热点排行