关于学生成绩管理系统的界面设计:代码如下
数据库表设计
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`username` varchar(30) NOT NULL,
`stu_time` datetime NOT NULL,
`stu_score` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
主界面设计
package com.studensystem;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import com.studensystem.SqlHelper;
class MyPanel extends JPanel{
Image img=Toolkit.getDefaultToolkit().getImage("c:/a.jpg");
public void paint(Graphics g){
g.drawImage(img,0,0,this);
}
}
public class MainForm extends JFrame implements ActionListener
{
JMenu mSystem=new JMenu("学生信息管理");
JMenuItem mExit=new JMenuItem("操作");
JMenuItem mExit1 = new JMenuItem("查看");
JMenu mOperate=new JMenu("课程信息管理");
JMenuItem mAdd=new JMenuItem("添加");
JMenuItem mDel=new JMenuItem("删除");
JMenuItem mModify=new JMenuItem("修改");
JMenu mQuery=new JMenu("成绩信息管理");
JMenuItem mName=new JMenuItem("按姓名查询");
JMenuItem mScore=new JMenuItem("按成绩查询");
JMenu mHelp=new JMenu("学期信息管理");
JMenuItem mAbout=new JMenuItem("操作");
JMenuItem mAbout1 = new JMenuItem("");
JMenuBar mBar=new JMenuBar();
MainForm()
{
super("学生成绩管理系统");
setSize(500,500);
mSystem.add(mExit);
mOperate.add(mAdd);
mOperate.add(mDel);
mOperate.add(mModify);
mQuery.add(mName);
mQuery.add(mScore);
mHelp.add(mAbout);
mBar.add(mSystem);
mBar.add(mOperate);
mBar.add(mQuery);
mBar.add(mHelp);
setJMenuBar(mBar);
mExit.addActionListener(this);
mAdd.addActionListener(this);
mDel.addActionListener(this);
mModify.addActionListener(this);
mName.addActionListener(this);
mScore.addActionListener(this);
mAbout.addActionListener(this);
setContentPane(new MyPanel());
setVisible(true);
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==mExit)
System.exit(0);
else if(ae.getSource()==mAbout)
JOptionPane.showMessageDialog(this,"学生成绩管理系统\n对不起,功能暂时未开放\n功能有待完善\n\n","期末作品",JOptionPane.INFORMATION_MESSAGE);
else if(ae.getSource()==mAdd)
new AddForm().setVisible(true);
else if(ae.getSource()==mDel)
new DeleteForm().setVisible(true);
else if(ae.getSource()==mModify)
new ModifyForm().setVisible(true);
else if(ae.getSource()==mName)
new NameQueryForm().setVisible(true);
else if(ae.getSource()==mScore)
new ScoreQueryForm().setVisible(true);
}
public static void main(String[] args) {
new MainForm();
}
}
添加界面设计
package com.studensystem;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import com.studensystem.SqlHelper;
public class AddForm extends JFrame implements ActionListener
{
JLabel labName=new JLabel("姓名:");
JLabel labDate=new JLabel("出生日期:");
JLabel labScore=new JLabel("成绩:");
JTextField txtName=new JTextField(20);
JTextField txtDate=new JTextField(18);
JTextField txtScore=new JTextField(20);
JButton btnAdd=new JButton("添加");
JButton btnCancel=new JButton("取消");
JPanel pan=new JPanel();
JPanel pan1=new JPanel();
JPanel pan2=new JPanel();
JPanel pan3=new JPanel();
JPanel pan4=new JPanel();
Connection con;
Statement sql;
ResultSet rs;
AddForm()
{
super("添加数据");
setSize(400,300);
pan1.add(labName);
pan1.add(txtName);
pan2.add(labDate);
pan2.add(txtDate);
pan3.add(labScore);
pan3.add(txtScore);
pan4.add(btnAdd);
pan4.add(btnCancel);
pan.setLayout(new GridLayout(3,1));
pan.add(pan1);
pan.add(pan2);
pan.add(pan3);
getContentPane().add(pan,"Center");
getContentPane().add(pan4,"South");
btnAdd.addActionListener(this);
btnCancel.addActionListener(this);
btnAdd.setEnabled(true);
txtDate.setEditable(true);
txtScore.setEditable(true);
setVisible(true);
txtName.requestFocus();
}
public void actionPerformed(ActionEvent ae)
{
String recode,insert1,stu_time,username;
int stu_score;
if(ae.getSource()==btnCancel)
{
this.dispose();
}
else if(ae.getSource()==btnAdd)
{
try
{
Class.forName("com.mysql.jdbc.Driver");
}
catch(ClassNotFoundException e){
}
try{con=DriverManager.getConnection("driver","user","passwd");
sql=con.createStatement();
username=txtName.getText();
stu_time=txtDate.getText();
stu_score=Integer.parseInt(txtScore.getText());
// recode="("+"'"+username+"'"+","+"'"+stu_time+""+"',"+stu_score+")";
// insert1="insert into student values "+recode;
insert1="insert into student values(?,?,?)";
sql.executeUpdate(insert1);
System.out.println("记录添加完毕!");
btnAdd.setEnabled(false);
txtName.setText("");
txtScore.setText("");
txtDate.setText("");
con.close();
}
catch(SQLException e){
}
}
}
public static void main(String[] args)
{
new AddForm();
}
}
删除数据设计
package com.studensystem;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import com.studensystem.SqlHelper;
public class DeleteForm extends JFrame implements ActionListener
{
JLabel labName=new JLabel("姓名:");
JLabel labDate=new JLabel("出生日期:");
JLabel labScore=new JLabel("成绩:");
JTextField txtName=new JTextField(20);
JTextField txtDate=new JTextField(18);
JTextField txtScore=new JTextField(20);
JButton btnDelete=new JButton("删除");
JButton btnCancel=new JButton("取消");
JButton btnQuery=new JButton("查询");
JPanel pan=new JPanel();
JPanel pan1=new JPanel();
JPanel pan2=new JPanel();
JPanel pan3=new JPanel();
JPanel pan4=new JPanel();
Connection con;
Statement sql;
ResultSet rs;
DeleteForm()
{
super("删除数据");
setSize(400,300);
pan1.add(labName);
pan1.add(txtName);
pan2.add(labDate);
pan2.add(txtDate);
pan3.add(labScore);
pan3.add(txtScore);
pan4.add(btnQuery);
pan4.add(btnDelete);
pan4.add(btnCancel);
pan.setLayout(new GridLayout(3,1));
pan.add(pan1);
pan.add(pan2);
pan.add(pan3);
getContentPane().add(pan,"Center");
getContentPane().add(pan4,"South");
btnQuery.addActionListener(this);
btnDelete.addActionListener(this);
btnCancel.addActionListener(this);
btnDelete.setEnabled(false);//取消删除按钮的功能
txtDate.setEditable(false);
txtScore.setEditable(false);
setVisible(true);
txtName.requestFocus();
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==btnCancel)
{
dispose();
}
else if(ae.getSource()==btnQuery){ //实现查询功能
try
{
Class.forName("com.mysql.jdbc.Driver");}
catch(ClassNotFoundException e){
}
try{con=DriverManager.getConnection("driver","user","passwd");
sql=con.createStatement();
rs=sql.executeQuery("select * from student where username='"+txtName.getText()+"'");
if(rs.next())
{
txtName.setText(rs.getString("姓名"));
txtScore.setText(new Integer(rs.getInt("成绩")).toString());
txtDate.setText(rs.getDate("出生日期").toString());
btnDelete.setEnabled(true);//使删除按钮功能实现
txtDate.setEditable(true);
txtScore.setEditable(true);
}else{
System.out.println("不存在该记录!");
btnDelete.setEnabled(false);
txtName.setText("");
txtScore.setText("");
txtDate.setText("");
txtDate.setEditable(false);
txtScore.setEditable(false);
}
}catch(SQLException e){
}
}
else if(ae.getSource()==btnDelete) //实现删除功能
{
try
{
System.out.println("update student set stu_time='"+txtDate.getText()+"',stu_score="+txtScore.getText()+" where username='"+txtName.getText()+"'");
sql.executeUpdate("delete from student where username='"+txtName.getText()+"'");
System.out.println("记录删除完毕!");
btnDelete.setEnabled(false);
txtName.setText("");
txtScore.setText("");
txtDate.setText("");
txtDate.setEditable(false);
txtScore.setEditable(false);
con.close();
}
catch(SQLException e){
}
}
}
public static void main(String[] args)
{
new DeleteForm();
}
}
修改界面设计
import javax.swing.*;
import com.studensystem.SqlHelper;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class ModifyForm extends JFrame implements ActionListener
{
JLabel labName=new JLabel("姓名:");
JLabel labDate=new JLabel("出生日期:");
JLabel labScore=new JLabel("成绩:");
JTextField txtName=new JTextField(20);
JTextField txtDate=new JTextField(18);
JTextField txtScore=new JTextField(20);
JButton btnModify=new JButton("修改");
JButton btnCancel=new JButton("取消");
JButton btnQuery=new JButton("查询");
JPanel pan=new JPanel();
JPanel pan1=new JPanel();
JPanel pan2=new JPanel();
JPanel pan3=new JPanel();
JPanel pan4=new JPanel();
Connection con;
Statement sql;
ResultSet rs;
ModifyForm()
{
super("修改数据");
setSize(400,300);
pan1.add(labName);
pan1.add(txtName);
pan2.add(labDate);
pan2.add(txtDate);
pan3.add(labScore);
pan3.add(txtScore);
pan4.add(btnQuery);
pan4.add(btnModify);
pan4.add(btnCancel);
pan.setLayout(new GridLayout(3,1));
pan.add(pan1);
pan.add(pan2);
pan.add(pan3);
getContentPane().add(pan,"Center");
getContentPane().add(pan4,"South");
btnQuery.addActionListener(this);
btnModify.addActionListener(this);
btnCancel.addActionListener(this);
btnModify.setEnabled(false);
txtDate.setEditable(false);
txtScore.setEditable(false);
setVisible(true);
txtName.requestFocus();
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==btnCancel){
dispose();
} else if(ae.getSource()==btnQuery){
try{
Class.forName("com.mysql.jdbc.Driver");}
catch(ClassNotFoundException e){
}
try{con=DriverManager.getConnection("driver","user","passwd");
sql=con.createStatement();
rs=sql.executeQuery("select * from student where username='"+txtName.getText()+"'");
if(rs.next()){
txtName.setText(rs.getString("姓名"));
txtScore.setText(new Integer(rs.getInt("成绩")).toString());
txtDate.setText(rs.getDate("出生日期").toString());
btnModify.setEnabled(true);
txtDate.setEditable(true);
txtScore.setEditable(true);
}else{
System.out.println("不存在该记录! ");
btnModify.setEnabled(false);
txtName.setText("");
txtScore.setText("");
txtDate.setText("");
txtDate.setEditable(false);
txtScore.setEditable(false);
}
}catch(SQLException e){
}
}else if(ae.getSource()==btnModify){
try{
System.out.println("update student set stu_time='"+txtDate.getText()+"',stu_score="+txtScore.getText()+" where username='"+txtName.getText()+"'");
sql.executeUpdate("update student set stu_time='"+txtDate.getText()+"',stu_score="+txtScore.getText()+" where username='"+txtName.getText()+"'");
System.out.println("记录修改完毕!");
btnModify.setEnabled(false);
txtName.setText("");
txtScore.setText("");
txtDate.setText("");
txtDate.setEditable(false);
txtScore.setEditable(false);
con.close();
}catch(SQLException e){
}
}
}
public static void main(String[] args) {
new ModifyForm();
}
}
按名字查询数据
package com.studensystem;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import com.studensystem.SqlHelper;
public class NameQueryForm extends JFrame implements ActionListener
{
JLabel labScore=new JLabel("请输入成绩:");
JTextField txtScore=new JTextField(10);
JButton btnQuery=new JButton("查询");
JPanel pan1=new JPanel();
JPanel pan2=new JPanel();
String []str={"姓名","出生日期","成绩"};
Object[][] data=new Object[10][3];
JTable table=new JTable(data,str);
JTableHeader head=table.getTableHeader();
JScrollPane jsp=new JScrollPane(table); //滚动
Connection con;
Statement sql;
ResultSet rs;
NameQueryForm()
{
super("按名字查询");
setSize(400,300);
pan1.add(labScore);
pan1.add(txtScore);
pan1.add(btnQuery); //以上3条在第一个面板加入内容
getContentPane().add(pan1,"North"); //把面板1放到窗口顶部
pan2.setLayout(new BorderLayout());
pan2.add(head,"North"); //将表头放在面板2的顶部
pan2.add(jsp,"Center"); //表格放在pan2中间
getContentPane().add(pan2,"Center");//把面板2整体加入到主窗口中
btnQuery.addActionListener(this); //增加查询按钮的监视器
setVisible(true);
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==btnQuery){
int i,j,row;
try{
Class.forName("com.mysql.jdbc.Driver");}
catch(ClassNotFoundException e){
}
try{ con=DriverManager.getConnection("driver","user","passwd");
sql=con.createStatement();
rs=sql.executeQuery("select COUNT(*) as rowcount from student where stu_score="+txtScore.getText()); //寻找相同成绩的行数
rs.next(); //去掉头
row=rs.getInt("rowcount");
rs.close();
data=new Object[row][3];
rs=sql.executeQuery("select * from student where stu_score="+txtScore.getText());
i=0;j=0;
while(rs.next()){
data[i][j++]=rs.getString("姓名");
data[i][j++]=rs.getDate("出生日期");
data[i][j]=new Integer(rs.getInt("成绩"));//将数据库中查询到的信息保存到data中
i++;j=0;
}
pan2.removeAll();
getContentPane().remove(pan2);
table=new JTable(data,str);
pan2.setLayout(new BorderLayout());
head=table.getTableHeader();
jsp=new JScrollPane(table);
pan2.add(head,"North");
pan2.add(jsp,"Center");
getContentPane().add(pan2,"Center");
this.validate();
con.close();
}catch(SQLException e){
}
}
}
public static void main(String[] args) {
new NameQueryForm();
}
}
按成绩查询数据
package com.studensystem;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import com.studensystem.SqlHelper;
public class ScoreQueryForm extends JFrame implements ActionListener
{
JLabel labScore=new JLabel("请输入成绩:");
JTextField txtScore=new JTextField(10);
JButton btnQuery=new JButton("查询");
JPanel pan1=new JPanel();
JPanel pan2=new JPanel();
String []str={"姓名","出生日期","成绩"};
Object[][] data=new Object[10][3];
JTable table=new JTable(data,str);
JTableHeader head=table.getTableHeader();
JScrollPane jsp=new JScrollPane(table); //滚动
Connection con;
Statement sql;
ResultSet rs;
ScoreQueryForm()
{
super("按成绩查询");
setSize(400,300);
pan1.add(labScore);
pan1.add(txtScore);
pan1.add(btnQuery); //以上3条在第一个面板加入内容
getContentPane().add(pan1,"North"); //把面板1放到窗口顶部
pan2.setLayout(new BorderLayout());
pan2.add(head,"North"); //将表头放在面板2的顶部
pan2.add(jsp,"Center"); //表格放在pan2中间
getContentPane().add(pan2,"Center");//把面板2整体加入到主窗口中
btnQuery.addActionListener(this); //增加查询按钮的监视器
setVisible(true);
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==btnQuery){
int i,j,row;
try{
Class.forName("com.mysql.jdbc.Driver");}
catch(ClassNotFoundException e){
}
try{
con=DriverManager.getConnection("driver","user","passwd");
sql=con.createStatement();
rs=sql.executeQuery("select COUNT(*) as rowcount from student where stu_score="+txtScore.getText()); //寻找相同成绩的行数
rs.next(); //去掉头
row=rs.getInt("rowcount");
rs.close();
data=new Object[row][3];
rs=sql.executeQuery("select * from student where stu_score="+txtScore.getText());
i=0;j=0;
while(rs.next()){
data[i][j++]=rs.getString("姓名");
data[i][j++]=rs.getDate("出生日期");
data[i][j]=new Integer(rs.getInt("成绩"));//将数据库中查询到的信息保存到data中
i++;j=0;
}
pan2.removeAll();
getContentPane().remove(pan2);
table=new JTable(data,str);
pan2.setLayout(new BorderLayout());
head=table.getTableHeader();
jsp=new JScrollPane(table);
pan2.add(head,"North");
pan2.add(jsp,"Center");
getContentPane().add(pan2,"Center");
this.validate();
con.close();
}catch(SQLException e){
}
}
}
public static void main(String[] args) {
new ScoreQueryForm();
}
}
连接数据库操作
package com.studensystem;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class SqlHelper {
//========数据库
private Connection ct = null;
private PreparedStatement ps = null;
private ResultSet rs = null;
private String driver = "com.mysql.jdbc.Driver"; //后面加上utf-8 不然中文乱码
private String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf-8";
private String user = "root";
private String passwd = "";
//========查询
public ResultSet queryExecute(String sql, String []paras)
{
try {
//========1、加载驱动
Class.forName(driver);
//========2、连接
ct = DriverManager.getConnection(url, user, passwd);
//========3、创建PreparedStatement
ps = ct.prepareStatement(sql);
//========4、给问号赋值
if(paras != null) {
for(int i = 0; i < paras.length; i++) {
ps.setString(i + 1, paras[i]);
}
}
//========5、执行
rs = ps.executeQuery();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
//this.close();
}
//========返回值
return rs;
}
//========增删改
public boolean cudExecute(String sql, String []paras)
{
boolean b = true;
try {
//========1、加载驱动
Class.forName(driver);
//========2、连接
ct = DriverManager.getConnection(url, user, passwd);
//========3、创建PreparedStatement
ps = ct.prepareStatement(sql);
//========4、给问号赋值
for(int i = 0; i < paras.length; i++) {
ps.setString(i + 1, paras[i]);
}
//========5、执行
if(ps.executeUpdate() != 1) b = false;
} catch (Exception e) {
// TODO: handle exception
b = false;
e.printStackTrace();
} finally {
this.close();
}
//========返回值
return b;
}
//========关闭资源
public void close()
{
try {
if(rs!=null) rs.close();
if(ps!=null) ps.close();
if(ct!=null) ct.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
截图演示
1.JPG
2.JPG
3.JPG
4.JPG
5.JPG