使用JDBC连接MySQL数据库时连接失败,并返回java.lang.NullPointerException异常,为什么会发生空指针异常?求大神指点~~
<body>
        <%
            String userName=
                    new String(request.getParameter("userName").getBytes("ISO-8859-1"),"UTF-8");
            String password=
                    new String(request.getParameter("password").getBytes("ISO-8859-1"),"UTF-8");
            Connection con=null;
            Statement st=null;
            ResultSet rs=null;
            if(userName.equals(""))
            {
                response.sendRedirect("login.jsp");
            }
            try{
                Class.forName("com.mysql.jdbc.Driver");
                String url="jdbc:mysql://localhost:3306/eims?useUnicode=true&characterEncoding=gbk";
                con=DriverManager.getConnection(url, "root", "6785098");
                st = con.createStatement();
                String query = "select * from user where userName = '" + userName + "'";
                rs = st.executeQuery(query);
                if (rs.next())
                {
                    String query2 = "select * from user where password = '" + password + "'";
                    rs = st.executeQuery(query2);
                    if (rs.next())
                    {
                        response.sendRedirect("main/main.jsp");
                    } else {
                        response.sendRedirect("login.jsp");
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                rs.close();
                st.close();
                con.close();
            }
        %>
    </body>
上述代码在调试的时候先进入try代码块,但是走到Class.forName("com.mysql.jdbc.Driver");这一行时驱动貌似没有加载,然后程序就直接跳过try块中的剩余代码,直接进入了catch代码块,再往后走到rs.close();时就发生空指针异常了,应该是rs为空导致的。初步判断是jdbc连接数据库部分代码没有执行,但是不知道为什么不执行?
这个项目名为EIMS,我在另一个项目(ch01)中用同样的jdbc代码连接MySQL是可以连接成功的,在mysql数据库中我是新建了两个连接,连接名分别对应这两个web项目名。请各位大神帮忙看下吧,不胜感激…