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

📄 extosubdbbean.java

📁 北大青鸟在线考试系统,包括在线考试模块,教师管理模块,系统管理模块
💻 JAVA
字号:
/*
 *定义试题、试卷数据操作组件
 */
package TestClass;

import java.sql.*;
import java.util.*;

public class ExToSubDBbean
{
	//数据库连接指向空
	private Connection conn=null;
	//根据试卷编号列出试题、试卷内容
	private PreparedStatement GetOne=null;
	//列出所有试题、试卷内容
	private PreparedStatement GetAll=null;
	//添加试题
	private PreparedStatement AddStmt=null;
	//修改试题
	private PreparedStatement UpStmt=null;
	//删除试题
	private PreparedStatement DelStmt=null;	
	
	public ExToSubDBbean()
	{
		//声明数据库连接
		String driver,user,pass,dbURL;
		PropertyResourceBundle resourceBundle = (PropertyResourceBundle) PropertyResourceBundle.getBundle("Config");

		driver = resourceBundle.getString("jdbc.driver");
		user = resourceBundle.getString("jdbc.user");
		pass = resourceBundle.getString("jdbc.password");
		dbURL = resourceBundle.getString("jdbc.dbURL");

		try  
		{
			Class.forName(driver);
			conn =DriverManager.getConnection(dbURL,user,pass);
			//根据试卷编号列出试题、试卷内容
			GetOne=conn.prepareStatement("Select * From ExToSub Where ExNo=?");
			//列出所有试题、试卷内容
			GetAll=conn.prepareStatement("Select * From ExToSub");
			//添加试题
			AddStmt=conn.prepareStatement("Insert into ExToSub values(?,?)");
			//修改试题
			UpStmt=conn.prepareStatement("Update ExToSub set SubID=? Where ExNo=?)");
			//删除试题
			DelStmt=conn.prepareStatement("Delete From ExToSub Where SubID=?)");
		}
		catch(java.lang.ClassNotFoundException e) 
		{
			System.err.println("ExToSubDBbean(): 0" + e.getMessage());
		}
		catch(java.sql.SQLException e)
		{
			System.err.println("ExToSubDBbean(): 1" + e.getMessage());
		}
		catch(Exception e)
		{
			System.err.println("ExToSubDBbean(): 2" + e.getMessage());	
		}
	}
	
	/*
	 * 数据访问
	 */	
	 
	//列出所有试题、试卷内容
	public ArrayList GetAllData()
	{
		ArrayList rt=new ArrayList();
		
		try 
		{
			ResultSet Rs=GetAll.executeQuery();
			
			ExToSubBean beanObj=null;
			while(Rs.next())
			{
				beanObj=new ExToSubBean();
				beanObj.setExNo(Rs.getString(1));
				beanObj.setSubID(Rs.getInt(2));
				rt.add(beanObj);
			}
			Rs.close();
		}
		catch(SQLException ex) 
		{
			System.err.println("ExToSubDBbean.GetAllData: " + ex.getMessage());
		}
		return rt;
	}
	
	//根据试卷编号列出试题、试卷内容
	public ExToSubBean GetOne(String PK)
	{
		ExToSubBean rt=null;
		try 
		{
			GetOne.setString(1,PK);
			
			ResultSet Rs=GetOne.executeQuery();
			
			if (Rs.next())
			{
				rt=new ExToSubBean();
				rt.setExNo(Rs.getString(1));
				rt.setSubID(Rs.getInt(2));
			}
			Rs.close();
		}
		catch(SQLException ex) 
		{
			System.err.println("ExToSubDBbean.GetOne: " + ex.getMessage());
		}
		return rt;
	}

	//添加试题
	public boolean InsertDB(ExToSubBean obj)
	{
		boolean rt=false;
		try 
		{
			AddStmt.setString(1,obj.getExNo());
			AddStmt.setInt(2,obj.getSubID());
			rt=(AddStmt.executeUpdate()==1);
		}
		catch(SQLException ex) 
		{
			System.err.println("ExToSubDBbean.InsertDB: " + ex.getMessage());
		}
		return rt;
	}
		
	//修改试题
	public boolean UpdateDB(ExToSubBean obj)
	{
		boolean rt=false;
		try 
		{
			UpStmt.setInt(2,obj.getSubID());
			UpStmt.setString(1,obj.getExNo());
			rt=(UpStmt.executeUpdate()==1);
		}
		catch(SQLException ex) 
		{
			System.err.println("ExToSubDBbean.UpdateDB: " + ex.getMessage());
		}	
		
		return rt;
	}

	//删除试题	
	public boolean DeleteDB(int PK)
	{
		boolean rt=false;
		try 
		{
			DelStmt.setInt(1,PK);
			rt=(DelStmt.executeUpdate()==1);
		}
		catch(SQLException ex) 
		{
			System.err.println("ExToSubDBbean.DeleteDB: " + ex.getMessage());
		}
		
		return rt;		
	}		
}

⌨️ 快捷键说明

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