菜鸟一只,初次做实训,说实话做的不咋地!以此文为纪念,希望在不久的将来实现那遥不可及的梦想!
DenLu.java
package studentmanage;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration;
import javax.swing.*;
public class DenLu extends JFrame {
public static void main(String[] args) {
new DenLu();
}
JLabel label;
JLabel userNum,userPwd;
JLabel user;
JTextField numField;
JPasswordField pwdField;
JButton button1,button2; //登录 修改密码
JRadioButton btn1,btn2,btn3; //身份 管理员 教师 学生
JPanel panel0,panel1,panel2,panel3,panel4; //即作为容器,又做为组件
JPanel panel;
ButtonGroup btngp;
public DenLu() {
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-800)/2,(screenSize.height-600)/2,800,600);
label=new JLabel("学生信息管理系统",JLabel.CENTER);
label.setFont(new Font("黑体", Font.PLAIN, 40));
userNum=new JLabel("账号",JLabel.CENTER);
userNum.setFont(new Font("宋体", Font.PLAIN, 20));
userPwd=new JLabel("密码",JLabel.CENTER);
userPwd.setFont(new Font("宋体", Font.PLAIN, 20));
user=new JLabel("用户身份",JLabel.CENTER);
user.setFont(new Font("宋体", Font.PLAIN, 20));
button1=new JButton("登录");
button1.setFont(new Font("宋体", Font.PLAIN, 20));
button1.setBackground(Color.WHITE);
button2=new JButton("修改密码");
button2.setFont(new Font("宋体", Font.PLAIN, 20));
button2.setBackground(Color.WHITE);
btn1=new JRadioButton("管理员");
btn1.setFont(new Font("宋体", Font.PLAIN, 15));
btn1.setBackground(Color.WHITE);
btn2=new JRadioButton("教师");
btn2.setFont(new Font("宋体", Font.PLAIN, 15));
btn2.setBackground(Color.WHITE);
btn3=new JRadioButton("学生");
btn3.setFont(new Font("宋体", Font.PLAIN, 15));
btn3.setBackground(Color.WHITE);
btngp=new ButtonGroup();
btngp.add(btn1);
btngp.add(btn2);
btngp.add(btn3);
button1.addActionListener(new Button1ActionListener());
button2.addActionListener(new Button2ActionListener());
numField=new JTextField(25);
pwdField=new JPasswordField(25);
panel0=new JPanel();
panel1=new JPanel();
panel2=new JPanel();
panel3=new JPanel();
panel4=new JPanel();
panel0.setBackground(Color.decode("#A9A9A9"));
panel1.setBackground(Color.decode("#A9A9A9"));
panel2.setBackground(Color.decode("#A9A9A9"));
panel3.setBackground(Color.decode("#A9A9A9"));
panel4.setBackground(Color.decode("#A9A9A9"));
panel=new JPanel();
add(panel);
panel0.add(label);
panel1.add(userNum);
panel1.add(numField);
panel2.add(userPwd);
panel2.add(pwdField);
panel3.add(user);
panel3.add(btn1);
panel3.add(btn2);
panel3.add(btn3);
panel4.add(button1);
panel4.add(button2);
panel.add(panel0);
panel.add(panel1);
panel.add(panel2);
panel.add(panel3);
panel.add(panel4);
panel.setLayout(new GridLayout(5,1,0,0)); //网格布局
setTitle("登录界面");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
class Button1ActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String num,pwd,user = null;
num=numField.getText();
pwd=pwdField.getText();
if(btn1.isSelected()) {
user=btn1.getText();
}else if(btn2.isSelected()) {
user=btn2.getText();
}else if(btn3.isSelected()) {
user=btn3.getText();
}
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //加载数据库驱动
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
try{
Connection con;
Statement sql;
ResultSet rs;
String url,userName,userPwd;
// 连接数据库的语句
url="jdbc:sqlserver://localhost:1433;DatabaseName=StudentInfo";
userName="sa";
userPwd="123";
con=DriverManager.getConnection(url,userName,userPwd);
sql=con.createStatement();
rs=sql.executeQuery("select * from 用户信息表 where 账号 ='"+num+"' and 密码='"+pwd+"' and 用户='"+user+"'");
int q=0;
while(rs.next()){
q++;
}
if(q>0){
JOptionPane.showMessageDialog(null, "登陆成功!","消息对话框",JOptionPane.WARNING_MESSAGE);
dispose();
if(user=="管理员") {
new Admin_UI();
}else if(user=="教师") {
new Teach_UI();
}else if(user=="学生") {
new Stud_UI(numField);
}
}
else
JOptionPane.showMessageDialog(null, "账号、密码、身份选择错误!","消息对话框",JOptionPane.WARNING_MESSAGE);
}
catch(SQLException exp){
System.out.println(exp);
}
}
}
class Button2ActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String num,pwd,user = null;
num=numField.getText();
pwd=pwdField.getText();
if(btn1.isSelected()) {
user=btn1.getText();
}else if(btn2.isSelected()) {
user=btn2.getText();
}else if(btn3.isSelected()) {
user=btn3.getText();
}
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //加载数据库驱动
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
try{
Connection con;
Statement sql;
ResultSet rs;
String url,userName,userPwd;
// 连接数据库的语句
url="jdbc:sqlserver://localhost:1433;DatabaseName=StudentInfo";
userName="sa";
userPwd="123";
con=DriverManager.getConnection(url,userName,userPwd);
sql=con.createStatement();
rs=sql.executeQuery("select * from 用户信息表 where 账号 ='"+num+"' and 密码='"+pwd+"' and 用户='"+user+"'");
int q=0;
while(rs.next()){
q++;
}
if(q>0){
JOptionPane.showMessageDialog(null, "您有权利修改密码","消息对话框",JOptionPane.WARNING_MESSAGE);
dispose();
new UpPsw();
}
else
JOptionPane.showMessageDialog(null, "账号、密码、身份选择错误不可修改密码!","消息对话框",JOptionPane.WARNING_MESSAGE);
}
catch(SQLException exp){
System.out.println(exp);
}
}
}
}
Admin_UI.java
package studentmanage;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import studentmanage.UpPsw.BtnActionListener;
public class Admin_UI extends JFrame{
CardLayout cardLayout=new CardLayout();
CardLayout card=new CardLayout();
JMenuBar menubar=new JMenuBar();
JMenu menu=new JMenu("管理员权限↓");
JMenuItem item1=new JMenuItem("录入学生信息");
JMenuItem item2=new JMenuItem("删除学生信息");
JMenuItem item3=new JMenuItem("查找学生信息");
JMenuItem item4=new JMenuItem("修改学生信息");
JPanel panel1=new JPanel();
JPanel panel1B=new JPanel();
JPanel panel1C=new JPanel();
JPanel panel2=new JPanel();
JPanel panel2B=new JPanel();
JPanel panel2C=new JPanel();
JPanel panel3=new JPanel();
JPanel panel3B=new JPanel();
JPanel panel3C=new JPanel();
JPanel panel4=new JPanel();
JPanel panel4B=new JPanel();
JPanel panel4C=new JPanel();
JPanel panel=new JPanel();
JPanel panel5=new JPanel();
JPanel panel6=new JPanel();
JPanel p=new JPanel();
JPanel p0=new JPanel();
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JPanel p4=new JPanel();
JLabel userNum=new JLabel("学生学号");
JLabel userName=new JLabel("学生姓名");
JTextField numField=new JTextField(15);
JTextField nameField=new JTextField(15);
JButton btn1=new JButton("提交");
JButton btn2=new JButton("删除");
JButton btn3=new JButton("查询");
JButton btn4=new JButton("修改");
JButton btn5=new JButton("确认修改");
String[] columnName= {"属性","信息"};
String[][] data= {
{"学号",""},{"姓名",""},{"性别",""},{"班级",""},{"出生年月",""},{"家庭地址",""},{"院系",""},{"入学时间",""}};
JTable table1,table2;
JScrollPane ScrollPane = new JScrollPane();
public Admin_UI() {
table1=new JTable(data,columnName);
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-800)/2,(screenSize.height-600)/2,800,600);
setTitle("学生信息管理系统——管理员");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
menu.add(item1);
menu.add(item2);
menu.add(item3);
menu.add(item4);
menubar.add(menu);
this.setJMenuBar(menubar);
panel.setLayout(cardLayout);
panel.add(panel1,"1");
panel.add(panel2,"2");
panel.add(panel3,"3");
panel.add(panel4,"4");
panel.add(panel5,"5");
panel.add(panel6,"6");
panel.setBackground(Color.decode("#A9A9A9"));
panel1.setBackground(Color.decode("#A9A9A9"));
panel2.setBackground(Color.decode("#A9A9A9"));
panel2B.setBackground(Color.decode("#A9A9A9"));
panel2C.setBackground(Color.decode("#A9A9A9"));
panel3.setBackground(Color.decode("#A9A9A9"));
panel3B.setBackground(Color.decode("#A9A9A9"));
panel3C.setBackground(Color.decode("#A9A9A9"));
panel4.setBackground(Color.decode("#A9A9A9"));
panel4B.setBackground(Color.decode("#A9A9A9"));
panel4C.setBackground(Color.decode("#A9A9A9"));
panel5.setBackground(Color.decode("#A9A9A9"));
panel6.setBackground(Color.decode("#A9A9A9"));
p.setBackground(Color.decode("#A9A9A9"));
p0.setBackground(Color.decode("#A9A9A9"));
p1.setBackground(Color.decode("#A9A9A9"));
p2.setBackground(Color.decode("#A9A9A9"));
p3.setBackground(Color.decode("#A9A9A9"));
p4.setBackground(Color.decode("#A9A9A9"));
add(panel);
item1.addActionListener(new Item1ActionListener());
item2.addActionListener(new Item2ActionListener());
item3.addActionListener(new Item3ActionListener());
item4.addActionListener(new Item4ActionListener());
}
//录入学生信息 成功
class Item1ActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
table1.setRowHeight(60);
table1.getColumnModel().getColumn(0).setPreferredWidth(250);
table1.getColumnModel().getColumn(1).setPreferredWidth(250);
ScrollPane.setViewportView(table1);
panel1B.add(ScrollPane,BorderLayout.CENTER);
panel1C.add(btn1);
panel1B.setBackground(Color.decode("#A9A9A9"));
panel1C.setBackground(Color.decode("#A9A9A9"));
BorderLayout borderLayout=new BorderLayout();
panel1.setLayout(borderLayout);
panel1.add(panel1B,BorderLayout.CENTER);
panel1.add(panel1C,BorderLayout.SOUTH);
cardLayout.first(panel);
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String data[]=new String[8];
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //加载数据库驱动
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
try{
Connection con;
String sql;
PreparedStatement pst;
ResultSet rs;
String url,userName,userPwd;
// 连接数据库的语句
url="jdbc:sqlserver://localhost:1433;DatabaseName=StudentInfo";
userName="sa";
userPwd="123";
con=DriverManager.getConnection(url,userName,userPwd);
sql="insert into 学生个人档案信息(学号,姓名,性别,班级,出生年月,家庭地址,院系,入学时间) values(?,?,?,?,?,?,?,?) ";
pst = con.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
for(int i=0;i<8;i++) {
data[i]=table1.getValueAt(i,1).toString();
}
for(int i=0;i<8;i++) {
if(data[i].equals("")) {
JOptionPane.showMessageDialog(null, "录入信息不完整无法提交!","消息对话框",JOptionPane.WARNING_MESSAGE);
break;
}
}
for(int i=0;i<8;i++) {
pst.setString(i+1,data[i]);
}
pst.executeUpdate();
pst.close();
//con.close();
JOptionPane.showMessageDialog(null, "录入信息成功!","消息对话框",JOptionPane.WARNING_MESSAGE);
dispose();
}
catch(SQLException exp){
System.out.println(exp);
}
}
});
}
}
//删除学生信息 有问题
class Item2ActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
p1.add(userNum);
p1.add(numField);
p2.add(userName);
p2.add(nameField);
p.add(p1);
p.add(p2);
p.setLayout((new GridLayout(2,1,0,100)));
panel2B.add(p,BorderLayout.CENTER);
panel2C.add(btn2);
panel2.add(panel2B);
panel2.add(panel2C);
panel2.setLayout(new GridLayout(2,1,0,100));
cardLayout.show(panel,"2");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num,name;
num=numField.getText();
name=nameField.getText();
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //加载数据库驱动
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
try{
Connection con;
String sql;
PreparedStatement pst;
ResultSet rs;
String url,userName,userPwd;
// 连接数据库的语句
url="jdbc:sqlserver://localhost:1433;DatabaseName=StudentInfo";
userName="sa";
userPwd="123";
con=DriverManager.getConnection(url,userName,userPwd);
sql="delete from 学生个人档案信息 where 学号 ='"+num+"' and 姓名='"+name+"'";
pst = con.prepareStatement(sql);
int t=pst.executeUpdate();
if(t>0) {
JOptionPane.showMessageDialog(null, "已删除该学生信息!","消息对话框",JOptionPane.WARNING_MESSAGE);
}else {
JOptionPane.showMessageDialog(null, "删除该学生信息失败!","消息对话框",JOptionPane.WARNING_MESSAGE);
}
con.close();
dispose();
}
catch(SQLException exp){
System.out.println(exp);
}
}
});
}
}
//查询学生信息 失败
class Item3ActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
p3.add(userNum);
p3.add(numField);
panel3B.add(p3,BorderLayout.CENTER);
panel3C.add(btn3);
panel3.add(panel3B);
panel3.add(panel3C);
panel3.setLayout(new GridLayout(2,1,0,100));
cardLayout.show(panel,"3");
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num;
String xh = null,xm = null,xb= null,bj= null,csny= null,yx= null,rxsj= null,jtdz= null;
num=numField.getText();
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //加载数据库驱动
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
try{
Connection con;
String sql;
PreparedStatement pst;
ResultSet rs;
String url,userName,userPwd;
// 连接数据库的语句
url="jdbc:sqlserver://localhost:1433;DatabaseName=StudentInfo";
userName="sa";
userPwd="123";
con=DriverManager.getConnection(url,userName,userPwd);
sql="select 学号,姓名,性别,班级,出生年月,家庭地址,院系,入学时间 from 学生个人档案信息 where 学号='"+num+"'";
pst = con.prepareStatement(sql);
rs=pst.executeQuery();
while(rs.next()) {
xh=rs.getString("学号");
xm=rs.getString("姓名");
xb=rs.getString("性别");
bj=rs.getString("班级");
csny=rs.getString("出生年月");
jtdz=rs.getString("家庭地址");
yx=rs.getString("院系");
rxsj=rs.getString("入学时间");
}
String[][] data11= {
{"学号",xh},{"姓名",xm},{"性别",xb},{"班级",bj},{"出生年月",csny},{"家庭地址",jtdz},{"院系",yx},{"入学时间",rxsj}
};
JTable tb=new JTable(data11,columnName);
tb.setRowHeight(55);
tb.getColumnModel().getColumn(0).setPreferredWidth(250);
tb.getColumnModel().getColumn(1).setPreferredWidth(250);
panel5.add(tb);
cardLayout.show(panel, "5");
}
catch(SQLException exp){
System.out.println(exp);
}
}
});
}
}
//修改学生信息 失败
class Item4ActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
p4.add(userNum);
p4.add(numField);
panel4B.add(p4,BorderLayout.CENTER);
panel4C.add(btn4);
panel4.add(panel4B);
panel4.add(panel4C);
panel4.setLayout(new GridLayout(2,1,0,100));
cardLayout.show(panel,"4");
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JTable tb;
String num;
String xh = null,xm = null,xb= null,bj= null,csny= null,yx= null,rxsj= null,jtdz= null;
num=numField.getText();
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //加载数据库驱动
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
try{
Connection con;
String sql;
PreparedStatement pst;
ResultSet rs;
String url,userName,userPwd;
// 连接数据库的语句
url="jdbc:sqlserver://localhost:1433;DatabaseName=StudentInfo";
userName="sa";
userPwd="123";
con=DriverManager.getConnection(url,userName,userPwd);
sql="select 学号,姓名,性别,班级,出生年月,家庭地址,院系,入学时间 from 学生个人档案信息 where 学号='"+num+"'";
pst = con.prepareStatement(sql);
rs=pst.executeQuery();
while(rs.next()) {
xh=rs.getString("学号");
xm=rs.getString("姓名");
xb=rs.getString("性别");
bj=rs.getString("班级");
csny=rs.getString("出生年月");
jtdz=rs.getString("家庭地址");
yx=rs.getString("院系");
rxsj=rs.getString("入学时间");
}
String[] columnName= {"属性","信息"};
String[][] data= {
{"学号",xh},{"姓名",xm},{"性别",xb},{"班级",bj},{"出生年月",csny},{"家庭地址",jtdz},{"院系",yx},{"入学时间",rxsj}
};
tb=new JTable(data,columnName);
tb.setRowHeight(40);
tb.getColumnModel().getColumn(0).setPreferredWidth(250);
tb.getColumnModel().getColumn(1).setPreferredWidth(250);
panel6.add(tb);
panel6.add(btn5);
cardLayout.show(panel, "6");
//确认修改按钮监视器
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String data1[]=new String[8];
for(int i=0;i<8;i++) {
data1[i]=tb.getValueAt(i,1).toString();
}
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //加载数据库驱动
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
try{
Connection con;
Statement sql;
int k;
String url,userName,userPwd;
// 连接数据库的语句
url="jdbc:sqlserver://localhost:1433;DatabaseName=StudentInfo";
userName="sa";
userPwd="123";
con=DriverManager.getConnection(url,userName,userPwd);
sql=con.createStatement();
k=sql.executeUpdate("update 学生个人档案信息 set 姓名='"+data1[1]+"' ,性别='"+data1[2]+"' ,班级='"+data1[3]+"' ,出生年月='"+data1[4]+"' ,家庭地址='"+data1[5]+"' ,院系='"+data1[6]+"' ,入学时间='"+data1[7]+"' where 学号='"+data1[0]+"'");
if(k>0) {
JOptionPane.showMessageDialog(null, "信息修改成功!","消息对话框",JOptionPane.WARNING_MESSAGE);
}
else {
JOptionPane.showMessageDialog(null, "信息修改失败!","消息对话框",JOptionPane.WARNING_MESSAGE);
}
dispose();
}
catch(SQLException exp){
System.out.println(exp);
}
}
});
}
catch(SQLException exp){
System.out.println(exp);
}
}
});
}
}
}
Teach_UI.java
package studentmanage;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import studentmanage.UpPsw.BtnActionListener;
public class Teach_UI extends JFrame{
CardLayout cardLayout=new CardLayout();
BorderLayout borderLayout=new BorderLayout();
JComboBox<String> c1 = new JComboBox<String>();//创建下拉列表
JComboBox<String> c2 = new JComboBox<String>();//创建下拉列表
JComboBox<String> c3 = new JComboBox<String>();//创建下拉列表
JComboBox<String> c4 = new JComboBox<String>();//创建下拉列表
JComboBox<String> c5 = new JComboBox<String>();//创建下拉列表
JComboBox<String> c6 = new JComboBox<String>();//创建下拉列表
JLabel l1 = new JLabel("班级:");
JLabel l2 = new JLabel("学期:");
JMenuBar menubar=new JMenuBar();
JMenu menu=new JMenu("教师权限");
JMenuItem item1=new JMenuItem("录入学生成绩");
JMenuItem item2=new JMenuItem("删除学生成绩");
JMenuItem item3=new JMenuItem("查找学生成绩");
JMenuItem item4=new JMenuItem("修改学生成绩");
JPanel panel1=new JPanel();
JPanel panel1B=new JPanel();
JPanel panel1C=new JPanel();
JPanel panel2=new JPanel();
JPanel panel2B=new JPanel();
JPanel panel2C=new JPanel();
JPanel panel3=new JPanel();
JPanel panel3B=new JPanel();
JPanel panel3C=new JPanel();
JPanel panel4=new JPanel();
JPanel panel4B=new JPanel();
JPanel panel4C=new JPanel();
JPanel panel=new JPanel();
JPanel panel5=new JPanel();
JPanel panel6=new JPanel();
JPanel p=new JPanel();
JPanel p0=new JPanel();
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JPanel p4=new JPanel();
JButton btn0=new JButton("确认班级和学期");
//JButton btn00=new JButton("确认");
JButton btn1=new JButton("提交成绩");
JButton btn2=new JButton("删除成绩");
JButton btn3=new JButton("查询成绩");
JButton btn4=new JButton("修改成绩");
JButton btn5=new JButton("确认修改");
JScrollPane ScrollPane = new JScrollPane();
String c1s=null;
String c2s=null;
JLabel userNum=new JLabel("学生学号");
JLabel userName=new JLabel("学生姓名");
JTextField numField=new JTextField(15);
JTextField nameField=new JTextField(15);
JTable table2,jTable;
//JTable table1=new JTable(data,columnName);
public Teach_UI() {
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-800)/2,(screenSize.height-600)/2,800,600);
setTitle("学生信息管理系统——教师");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
//下列列表中选项
c1.addItem("软件1701");
c1.addItem("软件1702");
c2.addItem("2019秋季学期");
c2.addItem("2020春季学期");
c3.addItem("软件1701");
c3.addItem("软件1702");
c4.addItem("2019秋季学期");
c4.addItem("2020春季学期");
c5.addItem("软件1701");
c5.addItem("软件1702");
c6.addItem("2019秋季学期");
c6.addItem("2020春季学期");
menu.add(item1);
menu.add(item3);
menu.add(item4);
menubar.add(menu);
this.setJMenuBar(menubar);
panel1.setLayout(borderLayout); //panel1容器
panel.setLayout(cardLayout); //卡片布局
panel.add(panel1,"1"); //panel1相当于panel的组件
panel.add(panel2,"2");
panel.add(panel3,"3");
panel.add(panel4,"4");
panel.add(panel5,"5");
panel.add(panel6,"6");
panel.setBackground(Color.decode("#A9A9A9"));
panel1.setBackground(Color.decode("#A9A9A9"));
panel1C.setBackground(Color.decode("#A9A9A9"));
panel1B.setBackground(Color.decode("#A9A9A9"));
panel2.setBackground(Color.decode("#A9A9A9"));
panel2B.setBackground(Color.decode("#A9A9A9"));
panel2C.setBackground(Color.decode("#A9A9A9"));
panel3.setBackground(Color.decode("#A9A9A9"));
panel3B.setBackground(Color.decode("#A9A9A9"));
panel3C.setBackground(Color.decode("#A9A9A9"));
panel4.setBackground(Color.decode("#A9A9A9"));
panel4B.setBackground(Color.decode("#A9A9A9"));
panel4C.setBackground(Color.decode("#A9A9A9"));
panel5.setBackground(Color.decode("#A9A9A9"));
panel6.setBackground(Color.decode("#A9A9A9"));
p.setBackground(Color.decode("#A9A9A9"));
p0.setBackground(Color.decode("#A9A9A9"));
p1.setBackground(Color.decode("#A9A9A9"));
p2.setBackground(Color.decode("#A9A9A9"));
p3.setBackground(Color.decode("#A9A9A9"));
p4.setBackground(Color.decode("#A9A9A9"));
add(panel);
item1.addActionListener(new Item1ActionListener());
//item2.addActionListener(new Item2ActionListener());
item3.addActionListener(new Item3ActionListener());
item4.addActionListener(new Item4ActionListener());
}
public Vector getNextRow(ResultSet rs, ResultSetMetaData rsmd) throws SQLException {
Vector currentRow = new Vector(); // 定义一个向量,用于存放记录
for (int i = 1; i <= rsmd.getColumnCount(); ++i)
currentRow.addElement(rs.getString(i)); // 获取记录
return currentRow; // 返回记录
}
public void displayResultSet(ResultSet rs,JPanel p) throws SQLException {
boolean moreRecords = rs.next(); // 定位到达第一条记录
if (!moreRecords) {
JOptionPane.showMessageDialog(null, "结果集中无记录", "无记录",
JOptionPane.INFORMATION_MESSAGE);
dispose();
return;
}
Vector rows = new Vector();
Vector columnHeads = new Vector();
try {
ResultSetMetaData rsmd = rs.getMetaData(); // 获得rs结果集中列属性信息
for (int i = 1; i <= rsmd.getColumnCount(); ++i)
columnHeads.addElement(rsmd.getColumnName(i)); // 获得列名(将列名存放至向量columnHeads)
do {
rows.addElement(getNextRow(rs, rsmd));
}
while (rs.next()); // 利用循环获得所有记录
jTable = new JTable(rows, columnHeads); // 将获得的行列数据信息作为参数重新构造表格视图
jTable.setRowHeight(55);
jTable.getColumnModel().getColumn(0).setPreferredWidth(200);
jTable.getColumnModel().getColumn(1).setPreferredWidth(200);
jTable.getColumnModel().getColumn(2).setPreferredWidth(200);
jTable.getColumnModel().getColumn(3).setPreferredWidth(200);
JScrollPane scroller = new JScrollPane(jTable);// 创建带有滚动条的面板,并将表格视图加入
p.add(scroller, BorderLayout.CENTER); // 将面板重新加入溶器中
} catch (SQLException e) {
e.printStackTrace();
}
}
//录入学生成绩
class Item1ActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
//p4.removeAll();
panel1B.add(btn1);
panel1C.add(l1);
panel1C.add(c1);
panel1C.add(l2);
panel1C.add(c2);
panel1C.add(btn0);
panel1.add(panel1C,borderLayout.NORTH);
panel1.add(p1,borderLayout.CENTER);
panel1.add(panel1B,borderLayout.SOUTH);
cardLayout.first(panel);
c1s=c1.getSelectedItem().toString();
c2s=c2.getSelectedItem().toString();
//确认按钮 --------------------------------------------------------------------------------------------------
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
p1.removeAll();
if(c1s.equals("软件1701")&&c2s.equals("2019秋季学期")) {
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //加载数据库
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
try{
Connection con; //连接数据库
String sql;
PreparedStatement pst;
ResultSet rs;
String url,userName,userPwd;
// 连接数据库的语句
url="jdbc:sqlserver://localhost:1433;DatabaseName=StudentInfo";
userName="sa";
userPwd="123";
con=DriverManager.getConnection(url,userName,userPwd);
sql="select * from 软件1701班2019秋季学期成绩表 "; //*全部
pst = con.prepareStatement(sql);
rs=pst.executeQuery(); //调用语句 数据库的数据————>
displayResultSet(rs,p1);
}
catch(SQLException exp){
System.out.println(exp);
}
}else if(c1s.equals("软件1701")&&c2s.equals("2020春季学期")) {
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
try{
Connection con;
String sql;
PreparedStatement pst;
ResultSet rs;
String url,userName,userPwd;
// 连接数据库的语句
url="jdbc:sqlserver://localhost:1433;DatabaseName=StudentInfo";
userName="sa";
userPwd="123";
con=DriverManager.getConnection(url,userName,userPwd);
sql="select * from 软件1701班2020春季学期成绩表 ";
pst = con.prepareStatement(sql);
rs=pst.executeQuery();
displayResultSet(rs,p1);
}
catch(SQLException exp){
System.out.println(exp);
}
}else if(c1s.equals("软件1702")&&c2s.equals("2019秋季学期")) {
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
try{
Connection con;
String sql;
PreparedStatement pst;
ResultSet rs;
String url,userName,userPwd;
// 连接数据库的语句
url="jdbc:sqlserver://localhost:1433;DatabaseName=StudentInfo";
userName="sa";
userPwd="123";
con=DriverManager.getConnection(url,userName,userPwd);
sql="select * from 软件1702班2019秋季学期成绩表 ";
pst = con.prepareStatement(sql);
rs=pst.executeQuery();
displayResultSet(rs,p1);
}
catch(SQLException exp){
System.out.println(exp);
}
}else {
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
try{
Connection con;
String sql;
PreparedStatement pst;
ResultSet rs;
String url,userName,userPwd;
// 连接数据库的语句
url="jdbc:sqlserver://localhost:1433;DatabaseName=StudentInfo";
userName="sa";
userPwd="123";
con=DriverManager.getConnection(url,userName,userPwd);
sql="select * from 软件1702班2020春季学期成绩表 ";
pst = con.prepareStatement(sql);
rs=pst.executeQuery();
displayResultSet(rs,p1);
}
catch(SQLException exp){
System.out.println(exp);
}
}
}
});
//p1.removeAll();
//确认提交 按钮----------------------------------------------------------------------------------------------
btn1.addActionListener(new ActionListener() { //给按钮添加监视器 按下按钮有反应
public void actionPerformed(ActionEvent e) {
String data[][]=new String[8][4];
int n = 0;
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
try{
Connection con;
String sql;
PreparedStatement pst;
ResultSet rs;
String url,userName,userPwd;
// 连接数据库的语句
url="jdbc:sqlserver://localhost:1433;DatabaseName=StudentInfo";
userName="sa";
userPwd="123";
con=DriverManager.getConnection(url,userName,userPwd);
for(int i=0;i<jTable.getRowCount();i++) {
for(int j=0;j<jTable.getColumnCount();j++) {
data[i][j]=jTable.getValueAt(i,j).toString();
}
}
if(c1s.equals("软件1701")&&c2s.equals("2019秋季学期")) {
for(int i=0;i<jTable.getRowCount();i++) {
//pst = con.createStatement();
//pst.executeQuery("update 软件1701班2019秋季学期成绩表 set 成绩='"+data[i][3]+"' where 学号='"+data[i][0]+"'");
pst = con.prepareStatement("update 软件1701班2019秋季学期成绩表 set 成绩='"+data[i][3]+"' where 学号='"+data[i][0]+"'");
pst.executeUpdate();
}
}else if(c1s.equals("软件1701")&&c2s.equals("2020春季学期")) {
for(int i=0;i<jTable.getRowCount();i++) {
//pst = con.createStatement();
//pst.executeQuery("update 软件1701班2020春季学期成绩表 set 成绩='\"+data[i][3]+\"' where 学号='\"+data[i][0]+\"'");
pst = con.prepareStatement("update 软件1701班2020春季学期成绩表 set 成绩='"+data[i][3]+"' where 学号='"+data[i][0]+"'");
pst.executeUpdate();
}
}else if(c1s.equals("软件1702")&&c2s.equals("2019秋季学期")) {
for(int i=0;i<jTable.getRowCount();i++) {
//pst = con.createStatement();
//pst.executeQuery("update 软件1702班2019秋季学期成绩表 set 成绩='"+data[i][3]+"' where 学号='"+data[i][0]+"'");
pst = con.prepareStatement("update 软件1702班2019秋季学期成绩表 set 成绩='"+data[i][3]+"' where 学号='"+data[i][0]+"'");
pst.executeUpdate();
}
}else {
for(int i=0;i<jTable.getRowCount();i++) {
//pst = con.createStatement();
//pst.executeQuery("update 软件1702班2020春季学期成绩表 set 成绩='"+data[i][3]+"' where 学号='"+data[i][0]+"'");
pst = con.prepareStatement("update 软件1702班2020春季学期成绩表 set 成绩='"+data[i][3]+"' where 学号='"+data[i][0]+"'");
pst.executeUpdate();
}
}
con.close();
JOptionPane.showMessageDialog(null, "录入成绩成功!","消息对话框",JOptionPane.WARNING_MESSAGE);
//dispose();
}
catch(SQLException exp){
System.out.println(exp);
}
}
});
}
}
//删除学生成绩 --------------------------------------------------------------------------------------------------------------
/*
class Item2ActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
*/
//查找学生成绩--------------------------------------------------------------------------------------------------------------
class Item3ActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
//p4.removeAll();
p0.add(l1);
p0.add(c3);
p0.add(l2);
p0.add(c4);
p0.add(btn3);
c1s=c3.getSelectedItem().toString();
c2s=c4.getSelectedItem().toString();
//panel3B.add(p0,BorderLayout.NORTH);
panel3.setLayout(new BorderLayout());
panel3.add(p0,BorderLayout.NORTH);
panel3.add(p2,BorderLayout.CENTER);
cardLayout.show(panel,"3");
//确认查询按钮
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
p2.removeAll();
if(c1s.equals("软件1701")&&c2s.equals("2019秋季学期")) {
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
try{
Connection con;
String sql;
PreparedStatement pst;
ResultSet rs;
String url,userName,userPwd;
// 连接数据库的语句
url="jdbc:sqlserver://localhost:1433;DatabaseName=StudentInfo";
userName="sa";
userPwd="123";
con=DriverManager.getConnection(url,userName,userPwd);
sql="select * from 软件1701班2019秋季学期成绩表 ";
pst = con.prepareStatement(sql);
rs=pst.executeQuery();
displayResultSet(rs,p2);
}
catch(SQLException exp){
System.out.println(exp);
}
}else if(c1s.equals("软件1701")&&c2s.equals("2020春季学期")) {
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
try{
Connection con;
String sql;
Statement pst;
ResultSet rs;
String url,userName,userPwd;
// 连接数据库的语句
url="jdbc:sqlserver://localhost:1433;DatabaseName=StudentInfo";
userName="sa";
userPwd="123";
con=DriverManager.getConnection(url,userName,userPwd);
sql="select * from 软件1701班2020春季学期成绩表 ";
pst = con.createStatement();
rs=pst.executeQuery(sql);
displayResultSet(rs,p2);
}
catch(SQLException exp){
System.out.println(exp);
}
}else if(c1s.equals("软件1702")&&c2s.equals("2019秋季学期")) {
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
try{
Connection con;
String sql;
PreparedStatement pst;
ResultSet rs;
String url,userName,userPwd;
// 连接数据库的语句
url="jdbc:sqlserver://localhost:1433;DatabaseName=StudentInfo";
userName="sa";
userPwd="123";
con=DriverManager.getConnection(url,userName,userPwd);
sql="select * from 软件1702班2019秋季学期成绩表 ";
pst = con.prepareStatement(sql);
rs=pst.executeQuery();
displayResultSet(rs,p2);
}
catch(SQLException exp){
System.out.println(exp);
}
}else {
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
try{
Connection con;
String sql;
PreparedStatement pst;
ResultSet rs;
String url,userName,userPwd;
// 连接数据库的语句
url="jdbc:sqlserver://localhost:1433;DatabaseName=StudentInfo";
userName="sa";
userPwd="123";
con=DriverManager.getConnection(url,userName,userPwd);
sql="select * from 软件1702班2020春季学期成绩表 ";
pst = con.prepareStatement(sql);
rs=pst.executeQuery();
displayResultSet(rs,p2);
}
catch(SQLException exp){
System.out.println(exp);
}
}
}
});
}
}
//修改学生成绩 ------------------------------------------------------------------------------------------------------------
class Item4ActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
//p4.removeAll();
p.add(l1);
p.add(c5);
p.add(l2);
p.add(c6);
p.add(btn4); //修改成绩按钮
p3.add(btn5);
panel4.setLayout(new BorderLayout());
panel4.add(p,BorderLayout.NORTH);
panel4.add(p4,BorderLayout.CENTER);
panel4.add(p3,BorderLayout.SOUTH);
cardLayout.show(panel,"4");
c1s=c5.getSelectedItem().toString();
c2s=c6.getSelectedItem().toString();
//修改成绩-----------------------------------------------------------------------------------------
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
p4.removeAll();
if(c1s.equals("软件1701")&&c2s.equals("2019秋季学期")) {
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
try{
Connection con;
String sql;
PreparedStatement pst;
ResultSet rs;
String url,userName,userPwd;
// 连接数据库的语句
url="jdbc:sqlserver://localhost:1433;DatabaseName=StudentInfo";
userName="sa";
userPwd="123";
con=DriverManager.getConnection(url,userName,userPwd);
sql="select * from 软件1701班2019秋季学期成绩表 ";
pst = con.prepareStatement(sql);
rs=pst.executeQuery();
displayResultSet(rs,p4);
}
catch(SQLException exp){
System.out.println(exp);
}
}else if(c1s.equals("软件1701")&&c2s.equals("2020春季学期")) {
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
try{
Connection con;
String sql;
PreparedStatement pst;
ResultSet rs;
String url,userName,userPwd;
// 连接数据库的语句
url="jdbc:sqlserver://localhost:1433;DatabaseName=StudentInfo";
userName="sa";
userPwd="123";
con=DriverManager.getConnection(url,userName,userPwd);
sql="select * from 软件1701班2020春季学期成绩表 ";
pst = con.prepareStatement(sql);
rs=pst.executeQuery();
displayResultSet(rs,p4);
}
catch(SQLException exp){
System.out.println(exp);
}
}else if(c1s.equals("软件1702")&&c2s.equals("2019秋季学期")) {
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
try{
Connection con;
String sql;
PreparedStatement pst;
ResultSet rs;
String url,userName,userPwd;
// 连接数据库的语句
url="jdbc:sqlserver://localhost:1433;DatabaseName=StudentInfo";
userName="sa";
userPwd="123";
con=DriverManager.getConnection(url,userName,userPwd);
sql="select * from 软件1702班2019秋季学期成绩表 ";
pst = con.prepareStatement(sql);
rs=pst.executeQuery();
displayResultSet(rs,p4);
}
catch(SQLException exp){
System.out.println(exp);
}
}else {
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
try{
Connection con;
String sql;
PreparedStatement pst;
ResultSet rs;
String url,userName,userPwd;
// 连接数据库的语句
url="jdbc:sqlserver://localhost:1433;DatabaseName=StudentInfo";
userName="sa";
userPwd="123";
con=DriverManager.getConnection(url,userName,userPwd);
sql="select * from 软件1702班2020春季学期成绩表 ";
pst = con.prepareStatement(sql);
rs=pst.executeQuery();
displayResultSet(rs,p4);
}
catch(SQLException exp){
System.out.println(exp);
}
}
}
});
//确认修改成绩----------------------------------------------------------------------------------------
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//p4.removeAll();
String data[][]=new String[100][4];
int n = 0;
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
try{
Connection con;
String sql;
PreparedStatement pst;
ResultSet rs;
String url,userName,userPwd;
// 连接数据库的语句
url="jdbc:sqlserver://localhost:1433;DatabaseName=StudentInfo";
userName="sa";
userPwd="123";
con=DriverManager.getConnection(url,userName,userPwd);
for(int i=0;i<jTable.getRowCount();i++) {
for(int j=0;j<jTable.getColumnCount();j++) {
data[i][j]=jTable.getValueAt(i,j).toString();
}
}
if(c1s.equals("软件1701")&&c2s.equals("2019秋季学期")) {
for(int i=0;i<jTable.getRowCount();i++) {
pst = con.prepareStatement("update 软件1701班2019秋季学期成绩表 set 成绩='"+data[i][3]+"' where 学号='"+data[i][0]+"'");
pst.executeUpdate();
}
}else if(c1s.equals("软件1701")&&c2s.equals("2020春季学期")) {
for(int i=0;i<jTable.getRowCount();i++) {
pst = con.prepareStatement("update 软件1701班2020春季学期成绩表 set 成绩='"+data[i][3]+"' where 学号='"+data[i][0]+"'");
pst.executeUpdate();
}
}else if(c1s.equals("软件1702")&&c2s.equals("2019秋季学期")) {
for(int i=0;i<jTable.getRowCount();i++) {
pst = con.prepareStatement("update 软件1702班2019秋季学期成绩表 set 成绩='"+data[i][3]+"' where 学号='"+data[i][0]+"'");
pst.executeUpdate();
}
}else {
for(int i=0;i<jTable.getRowCount();i++) {
pst = con.prepareStatement("update 软件1702班2020春季学期成绩表 set 成绩='"+data[i][3]+"' where 学号='"+data[i][0]+"'");
pst.executeUpdate();
}
}
con.close();
JOptionPane.showMessageDialog(null, "修改成绩成功!","消息对话框",JOptionPane.WARNING_MESSAGE);
dispose();
}
catch(SQLException exp){
System.out.println(exp);
}
}
});
}
}
}
Stud_UI.java
package studentmanage;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import studentmanage.UpPsw.BtnActionListener;
public class Stud_UI extends JFrame{
CardLayout cardLayout=new CardLayout();
JMenuBar menubar=new JMenuBar();
JMenu menu=new JMenu("学生权限");
JMenuItem item1=new JMenuItem("查询个人信息");
JMenuItem item2=new JMenuItem("查询成绩信息");
JPanel panel1=new JPanel();
JPanel panel1B=new JPanel();
JPanel panel1C=new JPanel();
JPanel panel2=new JPanel();
JPanel panel2B=new JPanel();
JPanel panel2C=new JPanel();
JPanel panel3=new JPanel();
JPanel panel4=new JPanel();
JPanel panel=new JPanel();
JPanel p=new JPanel();
JPanel p1=new JPanel();
JButton btn1=new JButton("查询信息");
JButton btn10=new JButton("退出");
JButton btn2=new JButton("查询成绩");
JButton btn20=new JButton("退出");
JComboBox<String> c1 = new JComboBox<String>();//创建下拉列表
JComboBox<String> c2 = new JComboBox<String>();//创建下拉列表
JLabel label1=new JLabel("学期");
JLabel label2=new JLabel("班级");
JTextField numField=new JTextField(15);
String num=null;
String c1s,c2s;
JTable table1,table2,jTable;
public Stud_UI(JTextField numField) {
num=numField.getText();
c1.addItem("2019秋季学期");
c1.addItem("2020春季学期");
c2.addItem("软件1701");
c2.addItem("软件1702");
num=numField.getText().toString();
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-800)/2,(screenSize.height-600)/2,800,600);
setTitle("学生信息管理系统——学生");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
menu.add(item1);
menu.add(item2);
menubar.add(menu);
this.setJMenuBar(menubar);
panel.setLayout(cardLayout);
panel.add(panel1,"1");
panel.add(panel2,"2");
panel.add(panel3,"3");
panel.add(panel4,"4");
panel.setBackground(Color.decode("#A9A9A9"));
panel1.setBackground(Color.decode("#A9A9A9"));
panel2.setBackground(Color.decode("#A9A9A9"));
panel2B.setBackground(Color.decode("#A9A9A9"));
panel2C.setBackground(Color.decode("#A9A9A9"));
panel3.setBackground(Color.decode("#A9A9A9"));
panel4.setBackground(Color.decode("#A9A9A9"));
p.setBackground(Color.decode("#A9A9A9"));
p1.setBackground(Color.decode("#A9A9A9"));
add(panel);
item1.addActionListener(new Item1ActionListener());
item2.addActionListener(new Item2ActionListener());
}
public Vector getNextRow(ResultSet rs, ResultSetMetaData rsmd) throws SQLException {
Vector currentRow = new Vector(); // 定义一个向量,用于存放记录
for (int i = 1; i <= rsmd.getColumnCount(); ++i)
currentRow.addElement(rs.getString(i)); // 获取记录
return currentRow; // 返回记录
}
public void displayResultSet(ResultSet rs,JPanel p) throws SQLException {
boolean moreRecords = rs.next(); // 定位到达第一条记录
if (!moreRecords) {
JOptionPane.showMessageDialog(null, "结果集中无记录", "无记录",
JOptionPane.INFORMATION_MESSAGE);
dispose();
return;
}
Vector rows = new Vector();
Vector columnHeads = new Vector();
try {
ResultSetMetaData rsmd = rs.getMetaData(); // 获得rs结果集中列属性信息
for (int i = 1; i <= rsmd.getColumnCount(); ++i)
columnHeads.addElement(rsmd.getColumnName(i)); // 获得列名(将列名存放至向量columnHeads)
do {
rows.addElement(getNextRow(rs, rsmd));
}
while (rs.next()); // 利用循环获得所有记录
jTable = new JTable(rows, columnHeads); // 将获得的行列数据信息作为参数重新构造表格视图
jTable.setRowHeight(55);
jTable.getColumnModel().getColumn(0).setPreferredWidth(200);
jTable.getColumnModel().getColumn(1).setPreferredWidth(200);
jTable.getColumnModel().getColumn(2).setPreferredWidth(200);
jTable.getColumnModel().getColumn(3).setPreferredWidth(200);
JScrollPane scroller = new JScrollPane(jTable);// 创建带有滚动条的面板,并将表格视图加入
p.add(scroller, BorderLayout.CENTER); // 将面板重新加入溶器中
} catch (SQLException e) {
e.printStackTrace();
}
}
//查询个人信息--------------------------------------------------------------------------------------------
class Item1ActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
p1.removeAll();
panel1.add(p1);
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
try{
Connection con;
String sql;
PreparedStatement pst;
ResultSet rs;
String url,userName,userPwd;
// 连接数据库的语句
url="jdbc:sqlserver://localhost:1433;DatabaseName=StudentInfo";
userName="sa";
userPwd="123";
con=DriverManager.getConnection(url,userName,userPwd);
sql="select * from 学生个人档案信息 where 学号='"+num+"' ";
pst = con.prepareStatement(sql);
rs=pst.executeQuery();
displayResultSet(rs,p1);
}
catch(SQLException exp){
System.out.println(exp);
}
cardLayout.show(panel,"1");
}
}
//查询成绩信息-------------------------------------------------------------------------------------------------------
class Item2ActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
panel2B.add(label1);
panel2B.add(c1);
panel2B.add(label2);
panel2B.add(c2);
panel2B.add(btn2);
panel2C.add(p);
panel2.setLayout(new BorderLayout());
panel2.add(panel2B,BorderLayout.NORTH);
panel2.add(panel2C,BorderLayout.CENTER);
cardLayout.show(panel,"2");
c1s=c1.getSelectedItem().toString();
c2s=c2.getSelectedItem().toString();
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
p.removeAll();
if(c2s.equals("软件1701")&&c1s.equals("2019秋季学期")) {
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
try{
Connection con;
String sql;
PreparedStatement pst;
ResultSet rs;
String url,userName,userPwd;
// 连接数据库的语句
url="jdbc:sqlserver://localhost:1433;DatabaseName=StudentInfo";
userName="sa";
userPwd="123";
con=DriverManager.getConnection(url,userName,userPwd);
sql="select * from 软件1701班2019秋季学期成绩表 where 学号='"+num+"'";
pst = con.prepareStatement(sql);
rs=pst.executeQuery();
displayResultSet(rs,p);
}
catch(SQLException exp){
System.out.println(exp);
}
}else if(c2s.equals("软件1701")&&c1s.equals("2020春季学期")) {
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
try{
Connection con;
String sql;
PreparedStatement pst;
ResultSet rs;
String url,userName,userPwd;
// 连接数据库的语句
url="jdbc:sqlserver://localhost:1433;DatabaseName=StudentInfo";
userName="sa";
userPwd="123";
con=DriverManager.getConnection(url,userName,userPwd);
sql="select *from 软件1701班2020春季学期成绩表 where 学号='"+num+"'";
pst = con.prepareStatement(sql);
rs=pst.executeQuery();
displayResultSet(rs,p);
}
catch(SQLException exp){
System.out.println(exp);
}
}else if(c2s.equals("软件1702")&&c1s.equals("2019秋季学期")) {
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
try{
Connection con;
String sql;
PreparedStatement pst;
ResultSet rs;
String url,userName,userPwd;
// 连接数据库的语句
url="jdbc:sqlserver://localhost:1433;DatabaseName=StudentInfo";
userName="sa";
userPwd="123";
con=DriverManager.getConnection(url,userName,userPwd);
sql="select * from 软件1702班2019秋季学期成绩表 where 学号='"+num+"'";
pst = con.prepareStatement(sql);
rs=pst.executeQuery();
displayResultSet(rs,p);
}
catch(SQLException exp){
System.out.println(exp);
}
}else {
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
try{
Connection con;
String sql;
PreparedStatement pst;
ResultSet rs;
String url,userName,userPwd;
// 连接数据库的语句
url="jdbc:sqlserver://localhost:1433;DatabaseName=StudentInfo";
userName="sa";
userPwd="123";
con=DriverManager.getConnection(url,userName,userPwd);
sql="select * from 软件1702班2020春季学期成绩表 where 学号='"+num+"'";
pst = con.prepareStatement(sql);
rs=pst.executeQuery();
displayResultSet(rs,p);
}
catch(SQLException exp){
System.out.println(exp);
}
}
}
});
}
}
}
UpPsw.java
package studentmanage;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.*;
import studentmanage.DenLu.Button1ActionListener;
public class UpPsw extends JFrame{
JLabel label;
JLabel usernum;
JLabel newPwd1,newPwd2;
JTextField numField;
JPasswordField newpwdField1;
JPasswordField newpwdField2;
JButton btn;
JPanel pane1,pane2,pane3,pane0,pane4;
public UpPsw() {
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-800)/2,(screenSize.height-600)/2,800,600);
label=new JLabel("★请输入账号和新密码,并确认新密码。若两次输入的密码不一致,您将无法修改密码!",JLabel.LEFT);
label.setFont(new Font("宋体", Font.PLAIN, 20));
usernum=new JLabel("输入账号",JLabel.CENTER);
usernum.setFont(new Font("宋体", Font.PLAIN, 20));
newPwd1=new JLabel("输入新密码",JLabel.CENTER);
newPwd1.setFont(new Font("宋体", Font.PLAIN, 20));
newPwd2=new JLabel("确认新密码",JLabel.CENTER);
newPwd2.setFont(new Font("宋体", Font.PLAIN, 20));
numField=new JTextField(21);
newpwdField1=new JPasswordField(20);
newpwdField2=new JPasswordField(20);
btn=new JButton("确定修改");
btn.setFont(new Font("宋体", Font.PLAIN, 20));
btn.setBackground(Color.WHITE);
pane0=new JPanel();
pane1=new JPanel();
pane2=new JPanel();
pane3=new JPanel();
pane4=new JPanel();
pane0.setBackground(Color.decode("#A9A9A9"));
pane1.setBackground(Color.decode("#A9A9A9"));
pane2.setBackground(Color.decode("#A9A9A9"));
pane3.setBackground(Color.decode("#A9A9A9"));
pane4.setBackground(Color.decode("#A9A9A9"));
btn.addActionListener(new BtnActionListener());
pane4.add(label);
pane0.add(usernum);
pane0.add(numField);
pane1.add(newPwd1);
pane1.add(newpwdField1);
pane2.add(newPwd2);
pane2.add(newpwdField2);
pane3.add(btn);
add(pane4);
add(pane0);
add(pane1);
add(pane2);
add(pane3);
this.setLayout(new GridLayout(5,1,0,0));
setTitle("修改密码界面");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
class BtnActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String num,psw1,psw2;
num=numField.getText();
psw1=newpwdField1.getText();
psw2=newpwdField2.getText();
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //加载数据库驱动
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
try{
Connection con;
Statement sql;
int k;
String url,userName,userPwd;
url="jdbc:sqlserver://localhost:1433;DatabaseName=StudentInfo";
userName="sa";
userPwd="123";
con=DriverManager.getConnection(url,userName,userPwd);
sql=con.createStatement();
if(psw1.equals(psw2)) {
k=sql.executeUpdate("update 用户信息表 set 密码= '"+psw2+"' where 账号='"+num+"'");
if(k>0) {
JOptionPane.showMessageDialog(null, "密码修改成功!","消息对话框",JOptionPane.WARNING_MESSAGE);
}
dispose();
}
else {
JOptionPane.showMessageDialog(null, "两次输入密码不一致!!","消息对话框",JOptionPane.WARNING_MESSAGE);
}
}
catch(SQLException exp){
System.out.println(exp);
}
}
}
}
代码优化