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

📄 question.java

📁 J2EE指南
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package javaBeanClass.Question;
import javaBeanClass.DatabaseConn;
import javaBeanClass.Question.QuestionData;
import java.util.*;
import java.sql.*;
public class Question
{
	private DatabaseConn db;
	
	private int pageNumber=1;//要显示的页码数,默认为1
	private int totalPage;//总页数
	
	private int ID;
	private String Classes;
	private String QuestText;
	private String QuestTextA;
	private String QuestTextB;
	private String QuestTextC;
	private String QuestTextD;
	private String Answer;
	private String Del;
	private int TestTypeID;
	private String Mark;
	
	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 setClasses(String Classes)throws Exception
	{try{this.Classes=new String(Classes.getBytes("8859_1"),"GB2312");}catch(Exception e){throw new Exception("编码失败!");}}
	public String getClasses(){return this.Classes;}
	public void setQuestText(String QuestText)throws Exception
	{try{this.QuestText=new String(QuestText.getBytes("8859_1"),"GB2312");}catch(Exception e){throw new Exception("编码失败!");}}
	public String getQuestText(){return this.QuestText;}
	public void setQuestTextA(String QuestTextA)throws Exception
	{try{this.QuestTextA=new String(QuestTextA.getBytes("8859_1"),"GB2312");}catch(Exception e){throw new Exception("编码失败!");}}
	public String getQuestTextA(){return this.QuestTextA;}
	public void setQuestTextB(String QuestTextB)throws Exception
	{try{this.QuestTextB=new String(QuestTextB.getBytes("8859_1"),"GB2312");}catch(Exception e){throw new Exception("编码失败!");}}
	public String getQuestTextB(){return this.QuestTextB;}
	public void setQuestTextC(String QuestTextC)throws Exception
	{try{this.QuestTextC=new String(QuestTextC.getBytes("8859_1"),"GB2312");}catch(Exception e){throw new Exception("编码失败!");}}
	public String getQuestTextC(){return this.QuestTextC;}
	public void setQuestTextD(String QuestTextD)throws Exception
	{try{this.QuestTextD=new String(QuestTextD.getBytes("8859_1"),"GB2312");}catch(Exception e){throw new Exception("编码失败!");}}
	public String getQuestTextD(){return this.QuestTextD;}
	public void setAnswer(String Answer)throws Exception
	{try{this.Answer=new String(Answer.getBytes("8859_1"),"GB2312");}catch(Exception e){throw new Exception("编码失败!");}}
	public String getAnswer(){return this.Answer;}
	public void setDel(String Del){this.Del=Del;}
	public String getDel(){return this.Del;}
	public void setTestTypeID(int TestTypeID){this.TestTypeID=TestTypeID;}
	public int getTestTypeID(){return this.TestTypeID;}
	public void setMark(String Mark){this.Mark=Mark;}
	public String getMark(){return this.Mark;}
	
	public Question(){db=new DatabaseConn();}
	
