-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcourse.java
More file actions
167 lines (143 loc) · 5.35 KB
/
course.java
File metadata and controls
167 lines (143 loc) · 5.35 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package javastdapp;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
/**
*
* @author HP
*/
public class course {
public void insertUpdateDeleteCourse(char operation, Integer id, String label,
Integer hours){
Connection con = MyConnection.getConnection();
PreparedStatement ps;
if(operation == 'i') //for insert
{
try
{
ps = con.prepareStatement("INSERT INTO course(label,hours_number) VALUES (?,?)");
ps.setString(1, label);
ps.setInt(2, hours);
if(ps.executeUpdate() > 0){
JOptionPane.showMessageDialog(null, "New Course Added");
}
}
catch (SQLException ex)
{
Logger.getLogger(course.class.getName()).log(Level.SEVERE,null,ex);
}
}
if(operation == 'u') //for update
{
try
{
ps = con.prepareStatement("UPDATE `course` SET `label`= ?,`hours_number`= ? WHERE `id` = ?");
ps.setString(1, label);
ps.setInt(2, hours);
ps.setInt(3, id);
if(ps.executeUpdate() > 0){
JOptionPane.showMessageDialog(null, "Cours Data Updated");
}
}
catch (SQLException ex)
{
Logger.getLogger(course.class.getName()).log(Level.SEVERE,null,ex);
}
}
if(operation == 'd') //for delete
{
int yesORno=JOptionPane.showConfirmDialog(null, "The score will be also deleted","Delete Course",JOptionPane.OK_CANCEL_OPTION,0);
if(yesORno == JOptionPane.OK_OPTION)
{
try
{
ps = con.prepareStatement("DELETE FROM `course` WHERE `id`= ?");
ps.setInt(1, id);
if(ps.executeUpdate() > 0){
JOptionPane.showMessageDialog(null, "Course Deleted");
}
}
catch (SQLException ex)
{
Logger.getLogger(student.class.getName()).log(Level.SEVERE,null,ex);
}
}
}
}
public boolean isCourseExist(String courseName){
boolean isExist= false;
Connection con = MyConnection.getConnection();
PreparedStatement ps;
try{
ps = con.prepareStatement("SELECT * FROM `course` WHERE `label` = ?");
ps.setString(1,courseName);
ResultSet rs =ps.executeQuery();
if(rs.next())
{
isExist= true;
}
} catch (SQLException ex) {
Logger.getLogger(course.class.getName()).log(Level.SEVERE, null, ex);
}
return isExist;
}
public void Fill_Course_Table(JTable table){
Connection con = MyConnection.getConnection();
PreparedStatement ps;
try{
ps = con.prepareStatement("SELECT * FROM `course`");
ResultSet rs =ps.executeQuery();
DefaultTableModel model =(DefaultTableModel) table.getModel();
Object[] row;
while(rs.next())
{
row = new Object[3];
row[0] = rs.getInt(1);
row[1] = rs.getString(2);
row[2] = rs.getInt(3);
model.addRow(row);
}
} catch (SQLException ex) {
Logger.getLogger(course.class.getName()).log(Level.SEVERE, null, ex);
}
}
public int getCourseId(String courselabel)
{
int courseid =0;
Connection con = MyConnection.getConnection();
PreparedStatement ps;
try{
ps = con.prepareStatement("SELECT * FROM `course` WHERE `label` = ?");
ps.setString(1,courselabel);
ResultSet rs =ps.executeQuery();
if(rs.next())
{
courseid = rs.getInt("Id");
}
} catch (SQLException ex) {
Logger.getLogger(course.class.getName()).log(Level.SEVERE, null, ex);
}
return courseid;
}
public void Fill_Course_combo(JComboBox combo){
Connection con = MyConnection.getConnection();
PreparedStatement ps;
try{
ps = con.prepareStatement("SELECT * FROM `course`");
ResultSet rs = ps.executeQuery();
while(rs.next())
{
combo.addItem(rs.getString(2));
}
} catch (SQLException ex){
Logger.getLogger(course.class.getName()).log(Level.SEVERE, null, ex);
}
}
}