大家帮我看看,这个JAVABEAN和check两个代码,是关于连接数据库和登陆验证的,老是无法正确运行。有人看了说是check中的if(rs.next())有问题,哪位高人能帮我解答一下。JAVABEAN:package com.bwm.db;
import java.sql.*;
public class Condb{
   String servername="localhost";
   String drivername="com.microsoft.jdbc.sqlserver.SQLServerDriver";
   String url="jdbc:microsoft:sqlserver://servername:1433;DatabaseName=db_Scenince;user=sa;password=123";
   Connection con=null;
   ResultSet rs=null;
   public Condb(){
       try{
           Class.forName(drivername);
       }catch(java.lang.ClassNotFoundException e){
           System.err.println(e.getMessage());
       }
   }
   public ResultSet executeQuery(String sql){
       try{
           con=DriverManager.getConnection(url);
           Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
           rs=stmt.executeQuery(sql);
       }catch(SQLException ex){
           System.err.println(ex.getMessage());
       }
       return rs;
   }
   public int executeUpdate(String sql){
       int result=0;
       try{
           con=DriverManager.getConnection(url);
           Statement stmt=con.createStatement();
           result=stmt.executeUpdate(sql);
       }catch(SQLException ex){
           System.err.println(ex.getMessage());
       }
       return result;
   }
   public void close(){
       try{
           if(rs!=null){
               rs.close();
           }
       }catch(Exception e){
           System.err.println(e.getMessage());
       }
       try{
           if(con!=null){
               con.close();
           }
       }catch(Exception e){
           System.err.println(e.getMessage());
       }
   }
}

解决方案 »

  1.   

    check代码:<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" %>
    <% request.setCharacterEncoding("gb2312"); %>
    <%@ page import="com.bwm.page.Show"%>
    <%@ page import="com.bwm.db.Condb"%>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>无标题文档</title>
    </head><body>
    <%
       Condb con=new Condb();
       //Connection conn=con.getConnection();
       Show show=new Show();
       String name=request.getParameter("name");
       String password=request.getParameter("password");
       String sql="select * from tb_Person where Username='"+name+"' and Password='"+password+"'";
       ResultSet rs=con.executeQuery(sql);
       if(rs.next()){
       String strsql=rs.getString(1);
           session.setAttribute("name",name);
           session.setAttribute("password",password);
           session.setAttribute("groupid",strsql);
           response.sendRedirect("index.htm");
       }else{
           out.print(show.errorBox("你输入的用户名或密码有误","错误信息"));
           out.close();
    }    
    //if(conn!=null){
    //    conn.close();
    //}
    con.close();
    %></body>
    </html>
      

  2.   

    说是空指针错误,type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:358)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
    root cause java.lang.NullPointerException
      org.apache.jsp.user.check_jsp._jspService(check_jsp.java:67)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
    note The full stack trace of the root cause is available in the Tomcat logs
      

  3.   

    楼主的url写的不对吧,servername是作为变量的去连接字符串,不是字符串的一部分.
    这样肯定找不到servername服务器.
    url="jdbc:microsoft:sqlserver://"+"servername"+":1433;DatabaseName=db_Scenince;user=sa;password=123";
      

  4.   

    直接把servername改成127.0.0.1试试
    url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=db_Scenince;user=sa;password=123";