📄 xuesheng.java
字号:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.sql.*;
class xuesheng extends JFrame implements ActionListener,MouseListener
{
Connection conn;
Statement stat;
ResultSet rs;
int k;
JButton jb_chaxun,jb_delete,jb_insert,jb_xiugai,jb_zongfen,jb_chazhao;
JTable table;
JTabbedPane tp;
Object body[][]=new Object[50][50];
String title[]={"学号","姓名","性别","出生日期","院系","数学","英语","计算机","总分"};
JLabel label = new JLabel("");
JMenuBar menubar=new JMenuBar();
JMenu file=new JMenu("查询全部");
JMenu edit=new JMenu("添加信息");
JMenu view=new JMenu("删除信息");
JMenu gai=new JMenu("修改信息");
JMenu zhao=new JMenu("查找信息");
JMenu author=new JMenu("作者信息");
JMenu exit=new JMenu("退出系统");
JMenuItem f1=new JMenuItem("查询信息");
JMenuItem e1=new JMenuItem("添加信息");
JMenuItem v1=new JMenuItem("删除信息");
JMenuItem g1=new JMenuItem("修改信息");
JMenuItem z1=new JMenuItem("查找信息");
JMenuItem a1=new JMenuItem("作者信息");
JMenuItem t1=new JMenuItem("退出系统");
public xuesheng()
{
super("学生管理系统");
//jb_chaxun=new JButton("查询");
//jb_insert=new JButton("插入");
//jb_delete=new JButton("删除");
//jb_xiugai=new JButton("修改");
//jb_zongfen=new JButton("显示总分");
jb_chaxun=new JButton(new ImageIcon("all.jpg"));
jb_insert=new JButton(new ImageIcon("add.jpg"));
jb_delete=new JButton(new ImageIcon("del.jpg"));
jb_xiugai=new JButton(new ImageIcon("correct.jpg"));
jb_zongfen=new JButton(new ImageIcon("order.jpg"));
jb_chazhao=new JButton(new ImageIcon("search.jpg"));
this.setSize(800,600);
this.setLayout(new FlowLayout());
table=new JTable(body,title);
tp=new JTabbedPane();
tp.add("学生表",new JScrollPane(table));
this.add(tp);
JPanel panel=new JPanel();
this.add(panel);
panel.add(jb_chaxun);
panel.add(jb_insert);
panel.add(jb_delete);
panel.add(jb_xiugai);
panel.add(jb_zongfen);
panel.add(jb_chazhao);
panel.add(label);
jb_chaxun.addActionListener(this);
jb_chaxun.addMouseListener(this);
jb_insert.addActionListener(this);
jb_insert.addMouseListener(this);
jb_delete.addActionListener(this);
jb_delete.addMouseListener(this);
jb_xiugai.addActionListener(this);
jb_xiugai.addMouseListener(this);
jb_zongfen.addActionListener(this);
jb_zongfen.addMouseListener(this);
jb_chazhao.addActionListener(this);
jb_chazhao.addMouseListener(this);
setJMenuBar(menubar);
menubar.add(file);
menubar.add(edit);
menubar.add(view);
menubar.add(gai);
menubar.add(zhao);
menubar.add(author);
menubar.add(exit);
file.add(f1);
edit.add(e1);
view.add(v1);
gai.add(g1);
zhao.add(z1);
author.add(a1);
exit.add(t1);
f1.addActionListener(this);
e1.addActionListener(this);
v1.addActionListener(this);
g1.addActionListener(this);
z1.addActionListener(this);
a1.addActionListener(this);
t1.addActionListener(this);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.connection();
}
public void connection()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=student.mdb";
conn=DriverManager.getConnection(url);
stat = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
}
catch (Exception ex)
{
}
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==jb_chaxun || event.getSource()==f1)
chaxun();
if(event.getSource()==jb_insert || event.getSource()==e1)
insert();
if(event.getSource()==jb_delete || event.getSource()==v1)
delete();
if(event.getSource()==jb_xiugai || event.getSource()==g1)
xiugai();
if(event.getSource()==jb_zongfen)
zongfen();
if(event.getSource()==a1)
JOptionPane.showMessageDialog(null," 计算机06级3班\n 20064524 陈庆凯\n 2008-12-20","作者信息",JOptionPane.PLAIN_MESSAGE);
if(event.getSource()==t1)
System.exit(0);
if(event.getSource()==jb_chazhao || event.getSource()==z1)
chazhao();
}
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e)
{
if(e.getSource()==jb_chaxun)
{
label.setText("显示信息");
label.setBackground(Color.GRAY);
label.setVisible(true);
}
if(e.getSource()==jb_insert)
{
label.setText("添加信息");
label.setBackground(Color.GRAY);
label.setVisible(true);
}
if(e.getSource()==jb_delete)
{
label.setText("删除信息");
label.setBackground(Color.GRAY);
label.setVisible(true);
}
if(e.getSource()==jb_xiugai)
{
label.setText("修改信息");
label.setBackground(Color.GRAY);
label.setVisible(true);
}
if(e.getSource()==jb_zongfen)
{
label.setText("计算总分");
label.setBackground(Color.GRAY);
label.setVisible(true);
}
if(e.getSource()==jb_chazhao)
{
label.setText("查找信息");
label.setBackground(Color.GRAY);
label.setVisible(true);
}
}
public void chaxun()
{
try {
for(int x=0;x<body.length;x++)
{
body[x][0]=null;
body[x][1]=null;
body[x][2]=null;
body[x][3]=null;
body[x][4]=null;
body[x][5]=null;
body[x][6]=null;
body[x][7]=null;
body[x][8]=null;
body[x][9]=null;
}
int i=0;
rs=stat.executeQuery("select * from student");
while(rs.next())
{
body[i][0]=rs.getString(1);
body[i][1]=rs.getString(2);
body[i][2]=rs.getString(3);
body[i][3]=rs.getString(4);
body[i][4]=rs.getString(5);
body[i][5]=rs.getInt(6);
body[i][6]=rs.getInt(7);
body[i][7]=rs.getInt(8);
i=i+1;
this.repaint();
}
}
catch (SQLException ex)
{
}
}
public void insert()
{
try
{
JTextField text[]= new JTextField[20];
text[0]=new JTextField("输入学号");
text[0].setEditable(false);
text[1]=new JTextField();
text[2]=new JTextField("输入姓名");
text[2].setEditable(false);
text[3]=new JTextField();
text[4]=new JTextField("输入性别");
text[4].setEditable(false);
text[5]=new JTextField();
text[6]=new JTextField("输入出生日期");
text[6].setEditable(false);
text[7]=new JTextField();
text[8]=new JTextField("输入院系");
text[8].setEditable(false);
text[9]=new JTextField();
text[10]=new JTextField("输入数学成绩");
text[10].setEditable(false);
text[11]=new JTextField();
text[12]=new JTextField("输入英语成绩");
text[12].setEditable(false);
text[13]=new JTextField();
text[14]=new JTextField("输入计算机成绩");
text[14].setEditable(false);
text[15]=new JTextField();
String but[]={"确定","取消"};
int go=JOptionPane.showOptionDialog(null,text,"插入信息",JOptionPane.YES_OPTION,JOptionPane.INFORMATION_MESSAGE,null,but,but[0]);
if(go==0)
{
try{
String num=new String(text[1].getText());
String name=new String(text[3].getText());
String sex=new String(text[5].getText());
String birthday=new String(text[7].getText());
String xueyuan=new String(text[9].getText());
String math=new String(text[11].getText());
String eng=new String(text[13].getText());
String jsj=new String(text[15].getText());
connection();
stat.executeUpdate("insert into student (num,name,sex,birthday,xueyuan,math,eng,jsj) values ('"+num+"','"+name+"','"+sex+"','"+birthday+"','"+xueyuan+"','"+math+"','"+eng+"','"+jsj+"')");
JOptionPane.showMessageDialog(null,"数据已成功插入!");
this.chaxun();
}catch(Exception ee){}
}
}catch (Exception ex) {}
}
public void delete()
{
try
{
int row=table.getSelectedRow();
stat.executeUpdate("delete from student where num='"+body[row][0]+"'");
JOptionPane.showMessageDialog(null,"数据已成功删除");
this.chaxun();
}
catch (SQLException ex)
{
}
}
public void xiugai()
{
try
{ int row=table.getSelectedRow();
JTextField text[]=new JTextField[20];
text[0]=new JTextField("输入学号:");
text[0].setEditable(false);
text[1]=new JTextField(""+body[row][0]+"");
text[2]=new JTextField("输入姓名:");
text[2].setEditable(false);
text[3]=new JTextField(""+body[row][1]+"");
text[4]=new JTextField("输入性别:");
text[4].setEditable(false);
text[5]=new JTextField(""+body[row][2]+"");
text[6]=new JTextField("输入出生日期:");
text[6].setEditable(false);
text[7]=new JTextField(""+body[row][3]+"");
text[8]=new JTextField("输入院系:");
text[8].setEditable(false);
text[9]=new JTextField(""+body[row][4]+"");
text[10]=new JTextField("输入数学成绩:");
text[10].setEditable(false);
text[11]=new JTextField(""+body[row][5]+"");
text[12]=new JTextField("输入英语成绩");
text[12].setEditable(false);
text[13]=new JTextField(""+body[row][6]+"");
text[14]=new JTextField("输入计算机成绩");
text[14].setEditable(false);
text[15]=new JTextField(""+body[row][7]+"");
String but[]={"确定","取消"};
int go=JOptionPane.showOptionDialog(null,text,"修改信息",JOptionPane.YES_OPTION,JOptionPane.INFORMATION_MESSAGE,null,but,but[0]);
if(go==0)
{
try{
String num=new String(text[1].getText());
String name=new String(text[3].getText());
String sex=new String(text[5].getText());
String birthday=new String(text[7].getText());
String xueyuan=new String(text[9].getText());
String math=new String(text[11].getText());
String eng=new String(text[13].getText());
String jsj=new String(text[15].getText());
connection();
stat.executeUpdate("update student "+"set num='"+num+"',"+"name='"+name+"',"+"sex='"+sex+"',"+"birthday='"+birthday+"',"+"xueyuan='"+xueyuan+"',"+"math='"+math+"',"+"eng='"+eng+"',"+"jsj='"+jsj+"'"+"where num='"+body[row][0]+"'");
JOptionPane.showMessageDialog(null,"数据已成功修改!");
this.chaxun();
}catch(Exception ee){}
}
}catch (Exception ex) {}
}
public void zongfen()
{
for(int j=0;j<body.length;j++)
{
int m=body[j][5].hashCode()+body[j][6].hashCode()+body[j][7].hashCode();
body[j][8]=m;
this.repaint();
}
}
public void chazhao()
{
try{
JTextField t[]=new JTextField[2];
t[0]=new JTextField("输入要查找的学号:");
t[0].setEditable(false);
t[1]=new JTextField();
String but[]={"确定","取消"};
int go=JOptionPane.showOptionDialog(null,t,"按学号查找",JOptionPane.YES_OPTION,JOptionPane.INFORMATION_MESSAGE,null,but,but[0]);
if(go==0)
{
try{
for(int x=0;x<body.length;x++)
{
body[x][0]=null;
body[x][1]=null;
body[x][2]=null;
body[x][3]=null;
body[x][4]=null;
body[x][5]=null;
body[x][6]=null;
body[x][7]=null;
body[x][8]=null;
body[x][9]=null;
}
String num=new String(t[1].getText());
connection();
rs=stat.executeQuery("select * from student where num='"+num+"'");
while(rs.next())
{
body[0][0]=rs.getString(1);
body[0][1]=rs.getString(2);
body[0][2]=rs.getString(3);
body[0][3]=rs.getString(4);
body[0][4]=rs.getString(5);
body[0][5]=rs.getInt(6);
body[0][6]=rs.getInt(7);
body[0][7]=rs.getInt(8);
this.repaint();
}
}catch(Exception ee){}
}
}catch(Exception ee){}
}
public static void main(String args[])
{
new xuesheng();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -