大家快帮忙,我很着急。一个错误提示

黑兵 2004-10-01 05:02:40
文件内容
<%@ page contentType="text/html; charset=GB2312" %>
<%@ page language="java" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*"%>
<jsp:useBean id="myconn" class="dulebean.conn" scope="application"/>
<jsp:useBean id="myuser" class="dulebean.myuser" scope="application"/>
<%
//定义上页传过来的变量接收变量
String s_xm=new String(request.getParameter("xm").getBytes("8859_1"));
String s_nr=new String(request.getParameter("nr1").getBytes("8859_1"));
//写入数据库
String sql = "insert into dule_jcsj(lb,nr) values('"+s_xm+"','"+s_nr+"')";
ResultSet rs = myconn.executeQuery(sql);
rs.close();
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>基础数据管理</title>
</head>
<body background="images/bg-1.gif">
<script language=javascript>
window.location="msg.jsp?xx=您成功地添加了一条基础信息!&cc=jcsj"
</script>
</body>

</html>
查看表时,发现数据已经添加进去了。可却总是有错误提示如下:
HTTP Status 500 -

--------------------------------------------------------------------------------

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.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.jcsj1_jsp._jspService(jcsj1_jsp.java:70)
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
...全文
246 55 打赏 收藏 转发到动态 举报
写回复
用AI写文章
55 条回复
切换为时间正序
请发表友善的回复…
发表回复
禽兽v5 2004-10-01
  • 打赏
  • 举报
回复
1.删除(也是使用executeUpdate,因为不需要返回ResultSet)
String sql = "delete dule_temp where xx = '" + xx + "'";

count = CONN.executeUpdate(sql);
2.更新同上。

insert, update, delete三个操作都是不返回结果集(ResultSet)的,他们只需要知道成功操作了几条纪录。

3.查找(使用executeQuery,因为需要返回结果集)
主要为了封装的目的,不能返回ResultSet,因此返回一个元素为Map的List对象。使用时有两种使用方法:

// 使用前记得import java.util.*;

List list = SqlWrapper.executeQuery(sql);

// 方法一:按名字取值
for ( int i = 0; i < list.size(); i++ )
{
Map map = (HashMap)list.get(i);

out.println((String)mag.get("xx"));
}

// 方法二:遍历取值
for ( int i = 0; i < list.size(); i++ )
{
Map map = (HashMap)list.get(i);

for (Iterator it = map.keySet().iterator(); it.hasNext();)
{
String column_name = (String)it.next();
out.println("column name = " + column_name);
out.println("column value = " + (String)map.get(column_name));
}
}
禽兽v5 2004-10-01
  • 打赏
  • 举报
回复
正在请求 621150
黑兵 2004-10-01
  • 打赏
  • 举报
回复
你QQ多少?
我的QQ:621150 或者 QQ:2587660
黑兵 2004-10-01
  • 打赏
  • 举报
回复
好用了。万分感谢。
现在要
1.删除记录时调用语句的例子
2.更新记录时调用语句的例子
3.查找记录时调用语句的例子
==============
太感激了。。。。
禽兽v5 2004-10-01
  • 打赏
  • 举报
回复
上QQ, 把工作目录发给我如何?
黑兵 2004-10-01
  • 打赏
  • 举报
回复
conn改成大写 CONN还是一样,错误还在那里
禽兽v5 2004-10-01
  • 打赏
  • 举报
回复
conn改成大写 CONN试试。
黑兵 2004-10-01
  • 打赏
  • 举报
回复
现在是
HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 17 in the jsp file: /jcsj1.jsp

Generated servlet error:
[javac] Compiling 1 source file

D:\tomcat\work\Catalina\localhost\dule\org\apache\jsp\jcsj1_jsp.java:70: cannot resolve symbol
symbol : variable conn
location: class org.apache.jsp.jcsj1_jsp
count = conn.executeUpdate(sql);
^
1 error


org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:127)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:351)
org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:415)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:458)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:552)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:291)
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.

黑兵 2004-10-01
  • 打赏
  • 举报
回复
现在的出错情况如上。全是按您说的做的
黑兵 2004-10-01
  • 打赏
  • 举报
回复
HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: /jcsj1.jsp(6,23) equal symbol expected
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:83)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:402)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:126)
org.apache.jasper.compiler.Parser.parseAttribute(Parser.java:238)
org.apache.jasper.compiler.Parser.parseAttributes(Parser.java:188)
org.apache.jasper.compiler.Parser.parsePageDirective(Parser.java:393)
org.apache.jasper.compiler.Parser.parseDirective(Parser.java:512)
org.apache.jasper.compiler.Parser.parseElements(Parser.java:1577)
org.apache.jasper.compiler.Parser.parse(Parser.java:171)
org.apache.jasper.compiler.ParserController.parse(ParserController.java:247)
org.apache.jasper.compiler.ParserController.parse(ParserController.java:149)
org.apache.jasper.compiler.ParserController.parse(ParserController.java:135)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:237)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:456)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:552)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:291)
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.
禽兽v5 2004-10-01
  • 打赏
  • 举报
回复
在第59行,记得要给java文件加package代码--如果你要把它放在dulebean目录的话。
禽兽v5 2004-10-01
  • 打赏
  • 举报
回复
写错,改成

