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

📄 testindex.java

📁 基于java的jsp开发的在线考试系统 基于java的jsp开发的在线考试系统
💻 JAVA
字号:
package javaBeanClass.TestIndex;
import javaBeanClass.DatabaseConn;
import javaBeanClass.TestIndex.TestIndexData;
import java.util.*;
import java.sql.*;
public class TestIndex
{
	private DatabaseConn db;
	
	private int pageNumber=1;//要显示的页码数,默认为1
	private int totalPage;//总页数
	
	private int ID;
	private int TestTypeID;
	private String Memo;
	private String Del;
	
	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 setTestTypeID(int TestTypeID)
	{this.TestTypeID=TestTypeID;}
	public int getTestTypeID(){return this.TestTypeID;}
	public void setMemo(String Memo)throws Exception
	{try{this.Memo=new String(Memo.getBytes("8859_1"),"GB2312");}catch(Exception e){throw new Exception("编码失败!");}}
	public String getMemo(){return this.Memo;}
	public void setDel(String Del){this.Del=Del;}
	public String getDel(){return this.Del;}
	
	public TestIndex(){db=new DatabaseConn();}
	
	public TestIndexData findByPrimaryKey()throws SQLException
	{
		sql="select * from View_TestIndex 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 TestIndexData(rs.getInt(1),rs.getInt(2),rs.getString(3),rs.getString(4),rs.getString(5));}
		}
		catch(Exception ex)
		{
			System.out.println("FindByPrimaryKey TestIndex failed:"+ex.getMessage());
			throw new SQLException("FindByPrimaryKey TestIndex failed!");
		}
		finally
		{
			try{con.close();ps.close();}
			catch(SQLException sqlex){System.out.println("FindByPrimaryKey Close TestIndex Error:"+sqlex.getMessage());}
		}
		return null;
	}
	
	public Vector findAll(int pageSizes)throws SQLException
	{
		String whereStr="where Del='0' And TestTypeDel='0'"+(this.TestTypeID==0?"":(" And TestTypeID="+this.TestTypeID));
		String orderByStr="order by TestTypeID";
		
		Vector vResults=new Vector(1,1);
		boolean bResult=false;
		//sql="select * from View_TestIndex where Del='0' "+((this.TestTypeID==0)?"":(" And TestTypeID="+this.TestTypeID));
		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,"View_TestIndex");
			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,"View_TestIndex");
			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 TestIndexData(rs.getInt(1),rs.getInt(2),rs.getString(3),rs.getString(4),rs.getString(5)));
				}while(rs.next());
			}
		}
		catch(Exception ex)
		{
			System.out.println("FindAll TestIndex failed:"+ex.getMessage());
			throw new SQLException("FindAll TestIndex failed!");
		}
		finally
		{
			try{con.close();ps.close();}
			catch(SQLException sqlex){System.out.println("FindAll Close TestIndex Error:"+sqlex.getMessage());}
		}
		if(bResult)
		{return vResults;}
		else
		{return null;}
	}
	
	public Vector findAllDeleted(int pageSizes)throws SQLException
	{
		String whereStr="where Del='1' And TestTypeDel='0'"+(this.TestTypeID==0?"":(" And TestTypeID="+this.TestTypeID));
		String orderByStr="order by TestTypeID";
		
		Vector vResults=new Vector(1,1);
		boolean bResult=false;
		//sql="select * from View_TestIndex where Del='1'"+((this.TestTypeID==0)?"":(" And TestTypeID="+this.TestTypeID));
		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,"View_TestIndex");
			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,"View_TestIndex");
			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 TestIndexData(rs.getInt(1),rs.getInt(2),rs.getString(3),rs.getString(4),rs.getString(5)));
				}while(rs.next());
			}
		}
		catch(Exception ex)
		{
			System.out.println("FindAllDeleted TestIndex failed:"+ex.getMessage());
			throw new SQLException("FindAllDeleted TestIndex failed!");
		}
		finally
		{
			try{con.close();ps.close();}
			catch(SQLException sqlex){System.out.println("FindAllDeleted Close TestIndex Error:"+sqlex.getMessage());}
		}
		if(bResult)
		{return vResults;}
		else
		{return null;}
	}
	
	public int autoInsert(String testNumber)throws SQLException
	{
		//sql="insert into TestIndex (TestTypeID,Memo) values(?,?)";
		Connection con;
		CallableStatement ps=null;
		Statement stat = null;
		ResultSet rs = null;
		
		try{con=db.connectToDB();}
		catch(Exception ex){throw new SQLException("Connect database failed!");}
		boolean bTemp;
		try
		{				
			ps=con.prepareCall("{?=call pr_CreateTest(?,?,?)}");
			ps.registerOutParameter(1,java.sql.Types.INTEGER);
			ps.setInt(2,TestTypeID);
			ps.setString(3,Memo);
			ps.setInt(4,Integer.parseInt(testNumber));
			ps.executeUpdate();
			int errNumber=ps.getInt(1);
			if(errNumber!=0)
			{
				if(errNumber==-1)
				{
					throw new SQLException("你输入的试题数量超出了题库中的题数!生成操作被取消!");
				}
				else
				{
					throw new SQLException("生成出错,操作被取消,请重试!");
				}
			}
		}
		catch(SQLException sqlex)
		{
			System.out.println("Insert TestIndex Error:"+sqlex.getMessage());
			if(!sqlex.getMessage().equals("No row count was produced"))
				throw new SQLException(sqlex.getMessage());
		}
		finally
		{
			try{con.close();ps.close();}
			catch(SQLException sqlex){System.out.println("Insert Close TestIndex Error:"+sqlex.getMessage());}
		}
		
		try{con=db.connectToDB();}
		catch(Exception ex){throw new SQLException("Connect database failed!");}
		try{
			stat=con.createStatement();
			rs = stat.executeQuery("select max(ID) from TestIndex");
			rs.next();
			int testid = rs.getInt(1);
			return testid;
		}
		catch(SQLException e){
			throw new SQLException(e.getMessage());
		}
		finally{
			try{con.close();ps.close();}
			catch(SQLException sqlex){System.out.println("Insert Close TestIndex Error:"+sqlex.getMessage());}
		}
	}
	public int handInsert()throws SQLException
	{
		sql="insert into TestIndex (TestTypeID,Memo) values(?,?)";
		Connection con;
		PreparedStatement ps=null;
		Statement stat = null;
		ResultSet rs = null;
		
		try{con=db.connectToDB();}
		catch(Exception ex){throw new SQLException("Connect database failed!");}
		boolean bTemp;
		try
		{				
			ps=con.prepareStatement(sql);
			ps.setInt(1,TestTypeID);
			ps.setString(2,Memo);
			int rowCount=ps.executeUpdate();
			if(rowCount==0)
			{
				throw new SQLException("试卷生成错误,请重试!");
			}
		}
		catch(SQLException sqlex)
		{
			System.out.println("Insert TestIndex Error:"+sqlex.getMessage());
			if(!sqlex.getMessage().equals("No row count was produced"))
				throw new SQLException(sqlex.getMessage());
		}
		finally
		{
			try{con.close();ps.close();}
			catch(SQLException sqlex){System.out.println("Insert Close TestIndex Error:"+sqlex.getMessage());}
		}
		
		try{con=db.connectToDB();}
		catch(Exception ex){throw new SQLException("Connect database failed!");}
		try{
			stat=con.createStatement();
			rs = stat.executeQuery("select max(ID) from TestIndex");
			rs.next();
			int testid = rs.getInt(1);
			return testid;
		}
		catch(SQLException e){
			throw new SQLException(e.getMessage());
		}
		finally{
			try{con.close();ps.close();}
			catch(SQLException sqlex){System.out.println("Insert Close TestIndex Error:"+sqlex.getMessage());}
		}
	}
	public void update()throws SQLException,Exception
	{
		sql="update TestIndex set TestTypeID=?,Memo=? 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,TestTypeID);
			ps.setString(2,Memo);
			ps.setInt(3,ID);
			int rowCount=ps.executeUpdate();
			if(rowCount==0)
			{
				throw new SQLException("Update TestIndex failed!");
			}
		}
		catch(SQLException sqlex)
		{
			System.err.println("Update TestIndex Error:"+sqlex.getMessage());
			throw new SQLException("Update TestIndex Error"+sqlex.getMessage());
		}
		catch(Exception e)
		{
			System.out.println("Other Of Update TestIndex Error:"+e.getMessage());
			throw new Exception("Other Of Update TestIndex Error");
		}
		finally
		{
			try{con.close();ps.close();}
			catch(SQLException sqlex){System.out.println("Update Close TestIndex Error:"+sqlex.getMessage());}
		}
	}
	
	public void delete()throws SQLException
	{
		sql="update TestIndex 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 TestIndex failed!");
			}
		}
		catch(SQLException sqlex)
		{
			System.out.println("Delete TestIndex Error:"+sqlex.getMessage());
			throw new SQLException("Delete TestIndex Error:"+sqlex.getMessage());
		}
		finally
		{
			try{con.close();ps.close();}
			catch(SQLException sqlex){System.out.println("Delete Close TestIndex Error:"+sqlex.getMessage());}
		}
	}
	
	public void realDelete()throws SQLException
	{
		sql="delete TestIndex 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 TestIndex failed!");
			}
		}
		catch(SQLException sqlex)
		{
			System.out.println("realDelete TestIndex Error:"+sqlex.getMessage());
			throw new SQLException("realDelete TestIndex Error");
		}
		finally
		{
			try{con.close();ps.close();}
			catch(SQLException sqlex){System.out.println("realDelete Close TestIndex Error:"+sqlex.getMessage());}
		}
	}
	
	public void restor()throws SQLException
	{
		sql="update TestIndex 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 TestIndex failed!");
			}
		}
		catch(SQLException sqlex)
		{
			System.out.println("Restor TestIndex Error:"+sqlex.getMessage());
			throw new SQLException("Restor TestIndex Error:"+sqlex.getMessage());
		}
		finally
		{
			try{con.close();ps.close();}
			catch(SQLException sqlex){System.out.println("Restor Close TestIndex Error:"+sqlex.getMessage());}
		}
	}
}

⌨️ 快捷键说明

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