📄 class.java
字号:
package javaBeanClass.Class;
import javaBeanClass.DatabaseConn;
import javaBeanClass.Class.ClassData;
import java.util.*;
import java.sql.*;
public class Class
{
private DatabaseConn db;
private int pageNumber=1;//要显示的页码数,默认为1
private int totalPage;//总页数
private int ID;
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 setClassNumber(String ClassNumber){this.ClassNumber=ClassNumber;}
public String getClassNumber(){return this.ClassNumber;}
public Class(){db=new DatabaseConn();}
public ClassData findByPrimaryKey()throws SQLException
{
sql="select * from Class 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 ClassData(rs.getInt(1),rs.getString(2));}
}
catch(Exception ex)
{
System.out.println("FindByPrimaryKey Class failed:"+ex.getMessage());
throw new SQLException("FindByPrimaryKey Class failed!");
}
finally
{
try{con.close();ps.close();}
catch(SQLException sqlex){System.out.println("FindByPrimaryKey Close Class Error:"+sqlex.getMessage());}
}
return null;
}
public Vector findAll(int pageSizes)throws SQLException
{
String tableName="Class";
String whereStr="where Del='0'"+(ClassNumber==null?"":" And ClassNumber like '%"+ClassNumber+"%'");
String orderByStr="order by ClassNumber";
Vector vResults=new Vector(1,1);
boolean bResult=false;
//sql="select * from Class 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 ClassData(rs.getInt(1),rs.getString(2)));
}while(rs.next());
}
}
catch(Exception ex)
{
System.out.println("FindAll Class failed:"+ex.getMessage());
throw new SQLException("FindAll Class failed!");
}
finally
{
try{con.close();ps.close();}
catch(SQLException sqlex){System.out.println("FindAll Close Class Error:"+sqlex.getMessage());}
}
if(bResult)
{return vResults;}
else
{return null;}
}
public Vector findAllDeleted(int pageSizes)throws SQLException
{
String tableName="Class";
String whereStr="where Del='1'"+(ClassNumber==null?"":" And ClassNumber like '%"+ClassNumber+"%'");
String orderByStr="order by ClassNumber";
Vector vResults=new Vector(1,1);
boolean bResult=false;
//sql="select * from Class 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 ClassData(rs.getInt(1),rs.getString(2)));
}while(rs.next());
}
}
catch(Exception ex)
{
System.out.println("FindAllDeleted Class failed:"+ex.getMessage());
throw new SQLException("FindAllDeleted Class failed!");
}
finally
{
try{con.close();ps.close();}
catch(SQLException sqlex){System.out.println("FindAllDeleted Close Class Error:"+sqlex.getMessage());}
}
if(bResult)
{return vResults;}
else
{return null;}
}
public void insert()throws SQLException
{
sql="insert into Class (ClassNumber) 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.setString(1,ClassNumber);
int rowCount=ps.executeUpdate();
if(rowCount==0)
{
throw new SQLException("Insert Class failed!");
}
}
catch(SQLException sqlex)
{
System.out.println("Insert Class Error:"+sqlex.getMessage());
throw new SQLException("Insert Class Error!");
}
finally
{
try{con.close();ps.close();}
catch(SQLException sqlex){System.out.println("Insert Close Class Error:"+sqlex.getMessage());}
}
}
private void check() throws RuntimeException
{
sql="select count(*) from UserInfo where ClassId=?";
Connection con;
PreparedStatement ps=null;
try{con=db.connectToDB();}
catch(Exception ex){throw new RuntimeException("Connect database failed!");}
try
{
ps=con.prepareStatement(sql);
ps.setInt(1,ID);
ResultSet rs=ps.executeQuery();
if(rs.next())
{
throw new RuntimeException("Failed!");
}
}
catch(Exception e)
{
System.out.println("Other Of Update Class Error:"+e.getMessage());
throw new RuntimeException("该班级还未清空请清空后再删除该班");
}
finally
{
try{con.close();ps.close();}
catch(SQLException sqlex){System.out.println("Update Close Class Error:"+sqlex.getMessage());}
}
}
public void update()throws SQLException,Exception
{
sql="update Class set ClassNumber=? 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.setString(1,ClassNumber);
ps.setInt(2,ID);
int rowCount=ps.executeUpdate();
if(rowCount==0)
{
throw new SQLException("Update Class failed!");
}
}
catch(SQLException sqlex)
{
System.err.println("Update Class Error:"+sqlex.getMessage());
throw new SQLException("Update Class Error");
}
catch(Exception e)
{
System.out.println("Other Of Update Class Error:"+e.getMessage());
throw new Exception("Other Of Update Class Error");
}
finally
{
try{con.close();ps.close();}
catch(SQLException sqlex){System.out.println("Update Close Class Error:"+sqlex.getMessage());}
}
}
public void delete()throws SQLException
{
this.check();
sql="update Class 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 Class failed!");
}
}
catch(SQLException sqlex)
{
System.out.println("Delete Class Error:"+sqlex.getMessage());
throw new SQLException("Delete Class Error");
}
finally
{
try{con.close();ps.close();}
catch(SQLException sqlex){System.out.println("Delete Close Class Error:"+sqlex.getMessage());}
}
}
public void realDelete()throws SQLException
{
this.check();
sql="delete Class 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 Class failed!");
}
}
catch(SQLException sqlex)
{
System.out.println("realDelete Class Error:"+sqlex.getMessage());
throw new SQLException("realDelete Class Error");
}
finally
{
try{con.close();ps.close();}
catch(SQLException sqlex){System.out.println("realDelete Close Class Error:"+sqlex.getMessage());}
}
}
public void restor()throws SQLException
{
sql="update Class 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 Class failed!");
}
}
catch(SQLException sqlex)
{
System.out.println("Restor Class Error:"+sqlex.getMessage());
throw new SQLException("Restor Class Error");
}
finally
{
try{con.close();ps.close();}
catch(SQLException sqlex){System.out.println("Restor Close Class Error:"+sqlex.getMessage());}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -