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

📄 testdbdao.java

📁 纯JAVA代码的考试系统
💻 JAVA
字号:
package org.fangsoft.testcenter.dao.db;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.fangsoft.testcenter.dao.TestDao;
import org.fangsoft.testcenter.model.Test;
import org.fangsoft.util.DataValidator;

public class TestDBDao implements TestDao {
	private static final TestDBDao tdao=new TestDBDao();
	public static final TestDBDao getInstance(){
		return tdao;
	}

	public static final String sql_findAllTest = "select * from TEST";

	public List<Test> findAllTest() {
		List<Test> allTest = new ArrayList<Test>();
		Connection conn = null;
		try {
			conn = ConnectionFactory.getConnection();
			ResultSet rs = conn.prepareStatement(sql_findAllTest)
					.executeQuery();
			while (rs.next()) {
				String type = rs.getString("T_TYPE");
				//Test t=new Test();
				Test t = this.getInstance(type);// 导人相应类型的试题	
				//t.setType(rs.getString("T_TYPE")); 
				//Test t=this.getInstance(type);
				t.setId(rs.getInt("T_ID"));
				t.setName(rs.getString("T_NAME"));
				t.setNumQuestion(rs.getInt("NUMQUESTION"));
				t.setTimeLimitMin(rs.getInt("TIMELIMITMIN"));
				t.setDescription(rs.getString("DISCRIPTION"));
				t.setScore(rs.getInt("SCORE"));
				t.setType(rs.getString("T_TYPE"));
				allTest.add(t);// 放入数组容器
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.close(conn);
		}
		return allTest;
	}

	private void close(Connection conn) {
		if (conn != null)
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
	}

	private Test getInstance(String className) {
		Test test = null;
		try {
			test = (Test) Class.forName(className).newInstance();// ????
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return test == null ? new Test() : test;
	}

	public static final String sql_findTestByName = "select * from TEST where T_NAME= ?";

	public Test findTestByName(String testName) {
		Connection conn = null;
		try {
			conn = ConnectionFactory.getConnection();
			PreparedStatement ps = conn.prepareStatement(sql_findTestByName);
			ps.setString(1, testName);
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				String type = rs.getString("T_TYPE");
				//Test t=new Test();
				Test t = this.getInstance(type);// 导人相应类型的试题	
				//tt.setType(rs.getString("T_TYPE")); 
				//Test t=this.getInstance(tt.getType());
				t.setId(rs.getInt("T_ID"));
				t.setName(rs.getString("T_NAME"));
				t.setNumQuestion(rs.getInt("NUMQUESTION"));
				t.setTimeLimitMin(rs.getInt("TIMELIMITMIN"));
				t.setDescription(rs.getString("DISCRIPTION"));
				t.setScore(rs.getInt("SCORE"));
				t.setType(rs.getString("T_TYPE"));
				return t;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.close(conn);
		}
		return null;
	}

	public static final String sql_save = "insert into TEST (T_NAME, NUMQUESTION, TIMELIMITMIN, DISCRIPTION, SCORE, T_TYPE,T_ID) values(?,?,?,?,?,?,?)";

	public void save(Test test) {
		Connection conn = null;
		try {
			conn = ConnectionFactory.getConnection();
			conn.setAutoCommit(false);
			PreparedStatement stmt = conn
					.prepareStatement("select SEQ_TEST.nextval from dual");
			ResultSet rs = stmt.executeQuery();
			int id = 0;
			if (rs.next())
				id = rs.getInt(1);
			PreparedStatement ps = conn.prepareStatement(sql_save);
			ps.setString(1, test.getName());
			ps.setInt(2, test.getNumQuestion());
			ps.setInt(3, test.getTimeLimitMin());
			ps.setString(4, DataValidator.validate(test.getDescription()));
			ps.setInt(5, test.getScore());
			ps.setString(6,test.getType());
			//ps.setString(6, test.getClass().getName());
			ps.setInt(7, id);
			ps.executeUpdate();
			test.setId(id);
			conn.commit();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			this.close(conn);
		}
	}

	public static final String sql_update = "update Test set T_NAME=?, NUMQUESTION=?, TIMELIMITMIN=?, DISCRIPTION=?, SCORE=?,T_TYPE=?  where T_ID=? ";

	public void update(Test test) {
		Connection conn = null;
		try {
			conn = ConnectionFactory.getConnection();
			PreparedStatement ps = conn.prepareStatement(sql_update);
			ps.setString(1, test.getName());
			ps.setInt(2, test.getNumQuestion());
			ps.setInt(3, test.getTimeLimitMin());
			ps.setString(4, DataValidator.validate(test.getDescription()));
			ps.setInt(5, test.getScore());
			ps.setInt(6, test.getId());
			ps.setString(7,test.getType());
			ps.executeUpdate();
			test.setId(0);
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			this.close(conn);
		}
	}

	public static final String sql_delete = "delete Test  where T_NAME=? ";

	public void delete(String testName) {
		Connection conn = null;
		try {
			conn = ConnectionFactory.getConnection();
			PreparedStatement ps = conn.prepareStatement(sql_delete);
			ps.setString(1, testName);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			this.close(conn);
		}
	}
	public static void main(String[] args){
		//List<TestResult> tr=new ArrayList<TestResult>();
		TestDBDao c=new TestDBDao();
	    Test tr=c.findTestByName("web考试");
	    System.out.println(tr.getId()+","+tr.getName()+","+tr.getDescription()+","+tr.getNumQuestion());
		
		//}
		
	}

}

⌨️ 快捷键说明

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