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

📄 userinfo.java

📁 基于java的jsp开发的在线考试系统 基于java的jsp开发的在线考试系统
💻 JAVA
字号:
package javaBeanClass.UserInfo;
import javaBeanClass.DatabaseConn;
import javaBeanClass.UserInfo.UserInfoData;
import javaBeanClass.MyException;
import java.util.*;
import java.sql.*;
public class UserInfo
{
	private DatabaseConn db;
	
	private int pageNumber=1;//要显示的页码数,默认为1
	private int totalPage;//总页数
	
	private int ID;
	private String Name;
	private String SN;
	private String Passwd;
	private int ClassID;
	private String Del;
	private String ClassNumber;
	
	private String sql;
	
	public void setPageNumber(int pageNumber){this.pageNumber=pageNumber;}
	public int getPageNumber(){return this.pageNumber;}
	public void setTotalPage(int totalPage){this.totalPage=totalPage;}
	public int getTotalPage(){return this.totalPage;}
	
	public void setID(int ID){this.ID=ID;}
	public int getID(){return this.ID;}
	public void setName(String Name)throws Exception
	{try{this.Name=new String(Name.getBytes("8859_1"),"GB2312");}catch(Exception e){throw new Exception("编码失败!");}}
	public String getName(){return this.Name;}
	public void setSN(String SN)
	{this.SN=SN;}
	public String getSN(){return this.SN;}
	public void setPasswd(String Passwd)
	{this.Passwd=Passwd;}
	public String getPasswd(){return this.Passwd;}
	public void setClassID(int ClassID){this.ClassID=ClassID;}
	public int getClassID(){return this.ClassID;}
	public void setDel(String Del){this.Del=Del;}
	public String getDel(){return this.Del;}
	public void setClassNumber(String ClassNumber){this.ClassNumber=ClassNumber;}
	public String getClassNumber(){return this.ClassNumber;}
	
	public UserInfo(){db=new DatabaseConn();}
	
	public UserInfoData findByPrimaryKey()throws SQLException
	{
		sql="select * from View_UserInfo where ID=?";
		Connection con;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try{con=db.connectToDB();}
		catch(Exception ex){throw new SQLException("Connect database failed!");}
		try
		{
			ps=con.prepareStatement(sql);
			ps.setInt(1,ID);
			rs=ps.executeQuery();
			if(rs.next())
			{return new UserInfoData(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getInt(5),rs.getString(6),rs.getString(7));}
		}
		catch(Exception ex)
		{
			System.out.println("FindByPrimaryKey UserInfo failed:"+ex.getMessage());
			throw new SQLException("FindByPrimaryKey UserInfo failed!");
		}
		finally
		{
			try{con.close();ps.close();}
			catch(SQLException sqlex){System.out.println("FindByPrimaryKey Close UserInfo Error:"+sqlex.getMessage());}
		}
		return null;
	}
	
	public Vector findAll(int pageSizes)throws SQLException
	{
		String tableName="View_UserInfo";
		String whereStr="where Del='0'"+(this.ClassID==0?"":(" And ClassID="+this.ClassID));
		String orderByStr="order by SN";
		
		Vector vResults=new Vector(1,1);
		boolean bResult=false;
		//sql="select * from UserInfo where Del='0'";
		Connection con;
		CallableStatement ps=null;
		ResultSet rs=null;
		try{con=db.connectToDB();}
		catch(Exception ex){throw new SQLException("Connect database failed!");}
		try
		{
			ps=con.prepareCall("{call pr_splitPage(?,?,?,?,?)}");
			ps.registerOutParameter(1,java.sql.Types.INTEGER);
			ps.setString(2,tableName);
			ps.setInt(3,88888888);//用于返回总页数
			ps.setInt(4,pageSizes);
			ps.setString(5,whereStr);
			ps.executeUpdate();//首先返回总页数,存储过程不能即返回总页数又返回记录集,具体用法还要再研究
			this.totalPage=ps.getInt(1);
						
			ps=con.prepareCall("{call pr_splitPage(?,?,?,?,?,?)}");
			ps.registerOutParameter(1,java.sql.Types.INTEGER);//用于返回记录集,此参数可不带
			ps.setString(2,tableName);
			ps.setInt(3,pageNumber);
			ps.setInt(4,pageSizes);
			ps.setString(5,whereStr);
			ps.setString(6,orderByStr);
			
			rs=ps.executeQuery();
			bResult=rs.next();
			if(bResult)
			{
				do
				{
					vResults.add(new UserInfoData(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getInt(5),rs.getString(6),rs.getString(7)));
				}while(rs.next());
			}
		}
		catch(Exception ex)
		{
			System.out.println("FindAll UserInfo failed:"+ex.getMessage());
			throw new SQLException("FindAll UserInfo failed!");
		}
		finally
		{
			try{con.close();ps.close();}
			catch(SQLException sqlex){System.out.println("FindAll Close UserInfo Error:"+sqlex.getMessage());}
		}
		if(bResult)
		{return vResults;}
		else
		{return null;}
	}
	
	public Vector findAllDeleted(int pageSizes)throws SQLException
	{
		String tableName="View_UserInfo";
		String whereStr="where Del='1'"+(this.ClassID==0?"":(" And ClassID="+this.ClassID));
		String orderByStr="order by SN";
		
		Vector vResults=new Vector(1,1);
		boolean bResult=false;
		//sql="select * from UserInfo where Del='1'";
		Connection con;
		CallableStatement ps=null;
		ResultSet rs=null;
		try{con=db.connectToDB();}
		catch(Exception ex){throw new SQLException("Connect database failed!");}
		try
		{
			ps=con.prepareCall("{call pr_splitPage(?,?,?,?,?)}");
			ps.registerOutParameter(1,java.sql.Types.INTEGER);
			ps.setString(2,tableName);
			ps.setInt(3,88888888);//用于返回总页数
			ps.setInt(4,pageSizes);
			ps.setString(5,whereStr);
			ps.executeUpdate();//首先返回总页数,存储过程不能即返回总页数又返回记录集,具体用法还要再研究
			this.totalPage=ps.getInt(1);
						
			ps=con.prepareCall("{call pr_splitPage(?,?,?,?,?,?)}");
			ps.registerOutParameter(1,java.sql.Types.INTEGER);//用于返回记录集,此参数可不带
			ps.setString(2,tableName);
			ps.setInt(3,pageNumber);
			ps.setInt(4,pageSizes);
			ps.setString(5,whereStr);
			ps.setString(6,orderByStr);
			
			rs=ps.executeQuery();
			bResult=rs.next();
			if(bResult)
			{
				do
				{
					vResults.add(new UserInfoData(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getInt(5),rs.getString(6),rs.getString(7)));
				}while(rs.next());
			}
		}
		catch(Exception ex)
		{
			System.out.println("FindAllDeleted UserInfo failed:"+ex.getMessage());
			throw new SQLException("FindAllDeleted UserInfo failed!");
		}
		finally
		{
			try{con.close();ps.close();}
			catch(SQLException sqlex){System.out.println("FindAllDeleted Close UserInfo Error:"+sqlex.getMessage());}
		}
		if(bResult)
		{return vResults;}
		else
		{return null;}
	}
	
	public void insert()throws SQLException,MyException
	{		
		Connection con;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try{con=db.connectToDB();}
		catch(Exception ex){throw new SQLException("Connect database failed!");}
		boolean bTemp;
		try
		{
			sql="select ID from UserInfo where SN=?";
			ps=con.prepareStatement(sql);
			ps.setString(1,this.SN);
			rs=ps.executeQuery();
			if(rs.next()) throw new MyException("此学员SN号已存在!");
			
			sql="insert into UserInfo (Name,SN,Passwd,ClassID) values(?,?,?,?)";
			ps=con.prepareStatement(sql);
			ps.setString(1,this.Name);
			ps.setString(2,this.SN);
			ps.setString(3,this.Passwd);
			ps.setInt(4,this.ClassID);
			int rowCount=ps.executeUpdate();
			if(rowCount==0)
			{
				throw new SQLException("Insert UserInfo failed!");
			}
		}
		catch(SQLException sqlex)
		{
			System.out.println("Insert UserInfo Error:"+sqlex.getMessage());
			throw new SQLException("Insert UserInfo Error!");
		}
		finally
		{
			try{con.close();ps.close();}
			catch(SQLException sqlex){System.out.println("Insert Close UserInfo Error:"+sqlex.getMessage());}
		}
	}
	
	public void update()throws SQLException,Exception
	{
		Connection con;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try{con=db.connectToDB();}
		catch(Exception ex){throw new SQLException("Connect database failed!");}
		try
		{
			sql="select ID from UserInfo where SN=?";
			ps=con.prepareStatement(sql);
			ps.setString(1,this.SN);
			rs=ps.executeQuery();
			if(rs.next()) throw new MyException("此学员SN号已存在!");
			
			if(this.Passwd!=null&&!this.Passwd.equals(""))
			{
				sql="update UserInfo set Name=?,SN=?,ClassID=?,Passwd=? where ID=?";
			}
			else
			{
				sql="update UserInfo set Name=?,SN=?,ClassID=? where ID=?";
			}
			
			ps=con.prepareStatement(sql);
			ps.setString(1,this.Name);
			ps.setString(2,this.SN);
			ps.setInt(3,this.ClassID);
			if(this.Passwd!=null&&!this.Passwd.equals(""))
			{
				ps.setString(4,this.Passwd);
				ps.setInt(5,this.ID);
			}
			else
			{
				ps.setInt(4,this.ID);
			}
			int rowCount=ps.executeUpdate();
			if(rowCount==0)
			{
				throw new SQLException("Update UserInfo failed!");
			}
		}
		catch(SQLException sqlex)
		{
			System.err.println("Update UserInfo Error:"+sqlex.getMessage());
			throw new SQLException("Update UserInfo Error");
		}
		finally
		{
			try{con.close();ps.close();}
			catch(SQLException sqlex){System.out.println("Update Close UserInfo Error:"+sqlex.getMessage());}
		}
	}
	
	public void delete()throws SQLException
	{
		sql="update UserInfo set Del='1' where ID=?";
		Connection con;
		PreparedStatement ps=null;
		try{con=db.connectToDB();}
		catch(Exception ex){throw new SQLException("Connect database failed!");}
		try
		{
			ps=con.prepareStatement(sql);
			ps.setInt(1,ID);
			int rowCount=ps.executeUpdate();
			if(rowCount==0)
			{
				throw new SQLException("Delete UserInfo failed!");
			}
		}
		catch(SQLException sqlex)
		{
			System.out.println("Delete UserInfo Error:"+sqlex.getMessage());
			throw new SQLException("Delete UserInfo Error");
		}
		finally
		{
			try{con.close();ps.close();}
			catch(SQLException sqlex){System.out.println("Delete Close UserInfo Error:"+sqlex.getMessage());}
		}
	}
	
	public void realDelete()throws SQLException
	{
		sql="delete UserInfo where ID=?";
		Connection con;
		PreparedStatement ps=null;
		try{con=db.connectToDB();}
		catch(Exception ex){throw new SQLException("Connect database failed!");}
		try
		{
			ps=con.prepareStatement(sql);
			ps.setInt(1,ID);
			int rowCount=ps.executeUpdate();
			if(rowCount==0)
			{
				throw new SQLException("realDelete UserInfo failed!");
			}
		}
		catch(SQLException sqlex)
		{
			System.out.println("realDelete UserInfo Error:"+sqlex.getMessage());
			throw new SQLException("realDelete UserInfo Error");
		}
		finally
		{
			try{con.close();ps.close();}
			catch(SQLException sqlex){System.out.println("realDelete Close UserInfo Error:"+sqlex.getMessage());}
		}
	}
	
	public void restor()throws SQLException
	{
		sql="update UserInfo set Del='0' where ID=?";
		Connection con;
		PreparedStatement ps=null;
		try{con=db.connectToDB();}
		catch(Exception ex){throw new SQLException("Connect database failed!");}
		try
		{
			ps=con.prepareStatement(sql);
			ps.setInt(1,ID);
			int rowCount=ps.executeUpdate();
			if(rowCount==0)
			{
				throw new SQLException("Restor UserInfo failed!");
			}
		}
		catch(SQLException sqlex)
		{
			System.out.println("Restor UserInfo Error:"+sqlex.getMessage());
			throw new SQLException("Restor UserInfo Error");
		}
		finally
		{
			try{con.close();ps.close();}
			catch(SQLException sqlex){System.out.println("Restor Close UserInfo Error:"+sqlex.getMessage());}
		}
	}
}

⌨️ 快捷键说明

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