📄 choiceitemdbdao.java
字号:
package org.fangsoft.testcenter.dao.db;
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.ChoiceItemDao;
import org.fangsoft.testcenter.model.ChoiceItem;
public class ChoiceItemDBDao implements ChoiceItemDao {
private static final ChoiceItemDBDao cdao=new ChoiceItemDBDao();
public static final ChoiceItemDBDao getInstance(){
return cdao;
}
private void close(Connection conn) {
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// implement findByQid
private static final String sql_findByQid = "select * from CHOICE where Q_ID=?";
public List<ChoiceItem> findByQid(int Q_id) {
Connection conn = null;
List<ChoiceItem> chList = new ArrayList<ChoiceItem>();
try {
conn = ConnectionFactory.getConnection();
PreparedStatement ps = conn.prepareStatement(sql_findByQid);
ps.setInt(1, Q_id);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
ChoiceItem ch = new ChoiceItem();
ch.setId(rs.getInt("C_ID"));
ch.setName(rs.getString("NAME"));
ch.setCorrect(rs.getInt("CORRECT") > 0 ? true : false);
chList.add(ch);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.close(conn);
}
return chList;
}
// implement insert
private static final String sql_insert = "inert into CHOICE(C_ID,Q_ID,NAME,CORRECT) values(?,?,?,?)";
public void insert(ChoiceItem choice) {
Connection conn = null;
try {
conn = ConnectionFactory.getConnection();
conn.setAutoCommit(false);
PreparedStatement stmt = conn
.prepareStatement("select SEQ_CHOICE.nextval from dual");
ResultSet rs = stmt.executeQuery();
int C_id = 0;
if (rs.next())
C_id = rs.getInt(1);
PreparedStatement ps = conn.prepareStatement(sql_insert);
ps.setInt(1, C_id);
ps.setInt(2, choice.getId());
ps.setString(3, choice.getName());
ps.setInt(4, choice.isCorrect() == true ? 1 : 0);
ps.executeUpdate();
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
} finally {
this.close(conn);
}
}
// implement update
private static final String sql_update = "update CHOICE set Q_ID=?,NAME=?,CORRECT=? where C_ID=?";
public void update(ChoiceItem choice) {
Connection conn = null;
try {
conn = ConnectionFactory.getConnection();
PreparedStatement ps = conn.prepareStatement(sql_update);
ps.setInt(1, choice.getId());
ps.setString(2, choice.getName());
ps.setInt(3, choice.isCorrect() == true ? 1 : 0);
ps.setInt(4, choice.getId());
ps.executeUpdate();
choice.setId(0);
} catch (SQLException e) {
e.printStackTrace();
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
} finally {
this.close(conn);
}
}
// implement delete
private static final String sql_delete = "delete from CHOICE where Q_ID=?";
public void delete(int Q_id) {
Connection conn = null;
try {
conn = ConnectionFactory.getConnection();
PreparedStatement ps = conn.prepareStatement(sql_delete);
ps.setInt(1, Q_id);
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<ChoiceItem> item=new ArrayList<ChoiceItem>();
ChoiceItemDBDao c=new ChoiceItemDBDao();
item=c.findByQid(1);
//c.deleteByTrID(2);
for(int i=0;i<item.size();i++){
System.out.println(item.get(i).getId()+","+item.get(i).getName());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -