📄 test.java
字号:
package javaBeanClass.Test;
import javaBeanClass.DatabaseConn;
import javaBeanClass.Test.TestData;
import java.util.*;
import java.sql.*;
public class Test
{
private DatabaseConn db;
private int pageNumber=1;//要显示的页码数,默认为1
private int totalPage;//总页数
private int TestID;
private int TestIndexID;
private String Classes;
private String QuestText;
private String QuestTextA;
private String QuestTextB;
private String QuestTextC;
private String QuestTextD;
private String Answer;
private String Mark;
private int QuestionID;
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 setTestID(int TestID){this.TestID=TestID;}
public int getTestID(){return this.TestID;}
public void setTestIndexID(int TestIndexID){this.TestIndexID=TestIndexID;System.out.println("asdf"+TestIndexID);}
public int getTestIndexID(){return this.TestIndexID;}
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 setMark(String Mark){this.Mark=Mark;}
public String getMark(){return this.Mark;}
public void setQuestionID(int QuestionID){this.QuestionID=QuestionID; System.out.println("sdf"+QuestionID);}
public int getQuestionID(){return this.QuestionID;}
public Test(){db=new DatabaseConn();}
public TestData findByPrimaryKey()throws SQLException
{
sql="select * from View_Test where TestIndexID=?";
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,TestIndexID);
rs=ps.executeQuery();
if(rs.next())
{return new TestData(rs.getInt(1),rs.getInt(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));}
}
catch(Exception ex)
{
System.out.println("FindByPrimaryKey Test failed:"+ex.getMessage());
throw new SQLException("FindByPrimaryKey Test failed!");
}
finally
{
try{con.close();ps.close();}
catch(SQLException sqlex){System.out.println("FindByPrimaryKey Close Test Error:"+sqlex.getMessage());}
}
return null;
}
public Vector findAll(int pageSizes)throws SQLException
{
String whereStr="where TestIndexID="+this.TestIndexID;
String orderByStr="";
Vector vResults=new Vector(1,1);
boolean bResult=false;
//sql="select * from View_Test where TestIndexID=?";
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_Test");
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_Test");
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 TestData(rs.getInt(1),rs.getInt(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)));
}while(rs.next());
}
}
catch(Exception ex)
{
System.out.println("FindAll Test failed:"+ex.getMessage());
throw new SQLException("FindAll Test failed!");
}
finally
{
try{con.close();ps.close();}
catch(SQLException sqlex){System.out.println("FindAll Close Test Error:"+sqlex.getMessage());}
}
if(bResult)
{return vResults;}
else
{return null;}
}
//Test表只能删除和新增记录
public void insert()throws SQLException
{
sql="insert Test (TestIndexID,QuestionID) values(?,?)";
Connection con;
PreparedStatement ps=null;
try{con=db.connectToDB();}
catch(Exception ex){throw new SQLException("Connect database failed!");}
boolean bTemp;
try
{
ps=con.prepareStatement(sql);
ps.setInt(1,TestIndexID);
ps.setInt(2,QuestionID);
int rowCount=ps.executeUpdate();
if(rowCount==0)
{
throw new SQLException("Insert Test failed!");
}
}
catch(SQLException sqlex)
{
System.out.println("Insert Test Error:"+sqlex.getMessage());
throw new SQLException(sqlex.getMessage());
}
finally
{
try{con.close();ps.close();}
catch(SQLException sqlex){System.out.println("Insert Close Test Error:"+sqlex.getMessage());}
}
}
public void delete()throws SQLException
{
sql="delete Test 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,TestID);
int rowCount=ps.executeUpdate();
if(rowCount==0)
{
throw new SQLException("Delete Test failed!");
}
}
catch(SQLException sqlex)
{
System.out.println("Delete Test Error:"+sqlex.getMessage());
throw new SQLException("Delete Test Error:"+sqlex.getMessage());
}
finally
{
try{con.close();ps.close();}
catch(SQLException sqlex){System.out.println("Delete Close Test Error:"+sqlex.getMessage());}
}
}
//Test表只能删除和新增记录
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -