问题遇到的现象和发生背景
Javabean写学生增加时500空指针报错
问题相关代码,请勿粘贴截图
XSCJZJ_1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<jsp:useBean id="myDbBean" scope="page" class="example.jdbc.DbBean"></jsp:useBean>
<%request.setCharacterEncoding("UTF-8"); %>
<script type="text/javascript">
function check(theForm)
{
if(theForm.xh.value.length != 11)
{
alert("学号必须为11位!");
theForm.bh.focus();
return(false);
}
if(theForm.xm.value == "")
{
alert("请输入姓名!");
theForm.xm.focus();
return(false);
}
if(theForm.yw.value == "")
{
alert("请输入语文!");
theForm.yw.focus();
return(false);
}
if(theForm.sx.value == "")
{
alert("请输入数学!");
theForm.sx.focus();
return(false);
}
if(theForm.yy.value == "")
{
alert("请输入英语!");
theForm.yy.focus();
return(false);
}
return(True);
}
</script>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生增加</title>
</head>
<body>
<div align="center">
<p>学生增加</p>
<form id="form1" name="form1" method="post" action="XSCJZJ_2.jsp" onsubmit="return check(this)">
<table border="1">
<%
// 查询数据库
String sql = "select * from class order by bjbh"; //从数据库中选择字段
myDbBean.openConnection();
ResultSet rs = myDbBean.executeQuery(sql);
%>
<tr><td>班级</td>
<td><select name="bjbh">
<%
while(rs.next())
{
String bjbh = rs.getString("bjbh");
String bjmc = rs.getString("bjmc");
%>
<option value="<%= bjbh %>"><%= bjmc %></option>
<%
}
rs.close();
// 关闭数据库连接
myDbBean.closeConnection();
%>
</select>
</td></tr>
<tr><td>学号</td><td><input name="xh" type="text" id="xh"/></td></tr>
<tr><td>姓名</td><td><input name="xm" type="text" id="xm"/></td></tr>
<tr><td>语文</td><td><input name="yw" type="text" id="yw"/></td></tr>
<tr><td>数学</td><td><input name="sx" type="text" id="sx"/></td></tr>
<tr><td>英语</td><td><input name="yy" type="text" id="yy"/></td></tr>
</table>
<br>
<input name="submit" type="submit" value="确定" />
<input name="reset" type="reset" value="重置" />
</form>
</div>
</body>
</html>
DbBean.java
package example.jdbc;
import java.sql.*;
public class DbBean {
private Statement stmt = null;
private Connection conn = null;
ResultSet rs = null;
//构造函数
public DbBean(){}
//打开链接
public void openConnection() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=student";
String user = "sa";
String password = "123456";
conn = DriverManager.getConnection(url,user,password);
}catch(ClassNotFoundException e) {
System.err.println("openConn:" + e.getMessage());
}
catch(SQLException e) {
System.err.println("openConn:" + e.getMessage());
}
catch(Exception e) {
System.err.println("openConn:" + e.getMessage());
}
}
//执行查询类的SQL语句
public ResultSet executeQuery(String sql) {
rs = null;
try {
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery(sql);
}
catch(SQLException e){
System.out.println("executeQuery:" + e.getMessage());
}
return rs;
}
//执行更新类的SQL语句
public int executeUpdate(String sql){
int n = 0;
try{
stmt = conn.createStatement();
n = stmt.executeUpdate(sql);
}catch(Exception e){
System.out.print(e.toString());
}
return n;
}
//关闭连接
public void closeConnection(){
try{
if( rs != null){
rs.close();
}
}catch(SQLException e){
System.err.println("closeRs:" + e.getMessage());
}
try{
if( stmt != null){
stmt.close();
}
}catch(SQLException e){
System.err.println("closeStmt:" + e.getMessage());
}
try{
if( conn != null){
conn.close();
}
}catch(SQLException e){
System.err.println("closeConn:" + e.getMessage());
}
}
}
运行结果及报错内容
我的解答思路和尝试过的方法
DbBean.java添加了