JSP中数据库的连接问题 ( 积分: 100 )

Z

zjh0910

Unregistered / Unconfirmed
GUEST, unregistred user!
我编的是一段JSP与数据库进行连接,能实现对数据的读取添加删除操作,编写好程序能进行数据的读取但不能进行添加和删除,代码如下:
显示数据库内容
<%@ page contentType=&quot;text/html;
charset=gb2312&quot;
language=&quot;java&quot;
import=&quot;java.sql.*,java.util.*;&quot;errorPage=&quot;&quot;%>
<html>
<head>
<title>学籍管理系统</title>
</head>
<%! Connection conn;%>
<%! ResultSet rs;%>
<%! Statement stmt;%>
<%! int i;%>
<body bgcolor=&quot;#ffffff&quot;>
<div align=&quot;center&quot;><font color=&quot;#000000&quot;
size=&quot;5&quot;>学籍管理系统</font>
</div>
<table width=&quot;75%&quot;
border=&quot;1&quot;
cellspacing=&quot;1&quot;
cellpadding=&quot;1&quot;
align=&quot;center&quot;>
<tr>
<td width=16% align=center>学号</td>
<td width=16% align=center>姓名</td>
<td width=8% align=center>性别</td>
<td width=8% align=center>年龄</td>
<td width=16% align=center>籍贯</td>
<td width=12% align=center>院系</td>
<td width=12% align=center>更改</td>
<td width=12% align=center>删除</td>
</tr>
<%
try
{
Class.forName(&quot;com.microsoft.jdbc.sqlserver.SQLServerDriver&quot;).newInstance();
url=&quot;jdbc:microsoft:sqlserver://localhost:1027;DatabaseName=university&quot;;
conn=DriverManager.getConnection(url,&quot;admin&quot;,&quot;123456&quot;);

stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
sql=&quot;select * from student&quot;;
rs=stmt.executeQuery(sql);
while(rs.next()){
%>
<tr>
<td width=16% align=center><%=rs.getObject(1)%></td>
<td width=16% align=center><%=rs.getObject(2)%></td>
<td width=8% align=center><%=rs.getObject(3)%></td>
<td width=8% align=center><%=rs.getObject(4)%></td>
<td width=16% align=center><%=rs.getObject(5)%></td>
<td width=12% align=center><%=rs.getObject(6)%></td>
<td width=12% align=center><a href=&quot;change.jsp?xuehao=<%=rs.getObject(1)%>&quot;>
修改</a></td>
<td width=12% align=center><a href=&quot;del.jsp?xuehao=<%=rs.getObject(1)%>&quot;>
删除</a></td>
</tr>
<%
}
rs.close();
stmt.close();
conn.close();
}
catch(Exception e){
out.println(e);
}
%>
</table>
<div align=&quot;center&quot;><a href=&quot;insert.jsp&quot;>添加新记录</a></div>
</body>
</html>

接收添加的数据并检查数据的合法性,

