-
Notifications
You must be signed in to change notification settings - Fork 0
/
TaoTiDAO.java
113 lines (111 loc) · 3.97 KB
/
TaoTiDAO.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.wgh.dao;
import com.wgh.actionForm.TaoTiForm;
import com.wgh.core.ConnDB;
import java.sql.*;
import java.util.*;
public class TaoTiDAO {
private ConnDB conn=new ConnDB();
//添加数据
public int insert(TaoTiForm taoTiForm) {
String sql1="SELECT * FROM tb_taoTi WHERE name='"+taoTiForm.getName()+"' AND lessonId="+taoTiForm.getLessonId()+"";
ResultSet rs = conn.executeQuery(sql1);
String sql = "";
int falg = 0;
try {
if (rs.next()) {
falg=2;
} else {
sql = "INSERT INTO tb_taoTi (id,name,lessonId) values(null,'" +
taoTiForm.getName() + "',"+taoTiForm.getLessonId()+")";
falg = conn.executeUpdate(sql);
System.out.println("添加套题时的SQL:" + sql);
conn.close();
}
} catch (Exception ex) {
falg=0;
}
return falg;
}
//查询方法
public List query(int id) {
List taoTiList = new ArrayList();
TaoTiForm taoTiForm1 = null;
String sql="";
if(id==0){
sql = "SELECT * FROM tb_taoTi ORDER BY lessonId DESC";
}else{
sql = "SELECT * FROM tb_taoTi WHERE id=" +id+ "";
}
ResultSet rs = conn.executeQuery(sql);
try {
while (rs.next()) {
taoTiForm1 = new TaoTiForm();
taoTiForm1.setID(rs.getInt(1));
taoTiForm1.setName(rs.getString(2));
taoTiForm1.setLessonId(rs.getInt(3));
taoTiForm1.setJoinTime(java.text.DateFormat.getDateTimeInstance().parse(rs.getString(4)));
taoTiList.add(taoTiForm1);
}
} catch (Exception ex) {}
return taoTiList;
}
//根据课程ID查询套题
public List queryTaoTi(int lessonId){
List taoTiList = new ArrayList();
TaoTiForm taoTiForm1 = null;
String sql="SELECT * FROM tb_taoTi WHERE lessonId="+lessonId+"";
System.out.println("queryTaoTi:"+sql);
ResultSet rs = conn.executeQuery(sql);
try {
while (rs.next()) {
taoTiForm1 = new TaoTiForm();
taoTiForm1.setID(rs.getInt(1));
taoTiForm1.setName(rs.getString(2));
taoTiForm1.setLessonId(rs.getInt(3));
taoTiForm1.setJoinTime(java.text.DateFormat.getDateTimeInstance().parse(rs.getString(4)));
taoTiList.add(taoTiForm1);
}
} catch (Exception ex) {}
return taoTiList;
}
//修改数据
public int update(TaoTiForm taoTiForm){
String sql="UPDATE tb_taoTi SET name='"+taoTiForm.getName()+"',lessonId="+taoTiForm.getLessonId()+" where id="+taoTiForm.getID()+"";
int ret=conn.executeUpdate(sql);
System.out.println("修改套题时的SQL:"+sql);
conn.close();
return ret;
}
//根据所属课程查询课程名称
public String getLesson(int id){
String lessonName="";
if(id>0){
String sql="SELECT * FROM tb_lesson WHERE id="+id+"";
ResultSet rs=conn.executeQuery(sql);
try {
if(rs.next()) {
lessonName=rs.getString(2);
}
} catch (Exception ex) {}
}
return lessonName;
}
// 删除数据
public int delete(TaoTiForm taoTiForm) {
int flag=0;
String[] delId=taoTiForm.getDelIdArray();
if (delId.length>0){
String id="";
for(int i=0;i<delId.length;i++){
id=id+delId[i]+",";
}
id=id.substring(0,id.length()-1);
String sql = "DELETE FROM tb_taoTi where id in (" + id +")";
flag = conn.executeUpdate(sql);
conn.close();
}else{
flag=0;
}
return flag;
}
}