⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 checkuser.java

📁 综合测评管理系统——完成班级的综合测评工作
💻 JAVA
字号:
import java.sql.*;
import javax.swing.*;

public class CheckUser{
	//private String url="jdbc:sqlserver://localhost:1433;DatabaseName=Restaurant;user=user;password=user;";
	String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";  //加载JDBC驱动
  String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=sample";  //连接服务器和数据库sample
  String userName = "sa";  //默认用户名
  String userPwd = "2005";  //密码
	public CheckUser(){
	  try{
	  	Class.forName(driverName);
      //dbConn=DriverManager.getConnection(dbURL,userName,userPwd);
      //System.out.println("Connection Successful!");  //如果连接成功 控制台输出Connection Successful!
     }catch (Exception e){
     	e.printStackTrace();
    }
	}
	public boolean isValidUser(String username,String password){
		Connection dbConn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		boolean isValid=false;
		try{
			dbConn=DriverManager.getConnection(dbURL,userName,userPwd);
			String queryStr="select * from login WHERE username=? and loginpass=?";
			ps=dbConn.prepareStatement(queryStr);
			ps.setString(1,username);
			ps.setString(2,password);
			rs=ps.executeQuery();
			if(rs.next())
			  isValid=true;
		}catch(SQLException exc){
			exc.printStackTrace();
		}finally{
			if(rs!=null)try{rs.close();}catch(SQLException ignore){}
			if(ps!=null)try{ps.close();}catch(SQLException ignore){}
			if(dbConn!=null)try{dbConn.close();}catch(SQLException ignore){}
		}
		return isValid;
	}
	
	public void updatePassword(String username,String password){
		Connection dbConn=null;
		PreparedStatement ps=null;
		try{
			dbConn=DriverManager.getConnection(dbURL,userName,userPwd);
		  String updateStr="update login set loginpass=? WHERE username=?";
		  ps=dbConn.prepareStatement(updateStr);
		  ps.setString(1,password);
	    ps.setString(2,username);
	    ps.executeUpdate();
	  }catch(SQLException exc){
		  exc.printStackTrace();
	  }finally{
		  if(ps!=null)try{ps.close();}catch(SQLException ignore){}
		  if(dbConn!=null)try{dbConn.close();}catch(SQLException ignore){}
	  }
	}
	
	public boolean modifyUser(String username,String password,String usertype){
		Connection dbConn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try{
			dbConn=DriverManager.getConnection(dbURL,userName,userPwd);
		  String updateStr="update login set loginpass=?,type=? WHERE username=?";
		  String selectStr="select * from login where username=?";
		  ps=dbConn.prepareStatement(selectStr);
		  ps.setString(1,username);
		  rs=ps.executeQuery();
		  if(rs.next()){
		  	ps=dbConn.prepareStatement(updateStr);
		  	ps.setString(1,password);
		  	ps.setString(2,usertype);
		  	ps.setString(3,username);
		  	ps.executeUpdate();
		  }else{return false;}
	  }catch(SQLException exc){
		  exc.printStackTrace();
	  }finally{
		  if(ps!=null)try{ps.close();}catch(SQLException ignore){}
		  if(dbConn!=null)try{dbConn.close();}catch(SQLException ignore){}
	  }
	  return true;
	}
	
	public boolean userExist(String username){
		Connection dbConn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		boolean exist=false;
		try{
			dbConn=DriverManager.getConnection(dbURL,userName,userPwd);
			String selectStr="select username from login where username=?";
			ps=dbConn.prepareStatement(selectStr);
			ps.setString(1,username);
			rs=ps.executeQuery();
			if(rs.next())
			  exist=true;
			}catch(SQLException exc){
				exc.printStackTrace();
			}finally{
				if(rs!=null)try{rs.close();}catch(SQLException ignore){}
			  if(ps!=null)try{ps.close();}catch(SQLException ignore){}
			  if(dbConn!=null)try{dbConn.close();}catch(SQLException ignore){}
			}
		return false;
	}
	
	public boolean addUser(String username,String password,String usertype){
		Connection dbConn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try{
			dbConn=DriverManager.getConnection(dbURL,userName,userPwd);
			String updateStr="insert into login(username,loginpass,type) values(?,?,?)";
			String selectStr="select * from login where username=?";
		  ps=dbConn.prepareStatement(selectStr);
		  ps.setString(1,username);
		  rs=ps.executeQuery();
		  String temp;
		  if(rs.next()){
		  	return false;
		  }
			ps=dbConn.prepareStatement(updateStr);
			ps.setString(1,username);
			ps.setString(2,password);
			ps.setString(3,usertype);
			ps.executeUpdate();
		}catch(SQLException exc){
			exc.printStackTrace();
		}finally{
			if(ps!=null)try{ps.close();}catch(SQLException ignore){}
			if(dbConn!=null)try{dbConn.close();}catch(SQLException ignore){}
		}
		return true;
	}
	
	public boolean deleteUser(String username){
		Connection dbConn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try{
			dbConn=DriverManager.getConnection(dbURL,userName,userPwd);
			String deleteStr="delete from login where username=?";
			String selectStr="select * from login where username=?";
		  ps=dbConn.prepareStatement(selectStr);
		  ps.setString(1,username);
		  rs=ps.executeQuery();
		  String temp;
		  if(!rs.next()){
		  	return false;
		  }
			ps=dbConn.prepareStatement(deleteStr);
			ps.setString(1,username);
			ps.executeUpdate();
		}catch(SQLException exc){
			
		}finally{
			if(ps!=null)try{ps.close();}catch(SQLException ignore){}
			if(dbConn!=null)try{dbConn.close();}catch(SQLException ignore){}
		}
		return true;
	}
	
	public int getUsertype(String username){
		Connection dbConn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		int usertype=0;
		try{
	 	Class.forName(driverName);
	 	}catch(Exception e){
	 		e.printStackTrace();
	 	}
		try{
			dbConn=DriverManager.getConnection(dbURL,userName,userPwd);
			String queryStr="select * from login WHERE username=?";
			ps=dbConn.prepareStatement(queryStr);
			ps.setString(1,username);
			rs=ps.executeQuery();
			if(rs.next())
			usertype=rs.getInt("type");

		}catch(SQLException exc){
			exc.printStackTrace();
		}finally{
			if(rs!=null)try{rs.close();}catch(SQLException ignore){}
			if(ps!=null)try{ps.close();}catch(SQLException ignore){}
			if(dbConn!=null)try{dbConn.close();}catch(SQLException ignore){}
		}
		return usertype;
	}
}

⌨️ 快捷键说明

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