我在进行数据库登陆验证时出现下列异常信息:
type: Exception report
message :description :The server encountered an internal error () that prevented it from fulfilling this request.exception :java.lang.NullPointerException
 loginwithdb.UserBean.validate(UserBean.java:15)
 loginwithdb.loginServlet.doGet(loginServlet.java:25)
 loginwithdb.loginServlet.doPost(loginServlet.java:36)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:802)note: The full stack trace of the root cause is available in the Apache Tomcat/5.0.27 logs.服务器是tomcat5.0,代码反复写了很多遍,应该没问题。不知是不是配置的问题,我的开发工具是JBuilder2005,在JDK和服务器里都添加了连接数据库的jar包,请高手给俺解答下,不胜感激!

解决方案 »

  1.   

    第一段代码:
    package loginwithdb;import java.sql.*;public class UserBean {
        private Connection conn;
        private PreparedStatement pstmt;
        private ResultSet rs;
        public UserBean() {
            conn = DBConn.getConn();
        }    public boolean validate(String username, String pwd) {
            String sqlStr = "select * from userdb where username=? pwd=?";
            try {
                pstmt = conn.prepareStatement(sqlStr);
                pstmt.setString(1, username);
                pstmt.setString(2, pwd);
                rs = pstmt.executeQuery();
                if (rs.next()) {
                    return true;
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            } finally {
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException ex1) {
                    }
                }
            }
            return false;
        }
    }第二段代码:
    package loginwithdb;import java.sql.*;public class DBConn {
        public static Connection getConn() {
            Connection conn = null;
            try {
                Class.forName("com.microsoft.jdbc.sqlserver.sqlserverdriver");
                String url =
                        "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=northwind";
                conn = DriverManager.getConnection(url, "sa", "");
            } catch (SQLException ex) {
                ex.printStackTrace();
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }
            return conn;
        }
    }
      

  2.   

    为了这段小程序俺重装了JBuilder,重装了SQL,最后还重装了系统。
      

  3.   

    代码没问题,UserBean.java:15 这个类的15行是哪个?
      

  4.   

    Serverlet中的get方法的代码是:
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws
                ServletException, IOException {
            response.setContentType(CONTENT_TYPE);
            response.setCharacterEncoding("GBK");
            request.setCharacterEncoding("GBK");
            HttpSession session = request.getSession();
            String username = request.getParameter("username");
            String pwd = request.getParameter("pwd");
            UserBean userBean = new UserBean();
            if(userBean.validate(username,pwd)){
                session.setAttribute("username",username);
                response.sendRedirect("welcome.jsp");
            }else{
                response.sendRedirect("login.jsp");
            }
        }
      

  5.   

    第15行代码是: pstmt = conn.prepareStatement(sqlStr);
      

  6.   

    select * from userdb where username=? pwd=?";这个sql 写的不对,少and ,你跟踪一下看运行到哪出的问题。
      

  7.   

    在DBConn中测试一下是否能够得到数据
    可能是没有得到conn=null
      

  8.   

    conn没取到, 你print一下你的conn看看
      

  9.   

    果然是DBConn中返回的conn为null,这个问题该怎么解决?
      

  10.   

    1.把sql2000的三个jar包放到环境变量中
    2.确定数据库名称,用户名和密码为"northwind","sa",""