期末作业.docx
《期末作业.docx》由会员分享,可在线阅读,更多相关《期末作业.docx(21页珍藏版)》请在冰豆网上搜索。
![期末作业.docx](https://file1.bdocx.com/fileroot1/2022-11/26/b103d7bb-9cc5-4168-9c28-a89854970bef/b103d7bb-9cc5-4168-9c28-a89854970bef1.gif)
期末作业
学生成绩管理系统
程序代码及运行效果:
主类代码:
/*主类代码*/
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
classMyPanelextendsJPanel{
Imageimg=Toolkit.getDefaultToolkit().getImage("c:
/a.jpg");
publicvoidpaint(Graphicsg){
g.drawImage(img,0,0,this);
}
}
publicclassMainFormextendsJFrameimplementsActionListener
{
JMenumSystem=newJMenu("系统");
JMenuItemmExit=newJMenuItem("退出");
JMenumOperate=newJMenu("数据操作");
JMenuItemmAdd=newJMenuItem("添加");
JMenuItemmDel=newJMenuItem("删除");
JMenuItemmModify=newJMenuItem("修改");
JMenumQuery=newJMenu("查询");
JMenuItemmName=newJMenuItem("按姓名查询");
JMenuItemmScore=newJMenuItem("按成绩查询");
JMenumHelp=newJMenu("帮助");
JMenuItemmAbout=newJMenuItem("软件信息");
JMenuBarmBar=newJMenuBar();
MainForm()
{
super("学生成绩管理系统");
setSize(875,583);
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(newMyPanel());
setVisible(true);
}
publicvoidactionPerformed(ActionEventae){
if(ae.getSource()==mExit)
System.exit(0);
elseif(ae.getSource()==mAbout)
JOptionPane.showMessageDialog(this,"学生成绩管理系统\n\n应用科学学院\n\n2011年4月","软件信息",JOptionPane.INFORMATION_MESSAGE);
elseif(ae.getSource()==mAdd)
newAddForm().setVisible(true);
elseif(ae.getSource()==mDel)
newDeleteForm().setVisible(true);
elseif(ae.getSource()==mModify)
newModifyForm().setVisible(true);
elseif(ae.getSource()==mName)
newNameQueryForm().setVisible(true);
elseif(ae.getSource()==mScore)
newScoreQueryForm().setVisible(true);
}
publicstaticvoidmain(String[]args){
newMainForm();
}
}
运行效果:
修改数据:
/*修改数据*/
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
importjava.sql.*;
publicclassModifyFormextendsJFrameimplementsActionListener
{
JLabellabName=newJLabel("姓名:
");
JLabellabDate=newJLabel("出生日期:
");
JLabellabScore=newJLabel("成绩:
");
JTextFieldtxtName=newJTextField(20);
JTextFieldtxtDate=newJTextField(18);
JTextFieldtxtScore=newJTextField(20);
JButtonbtnModify=newJButton("修改");
JButtonbtnCancel=newJButton("取消");
JButtonbtnQuery=newJButton("查询");
JPanelpan=newJPanel();
JPanelpan1=newJPanel();
JPanelpan2=newJPanel();
JPanelpan3=newJPanel();
JPanelpan4=newJPanel();
Connectioncon;
Statementsql;
ResultSetrs;
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(newGridLayout(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();
}
publicvoidactionPerformed(ActionEventae){
if(ae.getSource()==btnCancel){
dispose();
}elseif(ae.getSource()==btnQuery){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(ClassNotFoundExceptione){
}
try{con=DriverManager.getConnection("jdbc:
odbc:
sun","gxy","123");
sql=con.createStatement();
rs=sql.executeQuery("select*from成绩表where姓名='"+txtName.getText()+"'");
if(rs.next()){
txtName.setText(rs.getString("姓名"));
txtScore.setText(newInteger(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(SQLExceptione){
}
}elseif(ae.getSource()==btnModify){
try{
System.out.println("Update成绩表set出生日期='"+txtDate.getText()+"',成绩="+txtScore.getText()+"where姓名='"+txtName.getText()+"'");
sql.executeUpdate("Update成绩表set出生日期='"+txtDate.getText()+"',成绩="+txtScore.getText()+"where姓名='"+txtName.getText()+"'");
System.out.println("记录修改完毕!
");
btnModify.setEnabled(false);
txtName.setText("");
txtScore.setText("");
txtDate.setText("");
txtDate.setEditable(false);
txtScore.setEditable(false);
con.close();
}catch(SQLExceptione){
}
}
}
publicstaticvoidmain(String[]args){
newModifyForm();
}
}
运行效果:
删除数据:
/*删除数据*/
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
importjava.sql.*;
publicclassDeleteFormextendsJFrameimplementsActionListener
{
JLabellabName=newJLabel("姓名:
");
JLabellabDate=newJLabel("出生日期:
");
JLabellabScore=newJLabel("成绩:
");
JTextFieldtxtName=newJTextField(20);
JTextFieldtxtDate=newJTextField(18);
JTextFieldtxtScore=newJTextField(20);
JButtonbtnDelete=newJButton("删除");
JButtonbtnCancel=newJButton("取消");
JButtonbtnQuery=newJButton("查询");
JPanelpan=newJPanel();
JPanelpan1=newJPanel();
JPanelpan2=newJPanel();
JPanelpan3=newJPanel();
JPanelpan4=newJPanel();
Connectioncon;
Statementsql;
ResultSetrs;
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(newGridLayout(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();
}
publicvoidactionPerformed(ActionEventae)
{
if(ae.getSource()==btnCancel)
{
dispose();
}
elseif(ae.getSource()==btnQuery){//实现查询功能
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(ClassNotFoundExceptione){
}
try{con=DriverManager.getConnection("jdbc:
odbc:
sun","gxy","123");
sql=con.createStatement();
rs=sql.executeQuery("select*from成绩表where姓名='"+txtName.getText()+"'");
if(rs.next())
{
txtName.setText(rs.getString("姓名"));
txtScore.setText(newInteger(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(SQLExceptione){
}
}
elseif(ae.getSource()==btnDelete)//实现删除功能
{
try
{
System.out.println("Update成绩表set出生日期='"+txtDate.getText()+"',成绩="+txtScore.getText()+"where姓名='"+txtName.getText()+"'");
sql.executeUpdate("DELETEFROM成绩表where姓名='"+txtName.getText()+"'");
System.out.println("记录删除完毕!
");
btnDelete.setEnabled(false);
txtName.setText("");
txtScore.setText("");
txtDate.setText("");
txtDate.setEditable(false);
txtScore.setEditable(false);
con.close();
}
catch(SQLExceptione){
}
}
}
publicstaticvoidmain(String[]args)
{
newDeleteForm();
}
}
运行效果:
添加数据:
/*添加数据*/
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
importjava.sql.*;
publicclassAddFormextendsJFrameimplementsActionListener
{
JLabellabName=newJLabel("姓名:
");
JLabellabDate=newJLabel("出生日期:
");
JLabellabScore=newJLabel("成绩:
");
JTextFieldtxtName=newJTextField(20);
JTextFieldtxtDate=newJTextField(18);
JTextFieldtxtScore=newJTextField(20);
JButtonbtnAdd=newJButton("添加");
JButtonbtnCancel=newJButton("取消");
JPanelpan=newJPanel();
JPanelpan1=newJPanel();
JPanelpan2=newJPanel();
JPanelpan3=newJPanel();
JPanelpan4=newJPanel();
Connectioncon;
Statementsql;
ResultSetrs;
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(newGridLayout(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();
}
publicvoidactionPerformed(ActionEventae)
{
Stringrecode,insert1,出生日期,姓名;
int成绩;
if(ae.getSource()==btnCancel)
{
this.dispose();
}
elseif(ae.getSource()==btnAdd)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(ClassNotFoundExceptione){
}
try{con=DriverManager.getConnection("jdbc:
odbc:
sun","gxy","123");
sql=con.createStatement();
姓名=txtName.getText();
出生日期=txtDate.getText();
成绩=Integer.parseInt(txtScore.getText());
recode="("+"'"+姓名+"'"+","+"'"+出生日期+""+"',"+成绩+")";
insert1="INSERTINTO成绩表VALUES"+recode;
sql.executeUpdate(insert1);
System.out.println("记录添加完毕!
");
btnAdd.setEnabled(false);
txtName.setText("");
txtScore.setText("");
txtDate.setText("");
con.close();
}
catch(