?? itemmapper.java
字號:
/*
* ItemMapper.java
*
* MSE06B班張智力的實驗報告
*
* 2006年12月11日
*/
package olts.dao;
import java.util.*;
import java.io.*;
import java.sql.*;
import olts.item.*;
import olts.util.ErrorRecorder;
/**
* ItemMapper類,負責(zé)獲取題目、增加題目、修改題目、刪除題目的數(shù)據(jù)庫操作
*/
public class ItemMapper {
/**
* ItemMapper對象,SingleTon模式的單一對象
*/
private static ItemMapper itemMapper;
/**
* Connection對象,數(shù)據(jù)庫連接器
*/
private Connection conn;
/**
* Statement對象
*/
private Statement stmt;
/**
* driver屬性,記錄odbc的驅(qū)動程序名
*/
private String driver;
/**
* url屬性,記錄數(shù)據(jù)源的url字段
*/
private String url;
/**
* List對象,在數(shù)據(jù)庫中取出試題數(shù)據(jù),組織成<code>Item<code>對象,并存放在該List對象容器中
*/
private List itemLibrary = new LinkedList();
/**
* 構(gòu)造函數(shù),從driver.ini文件中讀出驅(qū)動程序名并初始化數(shù)據(jù)源的url。
*/
private ItemMapper(){
try{
File file = new File("driver.ini");
BufferedReader reader = new BufferedReader(new FileReader(file));
driver = reader.readLine();
url = "jdbc:odbc:OLTSDB";
reader.close();
}catch(IOException e){
ErrorRecorder.record(e);
driver = "sun.jdbc.odbc.JdbcOdbcDriver";
}
}
/**
* getItemMapper方法,SingleTon模式下用于返回單一對象的方法。
* @return
*/
public static ItemMapper getItemMapper(){
if(itemMapper == null){
itemMapper = new ItemMapper();
return itemMapper;
}
else return itemMapper;
}
/**
* getItemLibrary方法,從數(shù)據(jù)庫中查詢Item數(shù)據(jù),返回一隊列的Item對象
* @return List 返回一個Item對象的容器
* @throws SQLException 當發(fā)生SQL語法、數(shù)據(jù)庫等錯誤時拋出該異常
*/
public List getItemLibrary(String sql)throws SQLException{
itemLibrary.clear();
try{
Class.forName(driver);
conn = DriverManager.getConnection(url);
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
String content = rs.getString("content");
String standAnswer = rs.getString("standAnswer");
int difficulty = rs.getInt("difficulty");
int score = rs.getInt("score");
int time = rs.getInt("expecttime");
String subject = rs.getString("subject");
int type = rs.getInt("type");
switch(type){
case 1: itemLibrary.add(new SingleChoiceItem(content, standAnswer, difficulty, score, time, subject)); break;
case 2: itemLibrary.add(new MultiChoiceItem(content, standAnswer, difficulty, score, time, subject)); break;
case 3: itemLibrary.add(new TrueFalseItem(content, standAnswer, difficulty, score, time, subject)); break;
case 4: itemLibrary.add(new BlankFillItem(content, standAnswer, difficulty, score, time, subject)); break;
}
}
rs.close();
stmt.close();
conn.close();
}catch(ClassNotFoundException cnfe){
ErrorRecorder.record(cnfe);
}
return itemLibrary;
}
/**
* addItem方法,提供向數(shù)據(jù)庫添加一條Item記錄的方法。
* @param item 要加入數(shù)據(jù)庫的Item記錄
* @param type 該記錄的試題類型
* @return boolean 當添加Item記錄成功時返回true,不成功時返回false
* @throws SQLException 當發(fā)生SQL語法、數(shù)據(jù)庫等錯誤時拋出該異常
*/
public boolean addItem(Item item, int type)throws SQLException{
boolean b = false;
try{
Class.forName(driver);
conn = DriverManager.getConnection(url);
PreparedStatement pstmt = conn.prepareStatement("insert into ItemTable(content, standAnswer, difficulty, score, time, subject, type) values(?,?,?,?,?,?,?)");
pstmt.setString(1, item.content);
pstmt.setString(2, item.standAnswer);
pstmt.setInt(3, item.difficulty);
pstmt.setInt(4, item.score);
pstmt.setInt(5, item.time);
pstmt.setString(6, item.subject);
pstmt.setInt(7, type);
b = pstmt.execute();
pstmt.close();
conn.close();
}catch(ClassNotFoundException cnfe){
ErrorRecorder.record(cnfe);
}
return b;
}
/**
* alterItem方法,提供向數(shù)據(jù)庫修改Item記錄的方法
* @return int 返回該修改操作影響的行數(shù)
* @throws SQLException 當發(fā)生SQL語法、數(shù)據(jù)庫等錯誤時拋出該異常
*/
public int alterItem()throws SQLException{
try{
Class.forName(driver);
stmt = conn.createStatement();
stmt.execute("Update ");
}catch(ClassNotFoundException cnfe){
ErrorRecorder.record(cnfe);
}
return 0;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -