学生信息管理系统7

SM.java

import java.awt.Component;

import java.awt.FlowLayout;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.TableCellRenderer;
class SM extends JFrame implements ActionListener {
JPanel p = new JPanel();
JButton btnAdd = new JButton("增加");
JButton btnDelete = new JButton("删除");
JButton btnAlter = new JButton("修改");
JButton btnSearch = new JButton("查询");
JButton btnDisplay = new JButton("显示");
JMenuBar mb = new JMenuBar();
JPanel p1 = new JPanel();
JTable sTable;
JScrollPane scroll;
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
Object[][] playerInfo;
SSelect sst;
String mxh = null;
boolean bstd = false;


SM() {
super("学生信息管理");
add("South", p);
//this.add("Center", p1);
mb.add(btnAdd);
mb.add(btnDelete);
mb.add(btnAlter);
mb.add(btnSearch);
mb.add(btnDisplay);
this.connDB();
//this.display();
this.setBounds(200, 200, 400, 260);
btnAdd.addActionListener(this);
btnDelete.addActionListener(this);
btnAlter.addActionListener(this);
btnSearch.addActionListener(this);
btnDisplay.addActionListener(this);
this.setJMenuBar(mb);
// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
//show();
setVisible(true);
}


SM(SSelect sst) {
super("学生信息管理");
this.sst = sst;
bstd = true;
add("South", p);
//this.add("Center", p1);
mb.add(btnAdd);
mb.add(btnDelete);
mb.add(btnAlter);
mb.add(btnSearch);
mb.add(btnDisplay);
this.connDB();
this.setBounds(200, 200, 400, 260);
btnAdd.addActionListener(this);
btnDelete.addActionListener(this);
btnAlter.addActionListener(this);
btnSearch.addActionListener(this);
btnDisplay.addActionListener(this);
this.setJMenuBar(mb);
// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
show();
}


public void display() {
int i = 0;
int j = 0;
int k = 0;
List al = new ArrayList();
try {
rs = stmt.executeQuery("select * from s");
while (rs.next()) {
al.add(rs.getString("sno"));
al.add(rs.getString("sn"));
al.add(rs.getString("ss"));
al.add(rs.getInt("sa"));
al.add(rs.getString("sd"));
i++;
}
} catch (SQLException e) {
e.printStackTrace();
}
playerInfo = new Object[i][5];
String[] columnNames = { "学号", "姓名", "年龄", "性别", "院系" };


try {
rs = stmt.executeQuery("select * from s order by sno");
while (rs.next()) {
playerInfo[j][0] = rs.getString("sno");
playerInfo[j][1] = rs.getString("sn");
playerInfo[j][2] = rs.getInt("sa");
playerInfo[j][3] = rs.getString("ss");
playerInfo[j][4] = rs.getString("sd");
j++;
}
} catch (SQLException e) {
e.printStackTrace();
}
sTable = new JTable(playerInfo, columnNames);
p1.add(sTable);
scroll = new JScrollPane(sTable);
this.add(scroll);
}


public void connDB() { 
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/tudent",
"root", "123456");
stmt = con.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}


}


