期末作业救急加V:wu484028417
package GUICode;
import javax.swing.*;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.sql.*;
import java.util.Vector;
public class TopGUI {
Font font = new Font("微软雅黑", Font.BOLD, 24);
Font font1 = new Font("微软雅黑", Font.PLAIN, 20);
Font font2 = new Font("微软雅黑", Font.BOLD, 20);
Font font3 = new Font("微软雅黑", Font.PLAIN, 20);
Font font4 = new Font("微软雅黑", Font.PLAIN, 15);
Component hStruct1 = Box.createHorizontalStrut(800);
Component hStruct2 = Box.createHorizontalStrut(10);
Component hStruct3 = Box.createHorizontalStrut(10);
String name = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://127.0.0.1:1433;databaseName=GUI01;user=sa;password=111111";
int i = 0;
public static void main(String[] args) {
TopGUI topGUI = new TopGUI();
topGUI.loginGui();
}
public void topGui(int Sid) {
String[] strings = new String[49];
int[] i = new int[49];
try {
Class.forName(name);
Connection con = DriverManager.getConnection(url);
String SQL = "select Cname,Ctime from Course4 where Cid in (Select Cid from SC1 where Sid = ?)";
PreparedStatement statement = con.prepareStatement(SQL);
statement.setInt(1, Sid);
ResultSet resultSet = statement.executeQuery();
int z = 0;
while (resultSet.next()) {
strings[z] = resultSet.getString(1);
i[z] = resultSet.getInt(2);
z++;
}
con.close();
} catch (SQLException e) {
System.out.println("数据库连接出错!");
} catch (ClassNotFoundException e1) {
System.out.println("找不到目标加载类!");
}
System.out.println(strings[0]);
JFrame jFrame = new JFrame("首页");
jFrame.setSize(1200, 800);
jFrame.setLocationRelativeTo(null);
jFrame.setResizable(true);
jFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JMenuBar jMenuBar = new JMenuBar();
JMenu menu = new JMenu("菜单");
JMenuItem setting = new JMenuItem("设置");
JMenuItem consult = new JMenuItem("咨询");
JMenuItem exitMenu = new JMenuItem("退出登陆");
exitMenu.addActionListener(e -> {
jFrame.dispose();
}
);
menu.setFont(font2);
setting.setFont(font2);
consult.setFont(font2);
exitMenu.setFont(font2);
jMenuBar.add(hStruct2);
jMenuBar.add(menu);
jMenuBar.add(hStruct1);
jMenuBar.add(setting);
jMenuBar.add(hStruct3);
jMenuBar.add(consult);
jMenuBar.add(hStruct2);
jMenuBar.add(exitMenu);
JMenuItem selectMenu = new JMenuItem("选课");
JMenuItem dropMenu = new JMenuItem("退选课");
JMenuItem addMenu = new JMenuItem("加选课");
JMenuItem checkMenu = new JMenuItem("成绩查询");
menu.add(selectMenu);
menu.addSeparator();
menu.add(dropMenu);
menu.addSeparator();
selectMenu.setFont(font3);
dropMenu.setFont(font3);
addMenu.setFont(font3);
checkMenu.setFont(font3);
jFrame.setJMenuBar(jMenuBar);
JPanel jPanel = new JPanel();
dropMenu.addActionListener(e -> exit(Sid));
selectMenu.addActionListener(e -> TopGUI.this.innerGui(Sid));
JLabel jLabel = new JLabel("学 期 课 表");
jLabel.setFont(new Font("微软雅黑", Font.BOLD, 30));
JButton jButton = new JButton("刷 新");
jButton.setFont(font1);
jButton.addActionListener(e -> {
jFrame.dispose();
topGui(Sid);
});
Object[][] array = new Object[7][7];
Object[] columnNames = {"周一", "周二", "周三", "周四", "周五", "周六", "周日"};
Object[][] rowDate;
rowDate = array;
int q = 0;
for (int p : i) {
if (p != 0)
q++;
}
int length = q;
System.out.println(length);
int[] num = {0, 0, 0, 0, 0, 0, 0};
int[] num01 = {0, 0, 0, 0, 0, 0, 0};
while (length != 0) {
if (i[num01[0]] == 1) {
rowDate[0][num[0]] = strings[num01[0]];
num[0]++;
}
num01[0]++;
if (i[num01[1]] == 2) {
rowDate[1][num[1]] = strings[num01[1]];
num[1]++;
}
num01[1]++;
if (i[num01[2]] == 3) {
rowDate[num[2]][2] = strings[num01[2]];
num[2]++;
}
num01[2]++;
if (i[num01[3]] == 4) {
rowDate[num[3]][3] = strings[num01[3]];
num[3]++;
}
num01[3]++;
if (i[num01[4]] == 5) {
rowDate[num[4]][4] = strings[num01[4]];
num[4]++;
}
num01[4]++;
if (i[num01[5]] == 6) {
rowDate[num[5]][5] = strings[num01[5]];
num[5]++;
}
num01[5]++;
if (i[num01[6]] == 7) {
rowDate[num[6]][6] = strings[num01[6]];
num[6]++;
}
num01[6]++;
length--;
}
JTable jTable = new JTable(rowDate, columnNames);
jTable.getTableHeader().setFont(font2);
jTable.setFont(font4);
jTable.setRowHeight(60);
TableColumn[] tableColumns = new TableColumn[7];
for (int i1 = 0; i1 <= 6; i1++) {
tableColumns[i1] = jTable.getColumnModel().getColumn(i1);
tableColumns[i1].setPreferredWidth(150);
tableColumns[i1].sizeWidthToFit();
}
jTable.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
jTable.getTableHeader().setResizingAllowed(false);
jTable.getTableHeader().setReorderingAllowed(false);
Box vBox = Box.createVerticalBox();
Box hBox = Box.createHorizontalBox();
Box hBox1 = Box.createHorizontalBox();
hBox.add(Box.createHorizontalStrut(20));
hBox.add(jLabel);
hBox1.add(Box.createHorizontalStrut(900));
hBox1.add(jButton);
vBox.add(hBox);
vBox.add(jTable.getTableHeader());
vBox.add(jTable);
vBox.add(Box.createVerticalStrut(40));
vBox.add(hBox1);
jPanel.add(vBox);
jFrame.setContentPane(jPanel);
jFrame.setVisible(true);
}
public void loginGui() {
JFrame jFrame = new JFrame("LoginGui");
JPanel jPanel = new JPanel();
jFrame.setSize(500, 400);
JLabel jLabel = new JLabel("南苑选课系统");
JLabel jLabel1 = new JLabel("学号:");
JLabel jLabel2 = new JLabel("密码:");
jLabel.setFont(font);
jLabel1.setFont(font);
jLabel2.setFont(font);
JTextField jTextField = new JTextField(10);
JPasswordField jPasswordField = new JPasswordField(10);
jTextField.setFont(font1);
jPasswordField.setFont(font1);
JButton jButton = new JButton("登陆");
JButton jButton1 = new JButton("重置");
jButton.setFont(font1);
jButton1.setFont(font1);
jButton1.addActionListener(e -> {
jTextField.setText(null);
jPasswordField.setText(null);
});
jButton.addActionListener(e -> {
try {
Class.forName(name);
Connection con = DriverManager.getConnection(url);
String SQL = "select Sid from Student where Sid =? and pwd = ?";
PreparedStatement statement1 = con.prepareStatement(SQL);
statement1.setInt(1, Integer.valueOf(String.valueOf(jTextField.getText())));
statement1.setInt(2, Integer.valueOf(String.valueOf(jPasswordField.getPassword())));
ResultSet resultSet = statement1.executeQuery();
int Sid = Integer.parseInt(jTextField.getText());
if (resultSet.next()) {
topGui(Sid);
} else {
JOptionPane.showMessageDialog(
jFrame,
"输入错误或该用户不存在!",
"操作结果",
JOptionPane.WARNING_MESSAGE
);
jTextField.setText(null);
jPasswordField.setText(null);
}
} catch (ClassNotFoundException classNotFoundException) {
System.out.println("找不到加载类或加载类为空!");
} catch (SQLException e1) {
System.out.println("数据库连接错误!");
}
}
);
Box hbox = Box.createHorizontalBox();
hbox.add(jLabel1);
hbox.add(jTextField);
Box hbox1 = Box.createHorizontalBox();
hbox1.add(jLabel2);
hbox1.add(jPasswordField);
Box hbox2 = Box.createHorizontalBox();
hbox2.add(jButton);
hbox2.add(Box.createHorizontalStrut(20));
hbox2.add(jButton1);
Box hbox3 = Box.createHorizontalBox();
hbox3.add(jLabel);
hbox3.add(Box.createHorizontalStrut(30));
Box vbox = Box.createVerticalBox();
vbox.add(Box.createVerticalStrut(40));
vbox.add(hbox3);
vbox.add(Box.createVerticalStrut(40));
vbox.add(hbox);
vbox.add(Box.createVerticalStrut(40));
vbox.add(hbox1);
vbox.add(Box.createVerticalStrut(40));
vbox.add(hbox2);
jPanel.add(vbox);
jFrame.setContentPane(jPanel);
jFrame.setLocationRelativeTo(jPanel);
jFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
jFrame.setResizable(true);
jFrame.setVisible(true);
}
public void innerGui(int Sid) {
Vector<Object> vector1 = new Vector<>();
Vector<Object> vector2 = new Vector<>();
Vector<Object> vector3 = new Vector<>();
Vector<Object> vector4 = new Vector<>();
try {
Class.forName(name);
Connection con = DriverManager.getConnection(url);
String SQL = "Select * from Course1";
PreparedStatement stmt = con.prepareStatement(SQL);
ResultSet rs1 = stmt.executeQuery();
while (rs1.next()) {
vector1.add(rs1.getString(5));
vector2.add(rs1.getString(4));
vector3.add(rs1.getString(3));
vector4.add(rs1.getString(2));
i++;
}
} catch (SQLException e) {
System.out.println("数据库连接出错!");
} catch (ClassNotFoundException e1) {
System.out.println("找不到目标加载类!");
}
JFrame jFrame = new JFrame("选课界面");
jFrame.setVisible(true);
jFrame.setSize(900, 750);
jFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
jFrame.setLocationRelativeTo(null);
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JPanel jPanel3 = new JPanel();
JTabbedPane jTabbedPane = new JTabbedPane();
jFrame.setContentPane(jTabbedPane);
Object[] columnNames = {"课程名称", "任课老师", "上课时间", "学分", "是否选择"};
Object[][] rowDate = new Object[i][5];
String[] strings1 = new String[i];
int j = 0;
while (i != 0) {
rowDate[j][0] = vector4.elementAt(i - 1);
rowDate[j][1] = vector3.elementAt(i - 1);
rowDate[j][2] = vector2.elementAt(i - 1);
rowDate[j][3] = vector1.elementAt(i - 1);
j++;
i--;
}
JTable jTable1 = new JTable(rowDate, columnNames);
JComboBox<String> jComboBox1 = new JComboBox<>();
jComboBox1.addItem("是");
jComboBox1.addItem("否");
jComboBox1.setSelectedIndex(0);
DefaultCellEditor cellEditor1 = new DefaultCellEditor(jComboBox1);
jTable1.getColumnModel().getColumn(4).setCellEditor(cellEditor1);
jComboBox1.setFont(font1);
jComboBox1.setFont(font1);
jTable1.getTableHeader().setFont(font2);
jTable1.setFont(font1);
jTable1.setRowHeight(60);
jTable1.setBackground(null);
jTable1.setPreferredScrollableViewportSize(new Dimension(840, 500));
jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
for (int i = 0; i <= 4; i++) {
if (i == 0)
jTable1.getColumnModel().getColumn(i).setPreferredWidth(200);
else {
jTable1.getColumnModel().getColumn(i).setPreferredWidth(120);
}
}
jTable1.getTableHeader().setResizingAllowed(false);
jTable1.getTableHeader().setReorderingAllowed(false);
JScrollPane jScrollPane1 = new JScrollPane(jTable1);
Box box1 = Box.createVerticalBox();
JButton jButton1 = new JButton("提交");
jButton1.setFont(font1);
Box box01 = Box.createHorizontalBox();
box01.add(Box.createHorizontalStrut(700));
box01.add(jButton1);
box1.add(jScrollPane1);
box1.add(Box.createVerticalStrut(30));
box1.add(box01);
jPanel1.add(box1);
jButton1.addActionListener(e -> {
int num;
int snum = strings1.length - 1;
for (num = 0; num <= snum; num++) {
if (rowDate[num][4] != null) {
if (String.valueOf(rowDate[num][4]).equals("是")) {
try {
Class.forName(name);
Connection con = DriverManager.getConnection(url);
String SQL1 = "select Cid from Course1 where Cname = ?";
PreparedStatement stmt1 = con.prepareStatement(SQL1);
stmt1.setString(1, String.valueOf(rowDate[num][0]));
ResultSet rs1 = stmt1.executeQuery();
int Cid = 0;
while (rs1.next()) {
Cid = (rs1.getInt(1));
}
rs1.close();
String SQL2 = "insert into SC1 values(?,?)";
PreparedStatement stmt2 = con.prepareStatement(SQL2);
stmt2.setInt(1, Cid);
stmt2.setInt(2, Sid);
stmt2.executeUpdate();
String SQL3 = "insert into Course4 values(?,?,?,?,?)";
PreparedStatement stmt3 = con.prepareStatement(SQL3);
stmt3.setInt(1, Cid);
stmt3.setString(2, String.valueOf(rowDate[num][0]));
stmt3.setString(3, String.valueOf(rowDate[num][1]));
stmt3.setString(4, String.valueOf(rowDate[num][2]));
stmt3.setString(5, String.valueOf(rowDate[num][3]));
stmt3.executeUpdate();
String SQL4 = "delete from Course1 where Cid = ?";
PreparedStatement stmt4 = con.prepareStatement(SQL4);
stmt4.setInt(1, Cid);
stmt4.executeUpdate();
con.close();
jFrame.dispose();
innerGui(Sid);
} catch (SQLException e0) {
System.out.println("数据库连接出错!");
} catch (ClassNotFoundException e1) {
System.out.println("找不到目标加载类!");
}
}
}
}
});
i = 0;
Vector<Object> vector5 = new Vector<>();
Vector<Object> vector6 = new Vector<>();
Vector<Object> vector7 = new Vector<>();
Vector<Object> vector8 = new Vector<>();
try {
Class.forName(name);
Connection con = DriverManager.getConnection(url);
String SQL = "Select * from Course2";
PreparedStatement stmt = con.prepareStatement(SQL);
ResultSet rs2 = stmt.executeQuery();
while (rs2.next()) {
vector5.add(rs2.getString(5));
vector6.add(rs2.getString(4));
vector7.add(rs2.getString(3));
vector8.add(rs2.getString(2));
i++;
}
} catch (SQLException e) {
System.out.println("数据库连接出错!");
} catch (ClassNotFoundException e1) {
System.out.println("找不到目标加载类!");
}
Object[] columnNames1 = {"课程名称", "任课老师", "上课时间", "学分", "是否选择"};
Object[][] rowDate2 = new Object[i][5];
String[] strings2 = new String[i];
int j1 = 0;
while (i != 0) {
rowDate2[j1][0] = vector8.elementAt(i - 1);
rowDate2[j1][1] = vector7.elementAt(i - 1);
rowDate2[j1][2] = vector6.elementAt(i - 1);
rowDate2[j1][3] = vector5.elementAt(i - 1);
j1++;
i--;
}
JTable jTable2 = new JTable(rowDate2, columnNames1);
JComboBox<String> jComboBox2 = new JComboBox<>();
jComboBox2.addItem("--请选择--");
jComboBox2.addItem("是");
jComboBox2.addItem("否");
DefaultCellEditor cellEditor2 = new DefaultCellEditor(jComboBox2);
jTable2.getColumnModel().getColumn(4).setCellEditor(cellEditor2);
jComboBox2.setFont(font1);
jTable2.getTableHeader().setFont(font2);
jTable2.setFont(font1);
jTable2.setRowHeight(60);
jTable2.setBackground(null);
jTable2.setPreferredScrollableViewportSize(new Dimension(840, 500));
jTable2.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
for (int i = 0; i <= 4; i++) {
if (i == 0)
jTable2.getColumnModel().getColumn(i).setPreferredWidth(200);
else {
jTable2.getColumnModel().getColumn(i).setPreferredWidth(120);
}
}
jTable2.getTableHeader().setResizingAllowed(false);
jTable2.getTableHeader().setReorderingAllowed(false);
JScrollPane jScrollPane2 = new JScrollPane(jTable2);
Box box2 = Box.createVerticalBox();
JButton jButton2 = new JButton("提交");
jButton2.setFont(font1);
jButton2.addActionListener(e -> {
int num;
int snum = strings2.length - 1;
String[] s = new String[strings2.length];
for (num = 0; num <= snum; num++) {
if (rowDate2[num][4] != null) {
if (String.valueOf(rowDate2[num][4]).equals("是")) {
try {
Class.forName(name);
Connection con = DriverManager.getConnection(url);
String SQL1 = "select Cid from Course2 where Cname = ?";
PreparedStatement stmt1 = con.prepareStatement(SQL1);
stmt1.setString(1, String.valueOf(rowDate2[num][0]));
ResultSet rs1 = stmt1.executeQuery();
int Cid = 0;
while (rs1.next()) {
Cid = (rs1.getInt(1));
}
rs1.close();
System.out.println(Cid);
String SQL2 = "insert into SC1 values(?,?)";
PreparedStatement stmt2 = con.prepareStatement(SQL2);
stmt2.setInt(1, Cid);
stmt2.setInt(2, Sid);
stmt2.executeUpdate();
String SQL3 = "insert into Course4 values(?,?,?,?,?)";
PreparedStatement stmt3 = con.prepareStatement(SQL3);
stmt3.setInt(1, Cid);
stmt3.setString(2, String.valueOf(rowDate2[num][0]));
stmt3.setString(3, String.valueOf(rowDate2[num][1]));
stmt3.setString(4, String.valueOf(rowDate2[num][2]));
stmt3.setString(5, String.valueOf(rowDate2[num][3]));
stmt3.executeUpdate();
String SQL4 = "delete from Course2 where Cid = ?";
PreparedStatement stmt4 = con.prepareStatement(SQL4);
stmt4.setInt(1, Cid);
stmt4.executeUpdate();
con.close();
jFrame.dispose();
innerGui(Sid);
} catch (SQLException e0) {
System.out.println("数据库连接出错!");
} catch (ClassNotFoundException e1) {
System.out.println("找不到目标加载类!");
}
}
}
}
});
Box box02 = Box.createHorizontalBox();
box02.add(Box.createHorizontalStrut(700));
box02.add(jButton2);
box2.add(jScrollPane2);
box2.add(Box.createVerticalStrut(30));
box2.add(box02);
jPanel2.add(box2);
i = 0;
Vector<Object> vector9 = new Vector<>();
Vector<Object> vector10 = new Vector<>();
Vector<Object> vector11 = new Vector<>();
Vector<Object> vector12 = new Vector<>();
try {
Class.forName(name);
Connection con = DriverManager.getConnection(url);
String SQL = "Select * from Course3";
PreparedStatement stmt = con.prepareStatement(SQL);
ResultSet rs3 = stmt.executeQuery();
while (rs3.next()) {
vector9.add(rs3.getString(5));
vector10.add(rs3.getString(4));
vector11.add(rs3.getString(3));
vector12.add(rs3.getString(2));
i++;
}
} catch (SQLException e) {
System.out.println("数据库连接出错!");
} catch (ClassNotFoundException e1) {
System.out.println("找不到目标加载类!");
}
Object[] columnNames3 = {"课程名称", "任课老师", "上课时间", "学分", "是否选择"};
Object[][] rowDate3 = new Object[i][5];
String[] strings3 = new String[i];
int j3 = 0;
while (i != 0) {
rowDate3[j3][0] = vector12.elementAt(i - 1);
rowDate3[j3][1] = vector11.elementAt(i - 1);
rowDate3[j3][2] = vector10.elementAt(i - 1);
rowDate3[j3][3] = vector9.elementAt(i - 1);
j3++;
i--;
}
JTable jTable3 = new JTable(rowDate3, columnNames3);
JComboBox<String> jComboBox3 = new JComboBox<>();
jComboBox3.addItem("--请选择--");
jComboBox3.addItem("是");
jComboBox3.addItem("否");
DefaultCellEditor cellEditor3 = new DefaultCellEditor(jComboBox3);
jTable3.getColumnModel().getColumn(4).setCellEditor(cellEditor3);
jComboBox3.setFont(font1);
jTable3.getTableHeader().setFont(font2);
jTable3.setFont(font1);
jTable3.setRowHeight(60);
jTable3.setBackground(null);
jTable3.setPreferredScrollableViewportSize(new Dimension(840, 500));
jTable3.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
for (int i = 0; i <= 4; i++) {
if (i == 0)
jTable3.getColumnModel().getColumn(i).setPreferredWidth(200);
else {
jTable3.getColumnModel().getColumn(i).setPreferredWidth(120);
}
}
jTable3.getTableHeader().setResizingAllowed(false);
jTable3.getTableHeader().setReorderingAllowed(false);
Box box3 = Box.createVerticalBox();
JScrollPane jScrollPane3 = new JScrollPane(jTable3);
JButton jButton3 = new JButton("提交");
jButton3.setFont(font1);
jButton3.addActionListener(e -> {
int num;
int snum = strings3.length - 1;
String[] s = new String[strings3.length];
for (int z = 0; z <= snum; z++) {
s[z] = String.valueOf(rowDate3[z][4]);
}
int k = 0;
for (num = 0; num <= snum; num++) {
if (rowDate3[num][4] != null) {
if (s[num].equals("是")) {
try {
Class.forName(name);
Connection con = DriverManager.getConnection(url);
String SQL1 = "select Cid from Course3 where Cname = ?";
PreparedStatement stmt1 = con.prepareStatement(SQL1);
stmt1.setString(1, String.valueOf(rowDate3[num][0]));
ResultSet rs1 = stmt1.executeQuery();
int Cid = 0;
while (rs1.next()) {
Cid = (rs1.getInt(1));
}
rs1.close();
System.out.println(Cid);
String SQL2 = "insert into SC1 values(?,?)";
PreparedStatement stmt2 = con.prepareStatement(SQL2);
stmt2.setInt(1, Cid);
stmt2.setInt(2, Sid);
stmt2.executeUpdate();
String SQL3 = "insert into Course4 values(?,?,?,?,?)";
PreparedStatement stmt3 = con.prepareStatement(SQL3);
stmt3.setInt(1, Cid);
stmt3.setString(2, String.valueOf(rowDate3[num][0]));
stmt3.setString(3, String.valueOf(rowDate3[num][1]));
stmt3.setString(4, String.valueOf(rowDate3[num][2]));
stmt3.setString(5, String.valueOf(rowDate3[num][3]));
stmt3.executeUpdate();
String SQL4 = "delete from Course3 where Cid = ?";
PreparedStatement stmt4 = con.prepareStatement(SQL4);
stmt4.setInt(1, Cid);
stmt4.executeUpdate();
con.close();
jFrame.dispose();
innerGui(Sid);
} catch (SQLException e0) {
System.out.println("数据库连接出错!");
} catch (ClassNotFoundException e1) {
System.out.println("找不到目标加载类!");
}
} else {
System.out.println(num + 1 + "选择为否");
}
}
}
});
Box box03 = Box.createHorizontalBox();
box03.add(Box.createHorizontalStrut(700));
box03.add(jButton3);
box3.add(jScrollPane3);
box3.add(Box.createVerticalStrut(30));
box3.add(box03);
jPanel3.add(box3);
jTabbedPane.addTab("公共选修课", jPanel1);
jTabbedPane.addTab("专业选修课", jPanel2);
jTabbedPane.addTab("专业必修课", jPanel3);
jTabbedPane.setFont(font2);
jTabbedPane.setSelectedIndex(0);
}
public void exit(int Sid) {
JFrame jFrame0 = new JFrame("退课窗口");
jFrame0.setSize(1200, 800);
jFrame0.setLocationRelativeTo(null);
jFrame0.setResizable(true);
jFrame0.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
jFrame0.setVisible(true);
JPanel jPanel3 = new JPanel();
i = 0;
Vector<Object> vector9 = new Vector<>();
Vector<Object> vector10 = new Vector<>();
Vector<Object> vector11 = new Vector<>();
Vector<Object> vector12 = new Vector<>();
try {
Class.forName(name);
Connection con = DriverManager.getConnection(url);
String SQL = "Select * from Course4";
PreparedStatement stmt = con.prepareStatement(SQL);
ResultSet rs3 = stmt.executeQuery();
while (rs3.next()) {
vector9.add(rs3.getString(5));
vector10.add(rs3.getString(4));
vector11.add(rs3.getString(3));
vector12.add(rs3.getString(2));
i++;
}
} catch (SQLException e) {
System.out.println("数据库连接出错!");
} catch (ClassNotFoundException e1) {
System.out.println("找不到目标加载类!");
}
Object[] columnNames3 = {"课程名称", "任课老师", "上课时间", "学分", "是否选择"};
Object[][] rowDate4 = new Object[i][5];
String[] strings3 = new String[i];
int j3 = 0;
while (i != 0) {
rowDate4[j3][0] = vector12.elementAt(i - 1);
rowDate4[j3][1] = vector11.elementAt(i - 1);
rowDate4[j3][2] = vector10.elementAt(i - 1);
rowDate4[j3][3] = vector9.elementAt(i - 1);
j3++;
i--;
}
JTable jTable3 = new JTable(rowDate4, columnNames3);
JComboBox<String> jComboBox3 = new JComboBox<>();
jComboBox3.addItem("--请选择--");
jComboBox3.addItem("是");
jComboBox3.addItem("否");
DefaultCellEditor cellEditor3 = new DefaultCellEditor(jComboBox3);
jTable3.getColumnModel().getColumn(4).setCellEditor(cellEditor3);
jComboBox3.setFont(font1);
jTable3.getTableHeader().setFont(font2);
jTable3.setFont(font1);
jTable3.setRowHeight(60);
jTable3.setBackground(null);
jTable3.setPreferredScrollableViewportSize(new Dimension(840, 500));
jTable3.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
for (int i = 0; i <= 4; i++) {
if (i == 0)
jTable3.getColumnModel().getColumn(i).setPreferredWidth(200);
else {
jTable3.getColumnModel().getColumn(i).setPreferredWidth(120);
}
}
jTable3.getTableHeader().setResizingAllowed(false);
jTable3.getTableHeader().setReorderingAllowed(false);
Box box3 = Box.createVerticalBox();
JScrollPane jScrollPane3 = new JScrollPane(jTable3);
JButton jButton3 = new JButton("提交");
jButton3.setFont(font1);
jButton3.addActionListener(e -> {
int num;
int snum = strings3.length - 1;
for (num = 0; num <= snum; num++) {
if (rowDate4[num][4] != null) {
if (rowDate4[num][4].equals("是")) {
try {
int Cnum = 0;
Class.forName(name);
Connection con = DriverManager.getConnection(url);
String SQL1 = "select Cid from Course4 where Cname = ?";
PreparedStatement stmt1 = con.prepareStatement(SQL1);
stmt1.setString(1, String.valueOf(rowDate4[num][0]));
ResultSet rs1 = stmt1.executeQuery();
int Cid = 0;
while (rs1.next()) {
Cid = (rs1.getInt(1));
}
rs1.close();
if (Cid < 200) {
String SQL3 = "insert into Course1 values(?,?,?,?,?)";
PreparedStatement stmt3 = con.prepareStatement(SQL3);
stmt3.setInt(1, Cid);
stmt3.setString(2, String.valueOf(rowDate4[num][0]));
stmt3.setString(3, String.valueOf(rowDate4[num][1]));
stmt3.setString(4, String.valueOf(rowDate4[num][2]));
stmt3.setString(5, String.valueOf(rowDate4[num][3]));
stmt3.executeUpdate();
}
if (Cid < 300 && Cid >= 200) {
String SQL3 = "insert into Course2 values(?,?,?,?,?)";
PreparedStatement stmt3 = con.prepareStatement(SQL3);
stmt3.setInt(1, Cid);
stmt3.setString(2, String.valueOf(rowDate4[num][0]));
stmt3.setString(3, String.valueOf(rowDate4[num][1]));
stmt3.setString(4, String.valueOf(rowDate4[num][2]));
stmt3.setString(5, String.valueOf(rowDate4[num][3]));
stmt3.executeUpdate();
}
if (Cid >= 300) {
String SQL3 = "insert into Course3 values(?,?,?,?,?)";
PreparedStatement stmt3 = con.prepareStatement(SQL3);
stmt3.setInt(1, Cid);
stmt3.setString(2, String.valueOf(rowDate4[num][0]));
stmt3.setString(3, String.valueOf(rowDate4[num][1]));
stmt3.setString(4, String.valueOf(rowDate4[num][2]));
stmt3.setString(5, String.valueOf(rowDate4[num][3]));
stmt3.executeUpdate();
}
String SQL4 = "delete from Course4 where Cid = ?";
PreparedStatement stmt4 = con.prepareStatement(SQL4);
stmt4.setInt(1, Cid);
stmt4.executeUpdate();
stmt4.close();
String SQL2 = "delete from SC1 where Cid = ?";
PreparedStatement stmt2 = con.prepareStatement(SQL2);
stmt2.setInt(1, Cid);
stmt2.executeUpdate();
stmt2.close();
con.close();
jFrame0.dispose();
exit(Sid);
} catch (SQLException e0) {
System.out.println("数据库连接出错!");
} catch (ClassNotFoundException e1) {
System.out.println("找不到目标加载类!");
}
}
}
}
});
Box box03 = Box.createHorizontalBox();
box03.add(Box.createHorizontalStrut(700));
box03.add(jButton3);
box3.add(jScrollPane3);
box3.add(Box.createVerticalStrut(30));
box3.add(box03);
jPanel3.add(box3);
jFrame0.setContentPane(jPanel3);
}
}
use GUI01;
create table Course1(
Cid int not null,
Cname char(10) not null,
Credit char(10)not null,
Tname char(10) not null,
Ctime char(10) not null
primary key clustered (Cid)
);
create table Course2(
Cid int not null,
Cname char(10) not null,
Credit char(10)not null,
Tname char(10) not null,
Ctime char(10) not null,
primary key clustered (Cid),
);
create table Course3(
Cid int not null,
Cname char(10) not null,
Credit char(10)not null,
Tname char(10) not null,
Ctime char(10) not null,
primary key clustered (Cid),
);
create table Teacher(
Tid int not null,
Tname char(10) not null,
Cid int not null,
primary key clustered (Cid),
);
create table Student(
Sid int not null,
Sname char(10) not null,
Sage int not null check (Sage Between 15 and 25 ),
Ssex char(10) not null check (Ssex = '男' or Ssex = '女'),
primary key clustered (Sid));
create table SC(
Sid int not null,
Cid int not null,
primary key clustered (Sid),
foreign key (Sid) references Student(Sid),
foreign key (Cid) references Course1(Cid),
foreign key (Cid) references Course2(Cid),
foreign key (Cid) references Course3(Cid));
create unique index ser_Sid on SC(Sid);
alter table dbo.SC add constraint FK_Course1_SC
foreign key (Cid)
references dbo.Course1(Cid)
alter table dbo.Teacher add constraint FK_Course1_Teacher
foreign key (Cid)
references dbo.Course1(Cid)
alter table dbo.SC add constraint FK_Course2_SC
foreign key (Cid)
references dbo.Course2(Cid)
alter table dbo.Teacher add constraint FK_Course2_Teacher
foreign key (Cid)
references dbo.Course2(Cid)
alter table dbo.SC add constraint FK_Course3_SC
foreign key (Cid)
references dbo.Course3(Cid)
alter table dbo.Teacher add constraint FK_Course3_Teacher
foreign key (Cid)
references dbo.Course3(Cid)
alter table dbo.SC add constraint FK_Student_SC
foreign key (Sid)
references dbo.Student(Sid)
alter table dbo.SC add constraint FK_Teacher_SC
foreign key (Cid)
references dbo.Teacher(Cid)
USE GUI01;
insert into Course1 values(101,'网站设计','曹红杏','1','4');
insert into Course1 values(102,'数据结构','李原','2','3');
insert into Course1 values(103,'Linux教程','马渊博','3','3');
insert into Course1 values(104,'离散数学','成海秀','4','2');
insert into Course1 values(105,'管理学','曾丽玉','5','3');
insert into Course1 values(106,'操作原理','于恒','3','2');
insert into Course2 values(201,'《道德经》导读','丁黎','2','4');
insert into Course2 values(202,'电影赏析','苏昕','3','2');
insert into Course2 values(203,'诗词鉴赏','唐小笑','4','1');
insert into Course3 values(301,'中西方哲学史','黄家瑜','1','4');
insert into Course3 values(302,'近代史纲要','林向','2','3');
use GUI01;
delete from SC1 where Cid = 0;
delete from SC1 where Cid = 101;
delete from SC1 where Cid = 102;
delete from SC1 where Cid = 103;
delete from SC1 where Cid = 104;
delete from SC1 where Cid = 105;
delete from SC1 where Cid = 106;
delete from SC1 where Cid = 107;
delete from SC1 where Cid = 108;
delete from SC1 where Cid = 109;
delete from SC1 where Cid = 201;
delete from SC1 where Cid = 202;
delete from SC1 where Cid = 203;
delete from SC1 where Cid = 301;
delete from SC1 where Cid = 302;
delete from SC1 where Cid = 202;
delete from Course4 where Cid = 101;
delete from Course4 where Cid = 102;
delete from Course4 where Cid = 103;
delete from Course4 where Cid = 104;
delete from Course4 where Cid = 105;
delete from Course4 where Cid = 106;
delete from Course4 where Cid = 107;
delete from Course4 where Cid = 201;
delete from Course4 where Cid = 202;
delete from Course4 where Cid = 203;
delete from Course4 where Cid = 301;
delete from Course4 where Cid = 302;
delete from Course1 where Cid = 101;
delete from Course1 where Cid = 102;
delete from Course1 where Cid = 103;
delete from Course1 where Cid = 104;
delete from Course1 where Cid = 105;
delete from Course1 where Cid = 106;
delete from Course1 where Cid = 107;
delete from Course2 where Cid = 201;
delete from Course2 where Cid = 202;
delete from Course2 where Cid = 203;
delete from Course3 where Cid = 301;
delete from Course3 where Cid = 302;
delete from SC1 where Sid = 1901;
delete from SC1 where Sid = 1902;