我在写一个学生成绩管理系统~
但是登陆时
始终不能和数据库联上~`

解决方案 »

  1.   

    /**
       * 返回唯一实例.如果是第一次调用此方法,则创建实例
       * @return DBConnBean 唯一实例
       */
      static synchronized public DBConnBean getInstance() {
        if (instance == null) {
          instance = new DBConnBean();
        }
        clients++;
        return instance;
      }    /**
       * 获得一个可用的(空闲的)连接.如果没有可用连接,且已有连接数小于最大连接数
       * 限制,则创建并返回新连接
       *
       * @param name 在属性文件中定义的连接池名字
       * @return Connection 可用连接或null
       */
      public Connection getConnection(String name) {
        DBConnectionPool pool = (DBConnectionPool) pools.get(name);
        if (pool != null) {
          return pool.getConnection();
        }
        return null;
      }
      ObjDBConnBean=getInstance();
      Connection Conn=ObjDBConnBean.getConnection("goa");
      if (Conn==null) {
        response.sendRedirect("../index.jsp?MessageType=98");
        return;
      }else{
        ResultSet rs = null;
        Statement stmt = null;
        try {
          stmt = Conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
          strSql = "select PASSWORD,DEPTNAME,DEPTID,EMPLOYEE,EMPLOYEENAME,SUPERUSER";
          strSql = strSql + " from T_USERMAIN ";      rs = stmt.executeQuery(strSql);
      

  2.   

    登陆页面
    <html>
    <head>
    <title>主页面</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    </head>
    <body>
    <br><br>
    <center><img src="images/tree.jpg" width="500" height="300"></center> 
          <form name="myform" method="post" action="chklogin.jsp">
            <table width="23%" border="0" align="center" bgcolor="efefef">
              <tr>
                <td><br></td>
              </tr>  
              <tr>
                <td width="37%" align="center">用户:</td>
                <td width="63%" align="center">
                  <input type="text" name="username" size=14>
                </td>
              </tr>
              <tr>
                <td width="37%" align="center">密码:</td>
                <td width="63%" align="center">
                  <input type="password" name="password" size=14>
                </td>
              </tr>
              <tr>
                <td colspan=2 align="center"><br><a href="javascript:myform.submit();">登陆</a>
                    &nbsp&nbsp&nbsp&nbsp&nbsp;<a href="register.jsp">新用户注册</a>
                </td>
             </tr>
             <tr>
                <td><br></td>
              </tr>  
            </table>
          </form>
    </body>
    </html>登陆验证<html>
    <head>
    <title>登陆验证</title>
    </head>
    <%@page import="java.util.*, java.sql.*" contentType="text/html;charset=GB2312" %>
    <jsp:useBean id="pool" scope="application" class="connection.ConnPool"/>
    <body>
    <%
      boolean success=false; //是否能够登陆
      boolean valid=true; //是否重复登陆
      String username=new String(request.getParameter("username").getBytes("ISO8859_1"),"GBK");
      String password=new String(request.getParameter("password").getBytes("ISO8859_1"),"GBK");
      int rankid=0;
      //加载数据库驱动并用连接池连结数据库
      String sql="";
      Connection con = null;
      try{
          if (pool.getDriver()==null){
        pool.setDriver("sun.jdbc.odbc.JdbcOdbcDriver");
        pool.setURL("jdbc:odbc:MyForum");
        pool.setSize(5);
        pool.initializePool();
      }
          con = pool.getConnection();
          Statement statement = con.createStatement();
          sql="select * from Users where UserName='"+username+"' and Password='"+password+"'";
          ResultSet rs =statement.executeQuery(sql);
          if (rs.next()){
                   success=true;
                   rankid=rs.getInt("Rank");
           }else{
              success=false;
           }
      //释放数据库连结
          pool.releaseConnection(con);
      }catch (Exception e){
       out.println(e.getMessage());
       }
      //根据登陆结果判断
      Vector UserList=(Vector)session.getAttribute("userlist");
      if(UserList==null){
                         UserList=new Vector();
                        }
       if (success){ 
           session.setAttribute("username",username);
           UserList.addElement(username);
           //判断同一用户名是否重复登陆
           for(int i=0;i<UserList.size()-1;i++)
           {
              if(username.equals(UserList.elementAt(i).toString()))
              {
                  UserList.removeElement(username); 
                  valid=false; %><h3>用户 <%=username%> 已经登陆,您不要再尝试用此用户名登陆了!</h3>
            <%}
           } 
           if(valid)
           {           
               session.setAttribute("userlist",UserList);    
       session.setAttribute("username",username);            
               response.sendRedirect("listboard.jsp?rankid="+String.valueOf(rankid));                              
           }    
       }else{%>
              <h3>用户名或密码有误,请重新登录!</h3>
     <%}%>
    <a href="javascript:history.back();">返回</a>
    </body>
    </html>
    还需要一个连接bean,可以自己写了