日期:2014-05-17  浏览次数:20738 次

ResultSet为空的问题
登录网站访问数据库的过程中……

Serevlet 代码如下




Java code



package ser;

import impl.AccountImpl;
import it.shopping.dao.IAccount;
import it.shopping.pojo.Account;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AccountSer extends HttpServlet
{
   
 
   private IAccount accountImpl = new AccountImpl();
   
   private static final long serialVersionUID = -3708568559174453120L;
   
   public AccountSer()
   {
       super();
   }
     
    public void destroy()
    {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {

    this.doPost(request, response);
    }

    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    { 
        
        String status = request.getParameter("status");
        if(status.equals("login"))
        {
        
        Account account = new Account();
        account.setAlogin(request.getParameter("alogin"));
        account.setApass(request.getParameter("apass"));
     
        
         account =accountImpl.queryAccount(account);
    
        
        
        if(account == null)
        {
            request.setAttribute("error", "登录失败");
            request.getRequestDispatcher("/alogin.jsp").forward(request, response);
            System.out.println("doPost is invoked");
        }
        
        else
        {
            
          request.getSession().setAttribute("account", account);
          response.sendRedirect("/t31/admin/index.jsp");
          
            
        }
        }
    }
        
 

    
    public void init() throws ServletException
    {
        // Put your code here
        System.out.println("init is invoked");
    }

}



访问数据库:

Java code

package impl;

import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


import utils.JdbcUtils;

import it.shopping.dao.IAccount;
import it.shopping.pojo.Account;




public class AccountImpl implements IAccount
{

    @Override
    public Account queryAccount(Account account) 
    {
        
    
         Account temp = null;
         Connection conn = null;
         PreparedStatement pre = null;
         String sql = "SELECT * FROM students WHERE alogin=? AND apass=?"; 
         ResultSet rs = null;
         
       try
       { 
         
        conn = JdbcUtils.getConnection();
        pre= conn.prepareStatement(sql);

        pre.setObject(1,"alogin");
        pre.setObject(2,"apass");
        System.out.println("pre is invoked");
        rs = pre.executeQuery();
        System.out.println(rs.getString(1));
        
        
         
        
         
        if(rs.next())
        {
             
             temp = new A