dao.java

来自「登录 使用了Ajax 还有重设密码的功能」· Java 代码 · 共 147 行

JAVA
147
字号
package DAO;
import bean.login.*;
import connection.database.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
//等等在搞一个接口
public class DAO{
    
    //sql语句
    String add="insert into login2 value(?,?,?,?)";
    String lookDenglu="select * from login2 where id=? and password=?";
    String lookId="select * from login2 where id order by id desc";
	Connection con;
	PreparedStatement ps;
	ResultSet rs;
    
    //查登录时的数据
    public boolean lookDenglu(String yourId,String yourPassword){
    	boolean boo=false;
    	try{
    		con=DatabaseConnection.getConnection();
    		ps=con.prepareStatement(lookDenglu);
    		ps.setString(1,yourId);
    		ps.setString(2,yourPassword);
    		rs=ps.executeQuery();
    		if(rs.next()){
    			boo=true;
    		}
    	}catch(Exception e){
     		e.printStackTrace();
     	}
     	return boo;
    }
     
     //向login2中加入数据
     public boolean addUser(String yourId,String yourPassword,String yourQuestion,String yourAnswer){
     	boolean boo=false;
     	try{
     		con=DatabaseConnection.getConnection();
     		ps=con.prepareStatement(add);
     		ps.setString(1,yourId);
     		ps.setString(2,yourPassword);
     		ps.setString(3,yourQuestion);
     		ps.setString(4,yourAnswer);
     		int i=ps.executeUpdate();
     		if(i>0){
     			boo=true;
     		}
     	con.close();
     	}catch(Exception e1){
     		e1.printStackTrace();
     	}
     	return boo;
    }
    //查看数据用户名
    public ArrayList findAll(){
    	Login l;
    	ArrayList list=new ArrayList();
    	try{
    		con=DatabaseConnection.getConnection();
     		ps=con.prepareStatement(lookId);
     		rs=ps.executeQuery();  		
     		while(rs.next()){		
     			l=new Login();
     			l.setId(rs.getString("id"));
     			list.add(l);		
     		}
    	}catch(Exception e2){
     			e2.printStackTrace();
     	}
     	return list;
    }
    
    //Ajax  用户名id
    public boolean checkId(String yourId){
    	boolean boo=false;
		try{
	     		con=DatabaseConnection.getConnection();
	     		ps=con.prepareStatement("select * from login2 where id=?");
	     		ps.setString(1,yourId);
	     		rs=ps.executeQuery();  
	     		if(rs.next()){
	     			boo=true;
	     		}
	     	}catch(Exception e1){
	     		e1.printStackTrace();
	     	}
	     	return boo;
	 }
	 //Ajax  用户password
	 public boolean checkPassword(String yourPassword){
	 	boolean boo=false;
		try{
	     		con=DatabaseConnection.getConnection();
	     		ps=con.prepareStatement("select * from login2 where password=?");
	     		ps.setString(1,yourPassword);
	     		rs=ps.executeQuery();  
	     		if(rs.next()){
	     			boo=true;
	     		}
	     	}catch(Exception e1){
	     		e1.printStackTrace();
	     	}
	     	return boo;
	 }
	 
	 //回答的问题和答案
	  public boolean lookAnswer(String yourQuestion,String yourAnswer){
    	boolean boo=false;
    	try{
    		con=DatabaseConnection.getConnection();
    		ps=con.prepareStatement("select * from login2 where question=? and answer=?");
    		ps.setString(1,yourQuestion);
    		ps.setString(2,yourAnswer);
    		rs=ps.executeQuery();
    		if(rs.next()){
    			boo=true;
    		}
    	}catch(Exception e){
     		e.printStackTrace();
     	}
     	return boo;
    }
    
    //重设密码
    public boolean resetPassword(String yourId,String newPassword){
    	boolean boo=false;
    	try{
    		con=DatabaseConnection.getConnection();
    		ps=con.prepareStatement("update login2 set password=? where id=?");
    		ps.setString(1,newPassword);
    		ps.setString(2,yourId);
    		int n=ps.executeUpdate();
    		if(n!=0){
    			boo=true;
    		}
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	return boo;
    }
}

     	

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?