免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1310 | 回复: 2
打印 上一主题 下一主题

一个百思不解的问题? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-03-11 09:39 |只看该作者 |倒序浏览
我想输入用户名和密码进行数据库验证,但是我不输入也可以进去,数据库是连上了的,可以插入数据进去,一下是我的代码:package org.vcs.contract.jdbc;
import org.vcs.contract.exceptions.*;
import org.vcs.contract.dao.*;
import org.vcs.contract.dto.*;
import org.vcs.contract.factory.*;
import org.vcs.contract.jdbc.*;
import java.sql.Connection;
import java.sql.Types;
import java.util.Collection;
import org.apache.log4j.Logger;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
public class SysuserDaoImpl implements SysuserDao{
  /**
   * The factory class for this DAO has two versions of the create() method - one that
   takes no arguments and one that takes a Connection argument. If the Connection version
   is chosen then the connection will be stored in this attribute and will be used by all
   calls to this DAO, otherwise a new Connection will be allocated for each operation.
   */
  protected java.sql.Connection userConn;
  protected static final Logger logger = Logger.getLogger(SysuserDaoImpl.class);
  /**
         * All finder methods in this class use this SELECT constant to build their queries
         */
        protected final String SQL_SELECT = "SELECT userid,username,password,lastlogin FROM " + getTableName() + "";
        /**
         * Finder methods will pass this value to the JDBC setMaxRows method
         */
       private int maxRows;

       /**
        * SQL INSERT statement for this table
        */
       protected final String SQL_INSERT = "INSERT INTO " + getTableName() + " (username,password) VALUES ( ?,?)";
       /**
          * SQL UPDATE statement for this table
          */
         protected final String SQL_UPDATE = "UPDATE " + getTableName() + " SET jfbm = ?, jfmc = ? WHERE jfbm = ?";

         /**
          * SQL DELETE statement for this table
          */
         protected final String SQL_DELETE = "DELETE FROM " + getTableName() + " WHERE jfbm = ?";
         /**
          * SQL CheckSysuser statement for this table
          */
         protected final String SQL_SYSUSER="SELECT username,password"+getTableName()+"WHERE username=? and password=?";
         /**
          * Index of column jfbm
          */
         protected static final int COLUMN_JFBM = 1;

         /**
          * Index of column jfmc
          */
         protected static final int COLUMN_JFMC = 2;

         /**
          * Number of columns
          */
         protected static final int NUMBER_OF_COLUMNS = 2;

         /**
          * Index of primary-key column jfbm
          */
         protected static final int PK_COLUMN_JFBM = 1;
         /**
          * Method 'JfDaoImpl'
          *
          * @param userConn
          */
         public SysuserDaoImpl(final java.sql.Connection userConn)
         {
                 this.userConn = userConn;
         }
         /**
         * Method 'getTableName'
         *
         * @return String
         */
        public String getTableName()
        {
                return "Sysuser";
        }

        public  void  Insert(Sysuser dto)throws SysuserDaoException{
               long t1 = System.currentTimeMillis();
                // declare variables
                final boolean isConnSupplied = (userConn != null);
                Connection conn = null;
                PreparedStatement stmt = null;
                ResultSet rs = null;
                try {
                        // get the user-specified connection or get a connection from the ResourceManager
                        conn = isConnSupplied ? userConn : ResourceManager.getConnection();
                        conn.setAutoCommit(false);
                        stmt = conn.prepareStatement( SQL_INSERT );
                        //stmt.setString(1,dto.getUserid());
                        stmt.setString(1, dto.getUsername());
                        stmt.setString(2, dto.getPassword());
                        //stmt.setTimestamp(4, dto.getLastlogin()==null ? null : new java.sql.Timestamp( dto.getLastlogin().getTime()));
                        int rows=stmt.executeUpdate();
                        conn.commit();
                        long t2 = System.currentTimeMillis();
                        if (logger.isDebugEnabled()) {
                                logger.debug( rows + " rows affected (" + (t2-t1) + " ms)");
                        }




                }
                catch (SQLException _e) {
                        logger.error( "SQLException: " + _e.getMessage(), _e );
                        throw new SysuserDaoException( "SQLException: " + _e.getMessage(), _e );
                }
                catch (Exception _e) {
                        logger.error( "Exception: " + _e.getMessage(), _e );
                        throw new SysuserDaoException( "Exception: " + _e.getMessage(), _e );
                }
                finally {
                        ResourceManager.close(stmt);
                        if (!isConnSupplied) {
                                ResourceManager.close(conn);
                        }

                }

        }
        public boolean  CheckSysuser(Sysuser dto)throws SysuserDaoException{
                // declare variables
                final boolean isConnSupplied = (userConn != null);
                boolean tt;
                Connection conn = null;
                PreparedStatement stmt = null;
                ResultSet rs = null;

    try{
                  conn = isConnSupplied ? userConn : ResourceManager.getConnection();
                  conn.setAutoCommit(false);
                  stmt = conn.prepareStatement(SQL_SYSUSER);
                  stmt.setString(1,dto.getUsername());
                  stmt.setString(2,dto.getPassword());
                  rs=stmt.executeQuery();

                  //stmt.execute();
                  conn.commit();
                  if((rs.getString(1)!=null)&&(rs.getString(2)!=null)){
                     return true;
                  }else {
                    return false;
                  }
                  }catch(Exception e){
                  e.printStackTrace();
                }
      return false;
            
        }

  public SysuserDaoImpl() {

  }
}

package org.vcs.contract.struts.action;

import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.vcs.contract.struts.forms.LoginForm;
import org.apache.struts.action.Action;
import org.vcs.contract.factory.*;
import org.vcs.contract.dao.*;
import org.vcs.contract.dto.*;
public class Login extends Action {
  public ActionForward execute(ActionMapping mapping,
                               ActionForm form,
                               HttpServletRequest request,
                               HttpServletResponse response) throws Exception {
    LoginForm loginForm = (LoginForm) form;
    // create the DAO class
    SysuserDao dao = SysuserDaoFactory.create();
    Sysuser dto = new Sysuser();
    dto.setUsername(loginForm.getUsername());
    dto.setPassword(loginForm.getPassword());
    if(!dao.CheckSysuser(Sysuser)){
      return mapping.findForward("success");

    }
    else {
      return mapping.findForward("faliure");
    }
  }
}

论坛徽章:
0
2 [报告]
发表于 2006-03-11 09:47 |只看该作者
rs.next()

论坛徽章:
0
3 [报告]
发表于 2006-03-11 11:57 |只看该作者
代码没看.但解释下卡卡西的话
ResultSet的一个实例比如说叫rs中,除了记录之外还有两个光标位置,beforeFirst和afterLast,在第一行之前和最后一行之后,返回一个rs的时候光标默认是在beforeFirst的地方,所以需要rs.next()一下把光标移动到first的位置

另外提供个调试的思路:想不通的时候,可以把你认为可能出错的地方例如比较,两方面的值写到终端上看看嘛
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP