關(guān)于學(xué)生成績管理系統(tǒng)的界面設(shè)計:代碼如下
數(shù)據(jù)庫表設(shè)計
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;
主界面設(shè)計
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("學(xué)生信息管理");
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("學(xué)期信息管理");
JMenuItem mAbout=new JMenuItem("操作");
JMenuItem mAbout1 = new JMenuItem("");
JMenuBar mBar=new JMenuBar();
MainForm()
{
super("學(xué)生成績管理系統(tǒng)");
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,"學(xué)生成績管理系統(tǒng)\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();
}
}
添加界面設(shè)計
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("添加數(shù)據(jù)");
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();
}
}
刪除數(shù)據(jù)設(shè)計
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("刪除數(shù)據(jù)");
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){ //實現(xiàn)查詢功能
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);//使刪除按鈕功能實現(xiàn)
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) //實現(xiàn)刪除功能
{
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();
}
}
修改界面設(shè)計
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("修改數(shù)據(jù)");
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();
}
}
按名字查詢數(shù)據(jù)
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條在第一個面板加入內(nèi)容
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); //增加查詢按鈕的監(jiān)視器
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()); //尋找相同成績的行數(shù)
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("成績"));//將數(shù)據(jù)庫中查詢到的信息保存到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();
}
}
按成績查詢數(shù)據(jù)
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條在第一個面板加入內(nèi)容
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); //增加查詢按鈕的監(jiān)視器
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()); //尋找相同成績的行數(shù)
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("成績"));//將數(shù)據(jù)庫中查詢到的信息保存到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();
}
}
連接數(shù)據(jù)庫操作
package com.studensystem;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class SqlHelper {
//========數(shù)據(jù)庫
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亡电、加載驅(qū)動
Class.forName(driver);
//========2、連接
ct = DriverManager.getConnection(url, user, passwd);
//========3硅瞧、創(chuàng)建PreparedStatement
ps = ct.prepareStatement(sql);
//========4份乒、給問號賦值
if(paras != null) {
for(int i = 0; i < paras.length; i++) {
ps.setString(i + 1, paras[i]);
}
}
//========5、執(zhí)行
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腕唧、加載驅(qū)動
Class.forName(driver);
//========2或辖、連接
ct = DriverManager.getConnection(url, user, passwd);
//========3、創(chuàng)建PreparedStatement
ps = ct.prepareStatement(sql);
//========4枣接、給問號賦值
for(int i = 0; i < paras.length; i++) {
ps.setString(i + 1, paras[i]);
}
//========5颂暇、執(zhí)行
if(ps.executeUpdate() != 1) b = false;
} catch (Exception e) {
// TODO: handle exception
b = false;
e.printStackTrace();
} finally {
this.close();
}
//========返回值
return b;
}
//========關(guān)閉資源
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