<%@ page contentType=&quot;text/html;
charset=gb2312&quot;
language=&quot;java&quot;
import=&quot;java.sql.*,java.util.*&quot;
%>
<html>
<head>
<meta http-equiv=&quot;Content-Type&quot;
content=&quot;text/html;charset=gb2312&quot;>
<title>插入数据</title>
</head>
<body>
<%! String xuehao,name,sex,age,jiguan,department;%>
<%! int i;%>
<%! Connection conn;//定义数据库连接对象%>
<%! ResultSet rs;//定义结果集对象%>
<%! Statement stmt;//定义SQL语言对象%>
<%
xuehao=request.getParameter(&quot;xuehao&quot;);
xuehao=new String(name.getBytes(&quot;ISO8859_1&quot;),&quot;GBK&quot;);
name=request.getParameter(&quot;name&quot;).trim();
name=new String(name.getBytes(&quot;ISO8859_1&quot;));
sex=request.getParameter(&quot;sex&quot;);
sex=new String(sex.getBytes(&quot;ISO8859_1&quot;),&quot;GBK&quot;);
age=request.getParameter(&quot;age&quot;).trim();
age=new String(age.getBytes(&quot;ISO8859_1&quot;),&quot;GBK&quot;);
i=Integer.parseInt(age);
jiguan=request.getParameter(&quot;jiguan&quot;);
jiguan=new String(jiguan.getBytes(&quot;ISO8859_1&quot;),&quot;GBK&quot;);
department=request.getParameter(&quot;department&quot;);
department=new String(department.getBytes(&quot;ISO8859_1&quot;),&quot;GBK&quot;);
if(xuehao.length()!=7){
response.sendRedirect(&quot;err.jsp&quot;);
}
if(name.equals(&quot;&quot;)){
response.sendRedirect(&quot;err.jsp&quot;);
}
try
{
Class.forName(&quot;com.microsoft.jdbc.sqlserver.SQLServerDriver&quot;).newInstance();
String url=&quot;jdbc:microsoft:sqlserver://localhost:1027;DatabaseName=university&quot;;
Connection conn=DriverManager.getConnection(url,&quot;admin&quot;,&quot;123456&quot;);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql=&quot;select * from student WHERE xuehao='&quot;+xuehao+&quot;'&quot;;
ResultSet rs=stmt.executeQuery(sql);
if(rs.getRow()>0){
response.sendRedirect(&quot;err.jsp&quot;);
}
sql=&quot;insert into student(xuehao,name,sex,age,jiguan,dept)&quot;;
sql=sql+&quot;values('&quot;+xuehao+&quot;','&quot;+name+&quot;','&quot;+sex+&quot;','&quot;+i+&quot;','&quot;+jiguan+&quot;','&quot;+department+&quot;')&quot;;
stmt.executeUpdate(sql);
out.println(&quot;正在处理信息,请稍候.......&quot;);
rs.close();
stmt.close();
conn.close();
}
catch(Exception e){
response.sendRedirect(&quot;err.jsp&quot;);
}
%>
</body>
</html>

在我输入完添加数据按提交时就出现以下错误:
org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:372)
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:802)

root cause
java.lang.NullPointerException
org.apache.jsp.query_jsp._jspService(query_jsp.java:68)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
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:802)
请各位大侠帮帮忙,我是初学JSP对JSP的了解很少,我用J2SDK1.4.2+TOMCAT5.0+SQL2000公司让我在最短的时间内学会,但这个问题我找了好多资料都没有找到如果,请各位大侠帮帮忙,先在这谢谢各位了。
 
Z

zjh0910

Unregistered / Unconfirmed
GUEST, unregistred user!
我编的是一段JSP与数据库进行连接,能实现对数据的读取添加删除操作,编写好程序能进行数据的读取但不能进行添加和删除,代码如下:
显示数据库内容
<%@ page contentType=&quot;text/html;
charset=gb2312&quot;
language=&quot;java&quot;
import=&quot;java.sql.*,java.util.*;&quot;errorPage=&quot;&quot;%>
<html>
<head>
<title>学籍管理系统</title>
</head>
<%! Connection conn;%>
<%! ResultSet rs;%>
<%! Statement stmt;%>
<%! int i;%>
<body bgcolor=&quot;#ffffff&quot;>
<div align=&quot;center&quot;><font color=&quot;#000000&quot;
size=&quot;5&quot;>学籍管理系统</font>
</div>
<table width=&quot;75%&quot;
border=&quot;1&quot;
cellspacing=&quot;1&quot;
cellpadding=&quot;1&quot;
align=&quot;center&quot;>
<tr>
<td width=16% align=center>学号</td>
<td width=16% align=center>姓名</td>
<td width=8% align=center>性别</td>
<td width=8% align=center>年龄</td>
<td width=16% align=center>籍贯</td>
<td width=12% align=center>院系</td>
<td width=12% align=center>更改</td>
<td width=12% align=center>删除</td>
</tr>
<%
try
{
Class.forName(&quot;com.microsoft.jdbc.sqlserver.SQLServerDriver&quot;).newInstance();
url=&quot;jdbc:microsoft:sqlserver://localhost:1027;DatabaseName=university&quot;;
conn=DriverManager.getConnection(url,&quot;admin&quot;,&quot;123456&quot;);

stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
sql=&quot;select * from student&quot;;
rs=stmt.executeQuery(sql);
while(rs.next()){
%>
<tr>
<td width=16% align=center><%=rs.getObject(1)%></td>
<td width=16% align=center><%=rs.getObject(2)%></td>
<td width=8% align=center><%=rs.getObject(3)%></td>
<td width=8% align=center><%=rs.getObject(4)%></td>
<td width=16% align=center><%=rs.getObject(5)%></td>
<td width=12% align=center><%=rs.getObject(6)%></td>
<td width=12% align=center><a href=&quot;change.jsp?xuehao=<%=rs.getObject(1)%>&quot;>
修改</a></td>
<td width=12% align=center><a href=&quot;del.jsp?xuehao=<%=rs.getObject(1)%>&quot;>
删除</a></td>
</tr>
<%
}
rs.close();
stmt.close();
conn.close();
}
catch(Exception e){
out.println(e);
}
%>
</table>
<div align=&quot;center&quot;><a href=&quot;insert.jsp&quot;>添加新记录</a></div>
</body>
</html>

