JSP中连接数据库的问题
<%@ page contentType= "text/html; charset=gb2312 " language= "java " import= "java.sql.* " errorPage= " " %>
<%@ page import= "java.sql.* " %>
<%@ page import= "java.util.Date " %>
<%@ page import= "java.io.* " %>
<%!
String trans(String chi)
{
String result = null;
byte temp [];
try
{
temp=chi.getBytes( "iso-8859-1 ");
result = new String(temp);
}
catch(java.io.UnsupportedEncodingException e)
{
System.out.println (e.toString());
}
return result;
}
String trans(Object chi)
{
return trans(chi.toString());
}
%>
<jsp:useBean id= "db " scope= "page " class= "demo.openDB " />
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> 查看留言 </title>
<style type= "text/css ">
<!--
.STYLE1 {color: #FFFFFF}
body {
background-image: url(简单论坛/n3olcx.jpg);
}
.STYLE2 {
color: #F9F9F9;
font-size: 24px;
font-family: "华文隶书 ";
}
-->
</style>
</head>
<body>
<%
String id=request.getParameter( "id ");
String name=trans(request.getParameter( "name "));
String price=request.getParameter( "price ");
String producer=trans(request.getParameter( "producer "));
String descript=trans(request.getParameter( "descript "));
String type=trans(request.getParameter( "type "));
Connection conn=db.getConnection();
Statement stmt=conn.createStatement();
String sql= "select * from products ";
sql= "insert into products Values( ' "+id+ " ', ' "+type+ " ', ' "+name+ " ', ' "+producer+ " ', "+price+ ", ' "+descript+ " ') ";
stmt.executeUpdate(sql);
%>
<center>
<a href= "goodmain.jsp " class= "STYLE1 "> <span class= "STYLE2 "> 留言添加成功,返回 </span> </a>
</center>
</body>
</html>
其中的JAVABean是用来连接数据库的肯定是没问题的,但是,总是得不到结果,提示错误如下:
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:346)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
root cause
java.lang.NullPointerException
org.apache.jsp.addpro_005fdo_jsp.trans(addpro_005fdo_jsp.java:21)
org.apache.jsp.addpro_005fdo_jsp._jspService(addpro_005fdo_jsp.java:108)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:298)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
note The full stack trace of the root cause is available in the Apache Tomcat/5.0.25 logs.
请教哪个地方错了,帮忙修改一下,帮忙解决后立刻给分,谢了
[解决办法]
你或取得参数有没有问题?输出一下看看是不是有空参数。空参数的话,String trans(String chi)因为传入参数为null,会出空指针异常。另外,在insert上加上字段列表试一下,有可能是顺序问题。我也是菜鸟,互相帮助,呵呵
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html