public void closeDB() 
{
try {
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}


public void delete() {
String xh = null;
String xm = null;
int nl = 0;
String xb = null;
String yx = null;
int row = -1;
row = sTable.getSelectedRow();
if (row == -1) {
JOptionPane.showMessageDialog(null, "请选择要删除的记录!");
} else {
if (!bstd) {
int j1 = 0;
try {
rs = stmt.executeQuery("select * from s");
while (rs.next() && j1 <= row) {
xh = rs.getString("sno");
xm = rs.getString("sn");
nl = rs.getInt("sa");
xb = rs.getString("ss");
yx = rs.getString("sd");
j1++;
}
} catch (SQLException e) {
e.printStackTrace();
}
int i1 = 0;
try {
int rs1 = stmt.executeUpdate("delete from s where sno='"
+ xh + "'");
stmt.executeUpdate("delete from unpw where un='" + xh + "'");
JOptionPane.showMessageDialog(null, "记录删除成功!");
this.dispose();
new SM().display();
} catch (SQLException e) {
e.printStackTrace();
}
} else {
try {
int rs1 = stmt.executeUpdate("delete from s where sno='"
+ mxh + "'");
stmt.executeUpdate("delete from unpw where un='" + mxh
+ "'");
JOptionPane.showMessageDialog(null, "记录删除成功!");
this.dispose();
new SM().display();
} catch (SQLException e) {
e.printStackTrace();
}
}
}


}


public void update() {
String xh = null;
String xm = null;
int nl = 0;
String xb = null;
String yx = null;
int row = -1;
row = sTable.getSelectedRow();
if (row == -1) {
JOptionPane.showMessageDialog(null, "请选择要修改的记录!");
} else {
int j1 = 0;
try {
if (!bstd) {
rs = stmt.executeQuery("select * from s");
} else {
rs = stmt.executeQuery("select * from s where sno='" + mxh
+ "'");
}
while (rs.next() && j1 <= row) {
xh = rs.getString("sno");
xm = rs.getString("sn");
nl = rs.getInt("sa");
xb = rs.getString("ss");
yx = rs.getString("sd");
j1++;
}
} catch (SQLException e) {
e.printStackTrace();
}
SAdd sadd = new SAdd(xb, yx);
sadd.setTitle("修改");
sadd.tsno.setText(xh);
sadd.tsname.setText(xm);
sadd.tsage.setText("" + nl);
sadd.tsno.setEnabled(false);
this.dispose();
}
}


public void select() {/


mxh = sst.xh;
playerInfo = new Object[1][5];
String[] columnNames = { "学号", "姓名", "年龄", "性别", "院系" };
try {
rs = stmt.executeQuery("select * from s where sno='" + mxh + "'");
while (rs.next()) {
playerInfo[0][0] = rs.getString("sno");
playerInfo[0][1] = rs.getString("sn");
playerInfo[0][2] = rs.getInt("sa");
playerInfo[0][3] = rs.getString("ss");
playerInfo[0][4] = rs.getString("sd");
}
} catch (SQLException e) {
e.printStackTrace();
}
if (playerInfo[0][1] == null) {
this.dispose();
JOptionPane.showMessageDialog(null, "学号不存在!");
new SM().display();
} else {
sTable = new JTable(playerInfo, columnNames);
p1.add(sTable);
scroll = new JScrollPane(sTable);
this.add(scroll);
}
}


public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "增加") {


new SAdd("男", "卫管系");
this.dispose();
}
if (e.getActionCommand() == "删除") {
this.delete();
}
if (e.getActionCommand() == "修改") {
this.update();
}
if (e.getActionCommand() == "查询") {
sst = new SSelect("学号:");
this.dispose();
}
if (e.getActionCommand() == "显示") {
this.dispose();
new SM().display();
}
}

}


CM.java

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.TableCellRenderer;


class CM extends JFrame implements ActionListener {
JPanel p = new JPanel();
JButton btnAdd = new JButton("增加");
JButton btnDelete = new JButton("删除");
JButton btnAlter = new JButton("修改");
JButton btnSearch = new JButton("查询");
JButton btnDisplay = new JButton("显示");
JMenuBar mb = new JMenuBar();
JPanel p1 = new JPanel();;
JTable sTable;
JScrollPane scroll;
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
Object[][] playerInfo;
CSelect cst;
String mkch = null;
boolean bstd = false;


CM(String title) {
super(title);
add("South", p);
//this.add("Center", p1);
mb.add(btnAdd);
mb.add(btnDelete);
mb.add(btnAlter);
mb.add(btnSearch);
mb.add(btnDisplay);
this.connDB();
this.setBounds(200, 200, 400, 260);
btnAdd.addActionListener(this);
btnDelete.addActionListener(this);
btnAlter.addActionListener(this);
btnSearch.addActionListener(this);
btnDisplay.addActionListener(this);
this.setJMenuBar(mb);
// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
show();


}


CM(CSelect cst, String title) {
super(title);
this.cst = cst;
bstd = true;
add("South", p);
//this.add("Center", p1);
mb.add(btnAdd);
mb.add(btnDelete);
mb.add(btnAlter);
mb.add(btnSearch);
mb.add(btnDisplay);
this.connDB();
this.setBounds(200, 200, 400, 260);
btnAdd.addActionListener(this);
btnDelete.addActionListener(this);
btnAlter.addActionListener(this);
btnSearch.addActionListener(this);
btnDisplay.addActionListener(this);
this.setJMenuBar(mb);
// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
show();


}


public void display() {
int i = 0;
int j = 0;
int k = 0;
List al = new ArrayList();
try {
rs = stmt.executeQuery("select * from c");
while (rs.next()) {
al.add(rs.getString("cno"));
al.add(rs.getString("cn"));
al.add(rs.getString("pcno"));
i++;
}
} catch (SQLException e) {
e.printStackTrace();
}
playerInfo = new Object[i][3];
String[] columnNames = { "课程号", "课程名", "先行课程号" };


try {
rs = stmt.executeQuery("select * from c order by cno");
while (rs.next()) {
playerInfo[j][0] = rs.getString("cno");
playerInfo[j][1] = rs.getString("cn");
playerInfo[j][2] = rs.getString("pcno");
j++;
}
} catch (SQLException e) {
e.printStackTrace();
}
sTable = new JTable(playerInfo, columnNames);
p1.add(sTable);
scroll = new JScrollPane(sTable);
this.add(scroll);
}


public void connDB() { 
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/student",
"root", "123456");
stmt = con.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}


}


public void closeDB() 
{
try {
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}


}


public void delete() {
String kch = null;
String kcm = null;
String xxkch = null;


int row = -1;
row = sTable.getSelectedRow();
if (row == -1) {
JOptionPane.showMessageDialog(null, "请选择要删除的记录!");
} else {
if (!bstd) {
int j1 = 0;
try {
rs = stmt.executeQuery("select * from c");
while (rs.next() && j1 <= row) {
kch = rs.getString("cno");
kcm = rs.getString("cn");
xxkch = rs.getString("pcno");
j1++;
}
} catch (SQLException e) {
e.printStackTrace();
}
int i1 = 0;
try {
int rs1 = stmt.executeUpdate("delete from c where cno='"
+ kch + "'");
JOptionPane.showMessageDialog(null, "记录删除成功!");
this.dispose();
new CM("课程信息管理").display();
} catch (SQLException e) {
e.printStackTrace();
}
} else {
try {
int rs1 = stmt.executeUpdate("delete from c where cno='"
+ mkch + "'");
JOptionPane.showMessageDialog(null, "记录删除成功!");
this.dispose();
new CM("课程信息管理").display();
} catch (SQLException e) {
e.printStackTrace();
}
}
}


}


public void update() {
String kch = null;
String kcm = null;
String xxkch = null;


int row = -1;
row = sTable.getSelectedRow();
if (row == -1) {
JOptionPane.showMessageDialog(null, "请选择要修改的记录!");
} else {
int j1 = 0;
try {
if (!bstd) {
rs = stmt.executeQuery("select * from c");
} else {
rs = stmt.executeQuery("select * from c where cno='" + mkch
+ "'");
}
while (rs.next() && j1 <= row) {
kch = rs.getString("cno");
kcm = rs.getString("cn");
xxkch = rs.getString("pcno");
j1++;
}
} catch (SQLException e) {
e.printStackTrace();
}
CAdd cadd = new CAdd();
cadd.setTitle("修改");
cadd.tcno.setText(kch);
cadd.tcname.setText(kcm);
cadd.tpcno.setText(xxkch);
cadd.tcno.setEnabled(false);
this.dispose();
}
}


public void select() {


mkch = cst.kch;
playerInfo = new Object[1][3];
String[] columnNames = { "课程号", "课程名", "先行课程号" };
try {
rs = stmt.executeQuery("select * from c where cno='" + mkch + "'");
while (rs.next()) {
playerInfo[0][0] = rs.getString("cno");
playerInfo[0][1] = rs.getString("cn");
playerInfo[0][2] = rs.getString("pcno");
}
} catch (SQLException e) {
e.printStackTrace();
}
if (playerInfo[0][1] == null) {
this.dispose();
JOptionPane.showMessageDialog(null, "课程号不存在!");
new CM("课程信息管理").display();
} else {
sTable = new JTable(playerInfo, columnNames);
p1.add(sTable);
scroll = new JScrollPane(sTable);
this.add(scroll);
}
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "增加") {
new CAdd();
this.dispose();
}
if (e.getActionCommand() == "删除") {
this.delete();
}
if (e.getActionCommand() == "修改") {
this.update();
}
if (e.getActionCommand() == "查询") {
cst = new CSelect();
this.dispose();
}
if (e.getActionCommand() == "显示") {
this.dispose();
new CM("课程信息管理").display();
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值