接收添加的数据并检查数据的合法性,

<%@ page contentType=&quot;text/html;
charset=gb2312&quot;
language=&quot;java&quot;
import=&quot;java.sql.*,java.util.*&quot;
%>
<html>
<head>
<meta http-equiv=&quot;Content-Type&quot;
content=&quot;text/html;charset=gb2312&quot;>
<title>插入数据</title>
</head>
<body>
<%! String xuehao,name,sex,age,jiguan,department;%>
<%! int i;%>
<%! Connection conn;//定义数据库连接对象%>
<%! ResultSet rs;//定义结果集对象%>
<%! Statement stmt;//定义SQL语言对象%>
<%
xuehao=request.getParameter(&quot;xuehao&quot;);
xuehao=new String(name.getBytes(&quot;ISO8859_1&quot;),&quot;GBK&quot;);
name=request.getParameter(&quot;name&quot;).trim();
name=new String(name.getBytes(&quot;ISO8859_1&quot;));
sex=request.getParameter(&quot;sex&quot;);
sex=new String(sex.getBytes(&quot;ISO8859_1&quot;),&quot;GBK&quot;);
age=request.getParameter(&quot;age&quot;).trim();
age=new String(age.getBytes(&quot;ISO8859_1&quot;),&quot;GBK&quot;);
i=Integer.parseInt(age);
jiguan=request.getParameter(&quot;jiguan&quot;);
jiguan=new String(jiguan.getBytes(&quot;ISO8859_1&quot;),&quot;GBK&quot;);
department=request.getParameter(&quot;department&quot;);
department=new String(department.getBytes(&quot;ISO8859_1&quot;),&quot;GBK&quot;);
if(xuehao.length()!=7){
response.sendRedirect(&quot;err.jsp&quot;);
}
if(name.equals(&quot;&quot;)){
response.sendRedirect(&quot;err.jsp&quot;);
}
try
{
Class.forName(&quot;com.microsoft.jdbc.sqlserver.SQLServerDriver&quot;).newInstance();
String url=&quot;jdbc:microsoft:sqlserver://localhost:1027;DatabaseName=university&quot;;
Connection conn=DriverManager.getConnection(url,&quot;admin&quot;,&quot;123456&quot;);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql=&quot;select * from student WHERE xuehao='&quot;+xuehao+&quot;'&quot;;
ResultSet rs=stmt.executeQuery(sql);
if(rs.getRow()>0){
response.sendRedirect(&quot;err.jsp&quot;);
}
sql=&quot;insert into student(xuehao,name,sex,age,jiguan,dept)&quot;;
sql=sql+&quot;values('&quot;+xuehao+&quot;','&quot;+name+&quot;','&quot;+sex+&quot;','&quot;+i+&quot;','&quot;+jiguan+&quot;','&quot;+department+&quot;')&quot;;
stmt.executeUpdate(sql);
out.println(&quot;正在处理信息,请稍候.......&quot;);
rs.close();
stmt.close();
conn.close();
}
catch(Exception e){
response.sendRedirect(&quot;err.jsp&quot;);
}
%>
</body>
</html>

在我输入完添加数据按提交时就出现以下错误:
org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:372)
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:802)

root cause
java.lang.NullPointerException
org.apache.jsp.query_jsp._jspService(query_jsp.java:68)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
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:802)
请各位大侠帮帮忙,我是初学JSP对JSP的了解很少,我用J2SDK1.4.2+TOMCAT5.0+SQL2000公司让我在最短的时间内学会,但这个问题我找了好多资料都没有找到如果,请各位大侠帮帮忙,先在这谢谢各位了。
 
D

duhl

Unregistered / Unconfirmed
GUEST, unregistred user!
本人也是初学JSP,不过感觉你这种构架方式不是很好,一般数据库连接是直接放在Class里面,而且Class也方便调试,(需要加一个main函数)。
 
O

ocean617

Unregistered / Unconfirmed
GUEST, unregistred user!
问题很简单。你的参数未做null检查
 
顶部