Z
zjh0910
Unregistered / Unconfirmed
GUEST, unregistred user!
我编的是一段JSP与数据库进行连接,能实现对数据的读取添加删除操作,编写好程序能进行数据的读取但不能进行添加和删除,代码如下:
显示数据库内容
<%@ page contentType="text/html;
charset=gb2312"
language="java"
import="java.sql.*,java.util.*;"errorPage=""%>
<html>
<head>
<title>学籍管理系统</title>
</head>
<%! Connection conn;%>
<%! ResultSet rs;%>
<%! Statement stmt;%>
<%! int i;%>
<body bgcolor="#ffffff">
<div align="center"><font color="#000000"
size="5">学籍管理系统</font>
</div>
<table width="75%"
border="1"
cellspacing="1"
cellpadding="1"
align="center">
<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("com.microsoft.jdbc.sqlserver.SQLServerDriver".newInstance();
url="jdbc:microsoft:sqlserver://localhost:1027;DatabaseName=university";
conn=DriverManager.getConnection(url,"admin","123456"
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
sql="select * from student";
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="change.jsp?xuehao=<%=rs.getObject(1)%>">
修改</a></td>
<td width=12% align=center><a href="del.jsp?xuehao=<%=rs.getObject(1)%>">
删除</a></td>
</tr>
<%
}
rs.close();
stmt.close();
conn.close();
}
catch(Exception e){
out.println(e);
}
%>
</table>
<div align="center"><a href="insert.jsp">添加新记录</a></div>
</body>
</html>
接收添加的数据并检查数据的合法性,
<%@ page contentType="text/html;
charset=gb2312"
language="java"
import="java.sql.*,java.util.*"
%>
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html;charset=gb2312">
<title>插入数据</title>
</head>
<body>
<%! String xuehao,name,sex,age,jiguan,department;%>
<%! int i;%>
<%! Connection conn;//定义数据库连接对象%>
<%! ResultSet rs;//定义结果集对象%>
<%! Statement stmt;//定义SQL语言对象%>
<%
xuehao=request.getParameter("xuehao"
xuehao=new String(name.getBytes("ISO8859_1","GBK"
name=request.getParameter("name".trim();
name=new String(name.getBytes("ISO8859_1");
sex=request.getParameter("sex"
sex=new String(sex.getBytes("ISO8859_1","GBK"
age=request.getParameter("age".trim();
age=new String(age.getBytes("ISO8859_1","GBK"
i=Integer.parseInt(age);
jiguan=request.getParameter("jiguan"
jiguan=new String(jiguan.getBytes("ISO8859_1","GBK"
department=request.getParameter("department"
department=new String(department.getBytes("ISO8859_1","GBK"
if(xuehao.length()!=7){
response.sendRedirect("err.jsp"
}
if(name.equals(""){
response.sendRedirect("err.jsp"
}
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver".newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1027;DatabaseName=university";
Connection conn=DriverManager.getConnection(url,"admin","123456"
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from student WHERE xuehao='"+xuehao+"'";
ResultSet rs=stmt.executeQuery(sql);
if(rs.getRow()>0){
response.sendRedirect("err.jsp"
}
sql="insert into student(xuehao,name,sex,age,jiguan,dept)";
sql=sql+"values('"+xuehao+"','"+name+"','"+sex+"','"+i+"','"+jiguan+"','"+department+"')";
stmt.executeUpdate(sql);
out.println("正在处理信息,请稍候......."
rs.close();
stmt.close();
conn.close();
}
catch(Exception e){
response.sendRedirect("err.jsp"
}
%>
</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公司让我在最短的时间内学会,但这个问题我找了好多资料都没有找到如果,请各位大侠帮帮忙,先在这谢谢各位了。
显示数据库内容
<%@ page contentType="text/html;
charset=gb2312"
language="java"
import="java.sql.*,java.util.*;"errorPage=""%>
<html>
<head>
<title>学籍管理系统</title>
</head>
<%! Connection conn;%>
<%! ResultSet rs;%>
<%! Statement stmt;%>
<%! int i;%>
<body bgcolor="#ffffff">
<div align="center"><font color="#000000"
size="5">学籍管理系统</font>
</div>
<table width="75%"
border="1"
cellspacing="1"
cellpadding="1"
align="center">
<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("com.microsoft.jdbc.sqlserver.SQLServerDriver".newInstance();
url="jdbc:microsoft:sqlserver://localhost:1027;DatabaseName=university";
conn=DriverManager.getConnection(url,"admin","123456"
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
sql="select * from student";
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="change.jsp?xuehao=<%=rs.getObject(1)%>">
修改</a></td>
<td width=12% align=center><a href="del.jsp?xuehao=<%=rs.getObject(1)%>">
删除</a></td>
</tr>
<%
}
rs.close();
stmt.close();
conn.close();
}
catch(Exception e){
out.println(e);
}
%>
</table>
<div align="center"><a href="insert.jsp">添加新记录</a></div>
</body>
</html>
接收添加的数据并检查数据的合法性,
<%@ page contentType="text/html;
charset=gb2312"
language="java"
import="java.sql.*,java.util.*"
%>
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html;charset=gb2312">
<title>插入数据</title>
</head>
<body>
<%! String xuehao,name,sex,age,jiguan,department;%>
<%! int i;%>
<%! Connection conn;//定义数据库连接对象%>
<%! ResultSet rs;//定义结果集对象%>
<%! Statement stmt;//定义SQL语言对象%>
<%
xuehao=request.getParameter("xuehao"
xuehao=new String(name.getBytes("ISO8859_1","GBK"
name=request.getParameter("name".trim();
name=new String(name.getBytes("ISO8859_1");
sex=request.getParameter("sex"
sex=new String(sex.getBytes("ISO8859_1","GBK"
age=request.getParameter("age".trim();
age=new String(age.getBytes("ISO8859_1","GBK"
i=Integer.parseInt(age);
jiguan=request.getParameter("jiguan"
jiguan=new String(jiguan.getBytes("ISO8859_1","GBK"
department=request.getParameter("department"
department=new String(department.getBytes("ISO8859_1","GBK"
if(xuehao.length()!=7){
response.sendRedirect("err.jsp"
}
if(name.equals(""){
response.sendRedirect("err.jsp"
}
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver".newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1027;DatabaseName=university";
Connection conn=DriverManager.getConnection(url,"admin","123456"
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from student WHERE xuehao='"+xuehao+"'";
ResultSet rs=stmt.executeQuery(sql);
if(rs.getRow()>0){
response.sendRedirect("err.jsp"
}
sql="insert into student(xuehao,name,sex,age,jiguan,dept)";
sql=sql+"values('"+xuehao+"','"+name+"','"+sex+"','"+i+"','"+jiguan+"','"+department+"')";
stmt.executeUpdate(sql);
out.println("正在处理信息,请稍候......."
rs.close();
stmt.close();
conn.close();
}
catch(Exception e){
response.sendRedirect("err.jsp"
}
%>
</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公司让我在最短的时间内学会,但这个问题我找了好多资料都没有找到如果,请各位大侠帮帮忙,先在这谢谢各位了。