	public QuestionData findByPrimaryKey()throws SQLException
	{
		sql="select * from View_Question 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 QuestionData(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8),rs.getString(9),rs.getString(10),rs.getString(11),rs.getInt(12));}
		}
		catch(Exception ex)
		{
			System.out.println("FindByPrimaryKey Question failed:"+ex.getMessage());
			throw new SQLException("FindByPrimaryKey Question failed!");
		}
		finally
		{
			try{con.close();ps.close();}
			catch(SQLException sqlex){System.out.println("FindByPrimaryKey Close Question Error:"+sqlex.getMessage());}
		}
		return null;
	}
	
	public Vector findAll(int pageSizes/*每页显示的条数*/)throws SQLException
	{
		//使用分页存储过程进行查询操作
		
		/*old01-----使用PreparedStatement,在使用PreparedStatement时不能使用Like子句,所以改用Statement
		String sqlTestTypeID="";
		if(this.TestTypeID!=0) sqlTestTypeID=" And TestTypeID=?";
		sql="select * from View_Question where Del='0' And Classes like ? And Mark like ? And QuestText like ?"+sqlTestTypeID+" Order by TestTypeID,Mark";
		*/
		
		/*old02-----使用Statement,现改用分页存储过程
		sql="select * from View_Question where Del='0'";
		sql+=(this.Classes==null||this.Classes.equals(""))?"":(" And Classes like '%"+this.Classes+"%'");
		sql+=(this.Mark==null||this.Mark.equals(""))?"":(" And Mark="+this.Mark);
		sql+=(this.QuestText==null||this.QuestText.equals(""))?"":(" And QuestText like '%"+this.QuestText+"%'");
		sql+=(this.TestTypeID==0)?"":(" And TestTypeID="+this.TestTypeID);
		sql+=" Order by TestTypeID,Mark";
		*/
		
		//定义分页存储过程用到的where子句
		String whereStr="where Del='0' And TestTypeDel='0'";
		whereStr+=(this.Classes==null||this.Classes.equals(""))?"":(" And Classes like '%"+this.Classes+"%'");
		whereStr+=(this.Mark==null||this.Mark.equals(""))?"":(" And Mark="+this.Mark);
		whereStr+=(this.QuestText==null||this.QuestText.equals(""))?"":(" And QuestText like '%"+this.QuestText+"%'");
		whereStr+=(this.TestTypeID==0)?"":(" And TestTypeID="+this.TestTypeID);
		
		//定义分页存储过程用到的order by子句,可带asc或desc
		String orderByStr="order by TestTypeID,Mark";
		
		Vector vResults=new Vector(1,1);
		boolean bResult=false;
		Connection con;
		CallableStatement ps=null;
		ResultSet rs=null;
		try{con=db.connectToDB();}
		catch(Exception ex){throw new SQLException("Connect database failed!");}
		try
		{
			/*
				pr_SplitPage存储过程共有6个参数:
				1-总页数/int/输出参数
				2-查询所用的表名/String/输入参数(必须)
				3-需要查看的页码(页码为88888888时只返回总页数,默认为1)/int/输入
				4-每页显示的记录数(默认为10条)/int/输入
				5-分页时用到的where子句(完整的where子句,默认为空)/String/输入
				6-分页时用到的排序字符串(完整的order by子句,默认为空,只查看总页数时不用输入,但要返回结果集时必须输入)/String/输入
				
				返回0表示成功,返回-1表示排序字符串order by子句为空
				警告:此存储过程使用临时表,并在临时表中产生新的标识字段,如果原表中有标识字段,
            则不能正常工作,可以做一个原表的视图再使用此存储过程
			*/			
			
			ps=con.prepareCall("{call pr_splitPage(?,?,?,?,?)}");
			ps.registerOutParameter(1,java.sql.Types.INTEGER);
			ps.setString(2,"View_Question");
			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_Question");
			ps.setInt(3,pageNumber);
			ps.setInt(4,pageSizes);
			ps.setString(5,whereStr);
			ps.setString(6,orderByStr);
			
			/*old01
			ps.setString(1,(this.Classes==null||this.Classes.equals(""))?"'%'":("'%"+this.Classes+"%'"));
			ps.setString(2,(this.Mark==null||this.Mark.equals(""))?"'%'":("'%"+this.Mark+"%'"));
			ps.setString(3,(this.QuestText==null||this.QuestText.equals(""))?"'%'":("'%"+this.QuestText+"%'"));
			if(this.TestTypeID!=0) ps.setInt(4,this.TestTypeID);
			*/
			
			rs=ps.executeQuery();//返回记录集
			bResult=rs.next();
			if(bResult)
			{
				do
				{
					vResults.add(new QuestionData(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8),rs.getString(9),rs.getString(10),rs.getString(11),rs.getInt(12)));
				}while(rs.next());
			}
		}
		catch(Exception ex)
		{
			System.out.println("FindAll Question failed:"+ex.getMessage());
			throw new SQLException("FindAll Question failed!");
		}
		finally
		{
			try{con.close();ps.close();}
			catch(SQLException sqlex){System.out.println("FindAll Close Question Error:"+sqlex.getMessage());}
		}
		if(bResult)
		{return vResults;}
		else
		{return null;}
	}
	
	public Vector findAllDeleted(int pageSizes)throws SQLException
	{
		//使用分页存储过程进行查询操作
		
		/*old01-----使用PreparedStatement,在使用PreparedStatement时不能使用Like子句,所以改用Statement
		String sqlTestTypeID="";
		if(this.TestTypeID!=0) sqlTestTypeID=" And TestTypeID=?";
		sql="select * from View_Question where Del='1' And Classes like ? And Mark like ? And QuestText like ?"+sqlTestTypeID+" Order by TestTypeID,Mark";
		*/
		
		/*old02-----使用Statement,现改用分页存储过程
		sql="select * from View_Question where Del='1'";
		sql+=(this.Classes==null||this.Classes.equals(""))?"":(" And Classes like '%"+this.Classes+"%'");
		sql+=(this.Mark==null||this.Mark.equals(""))?"":(" And Mark="+this.Mark);
		sql+=(this.QuestText==null||this.QuestText.equals(""))?"":(" And QuestText like '%"+this.QuestText+"%'");
		sql+=(this.TestTypeID==0)?"":(" And TestTypeID="+this.TestTypeID);
		sql+=" Order by TestTypeID,Mark";
		*/
		
		//定义分页存储过程用到的where子句
		String whereStr="where Del='1' And TestTypeDel='0'";
		whereStr+=(this.Classes==null||this.Classes.equals(""))?"":(" And Classes like '%"+this.Classes+"%'");
		whereStr+=(this.Mark==null||this.Mark.equals(""))?"":(" And Mark="+this.Mark);
		whereStr+=(this.QuestText==null||this.QuestText.equals(""))?"":(" And QuestText like '%"+this.QuestText+"%'");
		whereStr+=(this.TestTypeID==0)?"":(" And TestTypeID="+this.TestTypeID);
		
		//定义分页存储过程用到的order by子句,可带asc或desc
		String orderByStr="order by TestTypeID,Mark";
		
		Vector vResults=new Vector(1,1);
		boolean bResult=false;
		Connection con;
		CallableStatement ps=null;
		ResultSet rs=null;
		try{con=db.connectToDB();}
		catch(Exception ex){throw new SQLException("Connect database failed!");}
		try
		{
			/*

⌨️ 快捷键说明

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