基于Java GUI、MySQL数据库的学生成绩管理系统(Java 图形界面)


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){}
					}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";
	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 ClassgetColumnClass(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=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

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);

 

你可能感兴趣的:(Java,学生成绩管理系统,MySQL,Java编程)