rs=stmt.executeQuery(sql);
黑兵 2004-10-01
  • 打赏
  • 举报
回复
conn.java:59: incompatible types
found :int
required:java.sql.ResultSet
rs=stmt.executeUpdate(sql);
^
yaray 2004-10-01
  • 打赏
  • 举报
回复
String s_xm=new String(request.getParameter("xm").getBytes("8859_1"));

对获得的字符串一定要先判断是否为null值,再进行其他操作!

以上修改为:
String xm = request.getParameter("xm");
xm = (xm!=null) ? new String(xm.getBytes("8859_1")) : "默认值";//默认值 视自己的情况而定
waistcoat 2004-10-01
  • 打赏
  • 举报
回复
回复满了,换马甲。

调用也改一下:

<%@ page contentType="text/html; charset=GB2312" language="java" %>
<%@ page import="java.util.*%>
<%@ page import="dulebean.*%>
<%
int count = 0;

//定义上页传过来的变量接收变量
String xx = request.getParameter("xm") == null ? "" : new String(request.getParameter("xm").getBytes("8859_1"));
String yy = request.getParameter("nr") == null ? "" : new String(request.getParameter("nr").getBytes("8859_1"));

String sql = "insert into dule_temp(xx,yy) values('" + xx + "','" + yy + "')";

count = CONN.executeUpdate(sql);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>基础数据管理</title>
</head>
<body background="images/bg-1.gif">
<script language=javascript>
window.location="msg.jsp?xx=您成功地添加了<%=count%>条基础信息!&cc=jcsj"
</script>
</body>
</html>
禽兽v5 2004-10-01
  • 打赏
  • 举报
回复
那就这样写:

import java.sql.*;
import java.util.*;

public class CONN
{
public static Connection openConnection() throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
return DriverManager.getConnection("jdbc:odbc:Driver={SQL Server};Server=localhost;uid=dule;pwd=huguangtao;Database=dule");
}

public static void closeConnection(Connection conn) throws Exception
{
if ( conn != null )
{
conn.close();
}
}

public static int executeUpdate(String sql) throws Exception
{
int count = 0;

Connection conn = null;
Statement stmt = null;

try
{
conn = openConnection();
stmt = conn.createStatement();

count = stmt.executeUpdate(sql);
}
catch ( Exception e )
{
throw e;
}
finally
{
closeConnection(conn);
}

return count;
}

public static List executeQuery(String sql) throws Exception
{
List list = new ArrayList();

Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

try
{
conn = openConnection();
stmt = conn.createStatement();
rs = stmt.executeUpdate(sql);

ResultSetMetaData rsmd = rs.getMetaData();

while ( rs.next() )
{
Map map = new HashMap();

for ( int i = 0; i < rsmd.getColumnCount(); i++ )
{
map.put(rsmd.getColumnName(i), rs.getString(i));
}

list.add(map);
}

}
catch ( Exception e )
{
e.printStackTrace();
}
finally
{
if ( rs != null ) rs.close();
closeConnection(conn);
}

return list;
}
}
禽兽v5 2004-10-01
  • 打赏
  • 举报
回复
上面的代码保存到DbPool.java

把下面的代码保存到tomcat/webapps/dd/WEB-INF/classes/db目录下的SqlWrapper.java

// 数据库操作封装
// 我加了package代码
package db;

import java.sql.*;
import java.util.*;

public class SqlWrapper
{
public static int executeUpdate(String sql) throws Exception
{
int count = 0;

Connection conn = null;
Statement stmt = null;

try
{
conn = DbPool.openConnection();
stmt = conn.createStatement();

count = stmt.executeUpdate(sql);
}
catch ( Exception e )
{
throw e;
}
finally
{
DbPool.closeConnection(conn);
}

return count;
}

public static List executeQuery(String sql) throws Exception
{
List list = new ArrayList();

Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

try
{
conn = DbPool.openConnection();
stmt = conn.createStatement();
rs = stmt.executeUpdate(sql);

ResultSetMetaData rsmd = rs.getMetaData();

while ( rs.next() )
{
Map map = new HashMap();

for ( int i = 0; i < rsmd.getColumnCount(); i++ )
{
map.put(rsmd.getColumnName(i), rs.getString(i));
}

list.add(map);
}

}
catch ( Exception e )
{
e.printStackTrace();
}
finally
{
if ( rs != null ) rs.close();
DbPool.closeConnection(conn);
}

return list;
}
}
禽兽v5 2004-10-01
  • 打赏
  • 举报
回复
那一个一个来:

把下面的代码保存到tomcat/webapps/dd/WEB-INF/classes/db目录

// 数据库连接池
// 我加了package代码
package db;

import java.sql.*;

public class DbPool
{
public static Connection openConnection() throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
return DriverManager.getConnection("jdbc:odbc:Driver={SQL Server};Server=localhost;uid=dule;pwd=huguangtao;Database=dule");
}

public static void closeConnection(Connection conn) throws Exception
{
if ( conn != null )
{
conn.close();
}
}
}
黑兵 2004-10-01
  • 打赏
  • 举报
回复
你帮我改成CONN.JAVA吧。
黑兵 2004-10-01
  • 打赏
  • 举报
回复
我编译哪个文件?
加载更多回复(35)

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