import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JTable.*;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.AbstractTableModel;
import java.sql.*;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.io.*;
import java.math.BigDecimal;
public class Main{
public static void main(String[] args) throws Exception{
new dengru();
}
}
class dengru{
private JFrame frame = new JFrame("学生成绩管理系统");
private JButton submit = new JButton("登入");
private JButton reset = new JButton("重置");
private JLabel nameLab = new JLabel("用户名:");
private JLabel passLab = new JLabel("密 码:");
private JLabel infoLab = new JLabel("用户登录(root,java)");
private JTextField nameText = new JTextField(10);
private JPasswordField passText = new JPasswordField() ;
public dengru()throws Exception{
Font fnt = new Font("Serief",Font.ITALIC + Font.BOLD,12) ;
infoLab.setFont(fnt) ;
submit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==submit){
String tname = nameText.getText() ;
String tpass = new String(passText.getPassword()) ;
LoginCheck log = new LoginCheck(tname,tpass) ;
if(log.validate()){
try{
JOptionPane.showMessageDialog(null,"密码正确,即将进入系统!","登入提示!",
JOptionPane.INFORMATION_MESSAGE);
Thread.sleep(3000);
frame.dispose();
new Menu();
}catch(Exception ex){
ex.printStackTrace();
}
}else{
try{
JOptionPane.showMessageDialog(null,"用户不存在或密码错误,请重新填写!","登入提示!",
JOptionPane.INFORMATION_MESSAGE);
frame.dispose();
new dengru();
}catch(Exception ex){}
}
}
}
}) ;
reset.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==reset){
nameText.setText("") ;
passText.setText("") ;
infoLab.setText("用户登录(root,java)");
}
}
}) ;
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(1) ;
}
}) ;
frame.setLayout(null) ;
nameLab.setBounds(5,5,60,20) ;
passLab.setBounds(5,30,60,20) ;
infoLab.setBounds(5,65,220,30) ;
nameText.setBounds(65,5,100,20) ;
passText.setBounds(65,30,100,20) ;
submit.setBounds(170,5,60,20) ;
reset.setBounds(170,30,60,20) ;
frame.add(nameLab) ;
frame.add(passLab) ;
frame.add(infoLab) ;
frame.add(nameText) ;
frame.add(passText) ;
frame.add(submit) ;
frame.add(reset) ;
frame.setSize(280,130) ;
frame.setBackground(Color.WHITE) ;
frame.setLocation(420,230) ;
frame.setVisible(true) ;
}
}
class LoginCheck{
private String name ;
private String password ;
public LoginCheck(String name,String password){
this.name = name ;
this.password = password ;
}
public boolean validate(){
if("root".equals(name)&&"java".equals(password)){
return true ;
}else{
return false ;
}
}
}
class Menu extends AbstractTableModel implements ActionListener{
public static String title[]={"学号","姓名","数学","物理","英语","平均分","总分"};
public static Object inf[][]={{"","","","","","",""},
{"","","","","","",""},{"","","","","","",""},
{"","","","","","",""},{"","","","","","",""},
{"","","","","","",""},{"","","","","","",""},
{"","","","","","",""},{"","","","","","",""},
{"","","","","","",""},{"","","","","","",""},
{"","","","","","",""},{"","","","","","",""},
{"","","","","","",""},{"","","","","","",""},
{"","","","","","",""},{"","","","","","",""},
{"","","","","","",""},{"","","","","","",""},
{"","","","","","",""},{"","","","","","",""},
{"","","","","","",""},{"","","","","","",""}};
public static final String datd="com.mysql.jdbc.Driver";
public static final String datu="jdbc:mysql://localhost:3306/student?characterEncoding=utf-8&serverTimezone=UTC";
public static final String dname="root";
public static final String dpass="skey";
public static final String sql="select id,name,math,wuli,english,everage,sum from stu";
JFrame f=new JFrame("学生成绩管理系统");
JButton display=new JButton("显示");
JButton search=new JButton("查找");
JButton modify=new JButton("修改");
JButton adda=new JButton("添加");
JButton delete=new JButton("删除");
JButton sort=new JButton("排序");
JButton save=new JButton("保存文件");
JButton quit=new JButton("退出");
JTable tab=null;
DefaultTableModel tabmo=null;
Connection con=null; //数据库连接
Statement sta=null; //数据库操作
ResultSet rs=null; //保存查询结果
PrintStream ps=null;
/*public Menu(){
} */
public Menu() throws Exception{
Class.forName(datd); //加载驱动程序
con=DriverManager.getConnection(datu,dname,dpass);
sta=con.createStatement();
tabmo=new DefaultTableModel(inf,title);
tab=new JTable(tabmo);
JScrollPane js=new JScrollPane(tab);
JPanel jp=new JPanel();
jp.add(display);
jp.add(search);
jp.add(modify);
jp.add(adda);
jp.add(delete);
jp.add(sort);
jp.add(save);
jp.add(quit);
f.add(jp,BorderLayout.NORTH);
f.add(js,BorderLayout.CENTER);
f.setSize(700,500);
f.setLocation(250,70);
f.setVisible(true);
display.addActionListener(this);
search.addActionListener(this);
modify.addActionListener(this);
adda.addActionListener(this);
delete.addActionListener(this);
sort.addActionListener(this);
save.addActionListener(this);
quit.addActionListener(this);
}
public int getColumnCount(){
return title.length;
}
public int getRowCount(){
return inf.length;
}
public Object getValueAt(int row,int col){
return inf[row][col];
}
public String getColumnName(int col){
return title[col];
}
public Class<?>getColumnClass(int col){
return this.getValueAt(0,col).getClass();
}
public boolean isCellEditable(int row,int col){
return false;
}
public void setValueAt(Object newv,int row,int col){
this.inf[row][col]=newv;
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==display){
try{
this.show(sql,0,"");
}catch(Exception ex){}
}
if(e.getSource()==search){
try{
f.dispose();
new Search();
if(tabmo.getValueAt(0,0).equals("")){
JOptionPane.showMessageDialog(null,"数据库中不存在你查找学生的信息,请重新输入!","提示!",JOptionPane.INFORMATION_MESSAGE); ;
f.dispose();
new Search();
}
}catch(Exception ex){}
}
if(e.getSource()==modify){
try{
f.dispose();
new Modify();
}catch(Exception ex){}
}
if(e.getSource()==adda){
try{
f.dispose();
new Adda();
}catch(Exception ex){}
}
if(e.getSource()==delete){
try{
f.dispose();
new Delete();
}catch(Exception ex){}
}
if(e.getSource()==sort){
try{
JOptionPane.showMessageDialog(null,"排序是将读取到数据库的内容进行排序,不修改数据库的内容!","提示!",JOptionPane.INFORMATION_MESSAGE);
this.Sort();
}catch(Exception ex){}
}
if(e.getSource()==save){
try{
this.Save();
JOptionPane.showMessageDialog(null,"保存文件成功,可以在D盘根目录查看文件!","提示!",JOptionPane.INFORMATION_MESSAGE);
}catch(Exception ex){}
}
if(e.getSource()==quit){
try{
sta.close();
con.close();
System.exit(1);
}catch(Exception ex){}
}
}
public void show(String s,int p,String t)throws Exception{ //p=0:只显示数据,p=1:update数据在显示
//con=DriverManager.getConnection(datu,dname,dpass); //s:要显示的sql,t:数据库更新语句
//sta=con.createStatement();
if(p==0){
rs=sta.executeQuery(s); //实例化查询结果对象
}else{
sta.executeUpdate(t); //执行数据库更新操作
rs=sta.executeQuery(s);
}
int id0;
String name0=null;
float math0;
float wuli0;
float english0;
float everage0;
float sum0;
int i=0;
while(rs.next()){
id0=rs.getInt("id");
name0=rs.getString("name");
math0=rs.getFloat("math");
wuli0=rs.getFloat("wuli");
english0=rs.getFloat("english");
everage0=toTwo(rs.getFloat("everage"));
sum0=rs.getFloat("sum");
tabmo.setValueAt(id0,i,0);
tabmo.setValueAt(name0,i,1);
tabmo.setValueAt(math0,i,2);
tabmo.setValueAt(wuli0,i,3);
tabmo.setValueAt(english0,i,4);
tabmo.setValueAt(everage0,i,5);
tabmo.setValueAt(sum0,i,6);
i+=1;
if (i>=tabmo.getRowCount()) {
tabmo.addRow(new Object[]{});
}
}
//sta.executeUpdate("create table stuce;");
}
public void Sort()throws Exception{
int id[]=new int[1000];
String name[]=new String[1000];
float math[]=new float[1000];
float wuli[]=new float[1000];
float english[]=new float[1000];
float everage[]=new float[1000];
float sum[]=new float[1000];
int idtemp;
String nametemp=null;
float mathtemp;
float wulitemp;
float englishtemp;
float everagetemp;
float sumtemp;
int num=0; //计数
rs=sta.executeQuery(sql); //实例化查询结果对象
while(rs.next()){
id[num]=rs.getInt("id");
name[num]=rs.getString("name");
math[num]=rs.getFloat("math");
wuli[num]=rs.getFloat("wuli");
english[num]=rs.getFloat("english");
everage[num]=toTwo(rs.getFloat("everage"));
sum[num]=rs.getFloat("sum");
num+=1;
}
for(int j=1;j<num;j++)
for(int k=0;k<num-j;k++)
if(sum[k]<sum[k+1])
{
idtemp=id[k];
id[k]=id[k+1];
id[k+1]=idtemp;
nametemp=name[k];
name[k]=name[k+1];
name[k+1]=nametemp;
mathtemp=math[k];
math[k]=math[k+1];
math[k+1]=mathtemp;
wulitemp=wuli[k];
wuli[k]=wuli[k+1];
wuli[k+1]=wulitemp;
englishtemp=english[k];
english[k]=english[k+1];
english[k+1]=englishtemp;
everagetemp=everage[k];
everage[k]=everage[k+1];
everage[k+1]=everagetemp;
sumtemp=sum[k];
sum[k]=sum[k+1];
sum[k+1]=sumtemp;
}
for(int n=0;n<num;n++){
tabmo.setValueAt(id[n],n,0);
tabmo.setValueAt(name[n],n,1);
tabmo.setValueAt(math[n],n,2);
tabmo.setValueAt(wuli[n],n,3);
tabmo.setValueAt(english[n],n,4);
tabmo.setValueAt(everage[n],n,5);
tabmo.setValueAt(sum[n],n,6);
if(n>=tabmo.getRowCount()) {
tabmo.addRow(new Object[]{});
}
}
}
public void Save()throws Exception{
String id1[]=new String[1000];
String name1[]=new String[1000];
String math1[]=new String[1000];
String wuli1[]=new String[1000];
String english1[]=new String[1000];
String everage1[]=new String[1000];
String sum1[]=new String[1000];
int i=0;
Writer out= new FileWriter(new File("d:"+File.separator+"Student.txt"));
rs=sta.executeQuery(sql);
while(rs.next()){
id1[i]=rs.getString("id");
name1[i]=rs.getString("name");
math1[i]=rs.getString("math");
wuli1[i]=rs.getString("wuli");
english1[i]=rs.getString("english");
everage1[i]=String.valueOf(toTwo(rs.getFloat("everage")));
sum1[i]=rs.getString("sum");
i+=1;
}
for(int j=0;j<i;j++){
out.write(id1[j]+"\t\t");
out.write(name1[j]+"\t\t");
out.write(math1[j]+"\t\t");
out.write(wuli1[j]+"\t\t");
out.write(english1[j]+"\t\t");
out.write(everage1[j]+"\t\t");
out.write(sum1[j]);
out.write("\r");
}
out.close();
}
public float toTwo(float f){ //设置float型数据保存两位小数
BigDecimal b=new BigDecimal(f);
float f1=b.setScale(2,BigDecimal.ROUND_HALF_UP).floatValue();
return f1;
}
}
class Search extends Menu{
private JFrame jf=new JFrame("查找学生信息!");
private JButton jb=new JButton("查询");
private JLabel jl=new JLabel("请输入需要查询学生的学号或姓名!");
private JTextField jt=new JTextField();
private String s=null;
private String sq=null;
public Search() throws Exception{
super();
Font fo=new Font("Serief",Font.BOLD,12);
jl.setFont(fo);
jt.setBounds(15,15,150,30);
jb.setBounds(175,15,60,30);
jl.setBounds(10,80,180,30);
jf.add(jt);
jf.add(jb);
jf.add(jl);
jf.setSize(300,170);
jf.setLocation(300,250);
jf.setVisible(true);
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==jb){
if(jt.getText().trim().equals("")){
JOptionPane.showMessageDialog(null,"输入的信息不可以为空,请重新输入!","错误提示!",
JOptionPane.INFORMATION_MESSAGE);
f.dispose();
jf.dispose();
try{
new Search();
}catch(Exception ex){}
}else{
s=jt.getText().trim();
try{
int t=Integer.parseInt(jt.getText().trim());
if(t<=0){
JOptionPane.showMessageDialog(null,"输入的学号不可以为0或负数,请重新输入!","错误提示!",
JOptionPane.INFORMATION_MESSAGE);
f.dispose();
jf.dispose();
try{
new Search();
}catch(Exception ex){}
}else{
sq="select id,name,math,wuli,english,everage,sum from stu where id like '%"+t+"%'";
jf.dispose();
try{
show(sq,0," ");
// if(tabmo.getValueAt(0,0).equals("")){
// JOptionPane.showMessageDialog(null,"你查找的学生信息不存在数据库,请重新输入!","提示!",JOptionPane.INFORMATION_MESSAGE); ;
// f.dispose();
// new Search();
// }
}catch(Exception ex){}
}
}catch(Exception ex){
sq="select id,name,math,wuli,english,everage,sum from stu where name like '%"+s+"%'";
jf.dispose();
try{
show(sq,0," ");
// if(tabmo.getValueAt(0,0).equals("")){
// JOptionPane.showMessageDialog(null,"你查找的学生信息不存在数据库,请重新输入!","提示!",JOptionPane.INFORMATION_MESSAGE); ;
// f.dispose();
// new Search();
// }
}catch(Exception ex1){}
}
}
}
}
});
}
}
class Modify extends Menu{
private JFrame jf=new JFrame("修改学生信息!");
private JLabel jl0=new JLabel("请输入信息:");
private JLabel jl1=new JLabel("对应学号:");
private JTextField jt1=new JTextField();
private JLabel jl2=new JLabel("姓 名:");
private JTextField jt2=new JTextField();
private JLabel jl3=new JLabel("数 学:");
private JTextField jt3=new JTextField();
private JLabel jl4=new JLabel("物 理:");
private JTextField jt4=new JTextField();
private JLabel jl5=new JLabel("英 语:");
private JTextField jt5=new JTextField();
private JButton bt1=new JButton("提交");
private JButton bt2=new JButton("退出");
private JLabel xin=new JLabel("");
private int id,idt;
private String na=null;
private String sq=null;
private float m,w,en,ever,su;
public Modify()throws Exception{
super();
jl0.setBounds(10,10,120,30);
jl1.setBounds(10,50,60,30);
jt1.setBounds(80,50,180,30);
jl2.setBounds(10,90,60,30);
jt2.setBounds(80,90,180,30);
jl3.setBounds(10,130,60,30);
jt3.setBounds(80,130,180,30);
jl4.setBounds(10,170,60,30);
jt4.setBounds(80,170,180,30);
jl5.setBounds(10,210,60,30);
jt5.setBounds(80,210,180,30);
bt1.setBounds(20,250,60,30);
bt2.setBounds(90,250,60,30);
xin.setBounds(10,300,100,30);
jf.add(jl0);
jf.add(jl1);
jf.add(jt1);
jf.add(jl2);
jf.add(jt2);
jf.add(jl3);
jf.add(jt3);
jf.add(jl4);
jf.add(jt4);
jf.add(jl5);
jf.add(jt5);
jf.add(bt1);
jf.add(bt2);
jf.add(xin);
jf.setSize(400,400);
jf.setLocation(300,200);
jf.setVisible(true);
bt1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==bt1){
if(jt1.getText().trim().equals("")||jt2.getText().trim().equals("")||jt3.getText().trim().equals("")
||jt4.getText().trim().equals("")||jt5.getText().trim().equals("")){
JOptionPane.showMessageDialog(null,"输入的信息不可以为空,请重新输入!","错误提示!",
JOptionPane.INFORMATION_MESSAGE);
f.dispose();
jf.dispose();
try{
new Modify();
}catch(Exception ex){}
}else if(Integer.parseInt(jt1.getText())<0||Float.parseFloat(jt3.getText())<0||Float.parseFloat(jt4.getText())<0||Float.parseFloat(jt5.getText())<0){
JOptionPane.showMessageDialog(null,"输入的信息不可以有负数,请重新输入!","错误提示!",
JOptionPane.INFORMATION_MESSAGE);
f.dispose();
jf.dispose();
try{
new Modify();
}catch(Exception ex){}
}else{
id=Integer.parseInt(jt1.getText().trim());
na=jt2.getText().trim();
m=Float.parseFloat(jt3.getText().trim());
w=Float.parseFloat(jt4.getText().trim());
en=Float.parseFloat(jt5.getText().trim());
su=m+w+en;
ever=toTwo((float)(su/3.0));
sq="update stu set name='"+na+"',math="+m+",wuli="+w+",english="+en+",everage="+ever+",sum="+su+" where id="+id;
try{
jf.dispose();
show(sql,1,sq);
}catch(Exception ex){}
}
}
}
}) ;
bt2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==bt2){
jf.dispose();
}
}
}) ;
}
}
class Adda extends Menu{
private JFrame jf=new JFrame("添加学生信息!");
private JLabel jl0=new JLabel("请输入新添加学生的信息:");
private JLabel jl1=new JLabel("姓 名:");
private JTextField jt1=new JTextField();
private JLabel jl2=new JLabel("数 学:");
private JTextField jt2=new JTextField();
private JLabel jl3=new JLabel("物 理:");
private JTextField jt3=new JTextField();
private JLabel jl4=new JLabel("英 语:");
private JTextField jt4=new JTextField();
private JButton bt1=new JButton("提交");
private JButton bt2=new JButton("退出");
private JLabel xin=new JLabel("");
private String na=null;
private String sq=null;
private float m,w,en,ever,su;
public Adda()throws Exception{
super();
jl0.setBounds(10,10,170,30);
jl1.setBounds(10,50,60,30);
jt1.setBounds(80,50,180,30);
jl2.setBounds(10,90,60,30);
jt2.setBounds(80,90,180,30);
jl3.setBounds(10,130,60,30);
jt3.setBounds(80,130,180,30);
jl4.setBounds(10,170,60,30);
jt4.setBounds(80,170,180,30);
bt1.setBounds(20,230,60,30);
bt2.setBounds(90,230,60,30);
xin.setBounds(10,260,100,30);
jf.add(jl0);
jf.add(jl1);
jf.add(jt1);
jf.add(jl2);
jf.add(jt2);
jf.add(jl3);
jf.add(jt3);
jf.add(jl4);
jf.add(jt4);
jf.add(bt1);
jf.add(bt2);
jf.add(xin);
jf.setSize(400,380);
jf.setLocation(300,200);
jf.setVisible(true);
bt1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==bt1){
if(jt1.getText().trim().equals("")||jt2.getText().trim().equals("")||jt3.getText().trim().equals("")
||jt4.getText().trim().equals("")){
JOptionPane.showMessageDialog(null,"输入的信息不可以为空,请重新输入!","错误提示!",
JOptionPane.INFORMATION_MESSAGE);
f.dispose();
jf.dispose();
try{
new Adda();
}catch(Exception ex){}
}else if(Float.parseFloat(jt2.getText())<0||Float.parseFloat(jt3.getText())<0||Float.parseFloat(jt4.getText())<0){
JOptionPane.showMessageDialog(null,"输入的分数不可以有负数,请重新输入!","错误提示!",
JOptionPane.INFORMATION_MESSAGE);
f.dispose();
jf.dispose();
try{
new Adda();
}catch(Exception ex){}
}else{
na=jt1.getText().trim();
m=Float.parseFloat(jt2.getText().trim());
w=Float.parseFloat(jt3.getText().trim());
en=Float.parseFloat(jt4.getText().trim());
su=m+w+en;
ever=toTwo((float)(su/3.0));
sq="insert into stu(name,math,wuli,english,everage,sum)"+"values('"+na+"',"+m+","+w+","+en+","+ever+","+su+")";
try{
jf.dispose();
show(sql,1,sq);
}catch(Exception ex){}
}
}
}
}) ;
bt2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==bt2){
jf.dispose();
}
}
}) ;
}
}
class Delete extends Menu{
private JFrame jf=new JFrame("删除学生信息!");
private JButton jb=new JButton("删除");
private JLabel jl=new JLabel("请输入需要删除学生的学号!");
private JTextField jt=new JTextField();
private String s=null;
private String sq=null;
public Delete() throws Exception{
super();
Font fo=new Font("Serief",Font.BOLD,12);
jl.setFont(fo);
jt.setBounds(15,15,150,30);
jb.setBounds(175,15,60,30);
jl.setBounds(10,80,80,30);
jf.add(jt);
jf.add(jb);
jf.add(jl);
jf.setSize(300,170);
jf.setLocation(300,250);
jf.setVisible(true);
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==jb){
if(jt.getText().trim().equals("")){
JOptionPane.showMessageDialog(null,"输入的信息不可以为空,请重新输入!","错误提示!",
JOptionPane.INFORMATION_MESSAGE);
f.dispose();
jf.dispose();
try{
new Delete();
}catch(Exception ex){}
}else{
s=jt.getText().trim();
int t=Integer.parseInt(jt.getText().trim());
if(t<=0){
JOptionPane.showMessageDialog(null,"输入的学号不可以为0或负数,请重新输入!","错误提示!",
JOptionPane.INFORMATION_MESSAGE);
f.dispose();
jf.dispose();
try{
new Delete();
}catch(Exception ex){}
}else{
sq="delete from stu where id="+t;
jf.dispose();
try{
show(sql,1,sq);
}catch(Exception ex){}
}
}
}
}
});
}
}
MySQL数据库sql转储脚本
create database student;
use student;
create table stu
(
id int auto_increment primary key,
name varchar(30) not null,
math float not null,
wuli float not null,
english float not null,
everage float not null,
sum float not null
);
insert into stu(name,math,wuli,english,everage,sum) values('张三',80,80,80,80,240);
加企鹅2844365449一起学习技术,讨论问题。