困zZ 2022-05-15 11:15 采纳率: 75%
浏览 60
已结题

Javabean写学生增加时500空指针报错

问题遇到的现象和发生背景

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());
            }
        }
        

}


运行结果及报错内容

img

我的解答思路和尝试过的方法

DbBean.java添加了

我想要达到的结果
  • 写回答

2条回答 默认 最新

  • 拾光师 2022-05-15 11:38
    关注

    数据库连接没有连接上,还是null的

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月23日
  • 已采纳回答 5月15日
  • 修改了问题 5月15日
  • 创建了问题 5月15日

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器