日期:2014-05-20  浏览次数:20892 次

学了几个月java ,头一次被整的要疯掉了。
有时候有数据,有时候又没有,现在连登录都不可以了。ConDB没有改动。数据库可以连上,

Java code

package  com.nong;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
public class LoginCL extends HttpServlet{

public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{  
    String u=req.getParameter("username");
    String p=req.getParameter("passwd");
    //UserBeanCL  ubc=new UserBeanCL();
    ConDB db=new ConDB();
    db.closeall();//为了确保上一次没有断开的连接断开
    if(db.isExist("select * from users"))
    {   
        
        res.setContentType("text/html;charset=gbk");
        PrintWriter pw=res.getWriter();
        ConDB db1=new ConDB();
        
        try{
    
        ResultSet rs1=db1.getRset("select * from student");
            while(rs1.next())
            {
            pw.println("aa");
            }
            
            
               try{
                rs1.close();
                  }
               catch(Exception e)
               {e.printStackTrace();}
        }
        
        catch(Exception e)
        {
            e.printStackTrace();
        
        }

        db1.closeall();
        db.closeall();
            
            
            
        
        
        
        /*
        
        
        
        
        Cookie name=new Cookie("myname",u);
        Cookie pass=new Cookie("mypasswd",p);
        name.setMaxAge(14*24*3600);
        pass.setMaxAge(14*24*3600);
        res.addCookie(name);
        res.addCookie(pass);
        
        HttpSession hs=req.getSession(true);
        hs.setMaxInactiveInterval(14*24*3600);
        hs.setAttribute("myname",u);
        hs.setAttribute("mypasswd",p);
        
        
        HttpSession hh=req.getSession(false);
        pw.println("你好"+(String)hh.getAttribute("myname"));
        
        int pageSize=2;
        int pageNow=1;
        UserBeanCL ubb=new UserBeanCL();
        
        
        ArrayList al=ubb.getResultBypage(pageNow,pageSize);

        pw.println(al.size());
            
            
        pw.println("<table border=1>");
        pw.println("<tr><th>id</th><th>name</th><th>age</th><th>score</th></tr>");
        for(int i=0;i<al.size();i++)
        {  
            UserBean ub=(UserBean)al.get(i);
            pw.println("<tr>");
            pw.println("<td>"+ub.getID()+"</td>");
            pw.println("<td>"+ub.getName()+"</td>");
            pw.println("<td>"+ub.getAge()+"</td>");
            pw.println("<td>"+ub.getScore()+"</td>");
            pw.println("</tr>");
            
        }
        pw.println("</table>");
    
        */
    

    }
    else
    {  
        
        res.sendRedirect("http://www.baidu.com");
    
    }
}

public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
    this.doGet(req,res);
}
    
}




Java code

package com.nong;
import java.sql.*;
public class ConDB {
private Connection ct=null;
private PreparedStatement pm=null;
private ResultSet rs=null;
public Connection getConn()
{
    try
    {
        Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
        ct=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1434;DatabaseName=Mybase","sa","long");
    }
    catch(Exception e)
    {e.printStackTrace();}
    return ct;
}

public ResultSet getRset(String sqlstr)
{  
   try
    {
           ct=getConn();
        pm=ct.prepareStatement(sqlstr);
        rs=pm.executeQuery();
        
    }
   catch(Exception e)
       {e.printStackTrace();}
      
   finally
   {
    return rs;
   }

}

public boolean isExist(String strsql)
{
    boolean  b=false;
    try
    {
    ct=this.getConn();
    pm=ct.prepareStatement(strsql);
    rs=pm.executeQuery();
      if(rs.next())
      {
        b=true;
         }
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
    }
    finally
    {
    
        return b;
    }
    
}


public void closeall()
{ 
try{

  if(this.rs!=null){rs.close();this.rs=null;}
  if(this.pm!=null){pm.close();this.pm=null;}
  if(this.ct!=null){ct.close();this.ct=null;}
}
catch(Exception e)
{e.printStackTrace();}
}  


 
}