JAVA编写的学生基本信息管理系统

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

"学生基本信息管理系统"运行说明:
1.运行系统
2.输入管理员姓名和密码,点击"进入"按钮,进行步骤3;若输入的姓名或密码不正确,则弹出提示对话框,提示"姓名输入错误或密码输入错误!"
3.进入操作界面
(1)若选择了"菜单选项"下的"录入学生基本信息",则进入录入界面:
输入要录入的学号,姓名,选择性别、专业、年级、出生日期;
若学号字段为空,则弹出提示对话框提示"学号字段不能为空!",若其它字段为空则弹出对话框提示"选项不能为空!";
在正确的输入下点击"录入"按钮,弹出提示对话框提示"录入成功".若要继续录入点击"重置"按钮.
(2)若选择了"菜单选项"下的"修改学生基本信息",则进入修改界面:
输入要修改的学号,
点击"开始修改"按钮,在下面的选项中会显示待修改的学生的基本信息,此时依次进行修改,修改完成后,点击"录入修改",弹出对话框提示"修改成功!";
若学号输入为空,则弹出对话框提示"学号字段不能为空!";
若输入的学号没有在库中,则弹出对话框提示"该学号不存在!请确认后再输入!";
(3)若选择了"菜单选项"下的"查询学生基本信息",则进入查询界面:
输入要查询的学号,点击"查询"按钮,该学生的基本信息就会在下面的相应项中显示出来;
若输入学号为空则弹出对话框提示"学号字段不能为空!";
若输入的学号不存在则弹出对话框提示"该学号不存在!请确认后再输入!";
(4)若选择了"菜单选项"下的"删除学生基本信息",则进入删除界面:
输入要删除的学号,打回车,则该学生的相关信息就会在下面的相应项中显示出来,点击"删除"按钮,弹出对话框"确定要删除该学生的基本信息吗?",点击"是",则该生基本信息被删除,点击"否",则取消操作;
(5)若选择了"设置选项"下的"重新设置"菜单,则弹出重新设置窗口:
输入新的管理员姓名,密码及确认密码,点击"重新注册",登陆的姓名和密码就会改变,点击"取消",不改变.
(6)若选择了"设置选项"下的"退出"菜单项,则退出操作界面,返回登陆界面.
注:行动环境eclipse-SDK-3.4.1-win32(JA V A的一种集成开发平台).zip
总体设计
本系统共包括8个java源文件和一个SQL Server数据库。

1.类之间的关系如图1所示。

2.Java源文件及其功能如表1所示
表1 源文件功能简介
1.主类StudentManagers.java
(1)成员变量(表2)
表2 成员变量
(2)方法(表3)
表3 方法
import java.awt.BorderLayout;
import ponent;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class StudentManagers {
static final JFrame mainf=new JFrame();
JLabel welcome=new JLabel("欢迎访问学生信息管理系统!");
JLabel labname=new JLabel("请输入管理员姓名:");
public JTextField txtname=new JTextField();
JLabel labpwd=new JLabel("请输入密码: "); public JPasswordField txtpwd=new JPasswordField();
JButton btnin=new JButton("进入");
JPanel p;
public StudentManagers()
{
p=new JPanel();
p.removeAll();
p.setSize(400,400);
txtname.requestFocus();
txtname.setText("");
txtpwd.setText("");
p.setBorder(BorderFactory.createLoweredBevelBorder());
p.setLayout(new GridBagLayout());
Layoututil.add(p, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0,0, 0, 0, 1,1,labname);
Layoututil.add(p,
GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER,1,0, 1, 0,1,1,txtname);
Layoututil.add(p,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,0,
1,1,1,labpwd);
Layoututil.add(p,GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER,1,0, 1, 1, 1,1,txtpwd);
Layoututil.add(p,GridBagConstraints.NONE, GridBagConstraints.CENTER,1, 0, 3, 1, 1, 1, btnin);
mainf.getContentPane().add(p,BorderLayout.CENTER);
txtname.requestFocus();
btnin.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String a1=txtname.getText();
String a2=txtpwd.getText();
txtname.setText("");
txtpwd.setText("");
boolean tof=false;
jdbcenroll checks=new jdbcenroll();
tof=checks.check(a1,a2);
if(tof==false)
{
JOptionPane.showMessageDialog(null,"姓名输入错误或密码输入错误!","警告",JOptionPane.W ARNING_MESSAGE);
}
else if(tof==true)
{
new Landat();
mainf.setVisible(false);
}
}
});
JPanel ap=new JPanel();
ap.setLayout(new BorderLayout());
ap.add(welcome,BorderLayout.CENTER);
mainf.getContentPane().add(ap,BorderLayout.NORTH);
mainf.setSize(400,400);
mainf.show();
mainf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
JPanel mainp=new JPanel();
StudentManagers student=new StudentManagers();
}
}
2.类Landat.java
(1).成员变量(表4)
import java.awt.*;
import java.sql.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.TextEvent;
public class Landat extends JFrame implements ActionListener{
private static final int Sytle = 0;
JFrame f;
JPanel p;//登陆
JPanel p2;//录入
JPanel p3;//修改
JPanel p4;//查询
JPanel p5;//删除
JPanel newp;
/*final int i=0;//控制菜单项的打开次数(录入学生基本信息)
final int i1=0;//控制(修改学生基本信息)项
final int i2=0;//控制(查询学生基本信息)项
final int i3=0;//控制(删除学生基本信息)项*/
public Landat()
{
super.setVisible(false);
f=new JFrame("学生学籍管理系统");
p=new JPanel();
p2=new JPanel();
p3=new JPanel();
p4=new JPanel();
p5=new JPanel();
newp=new JPanel();
Box box1=Box.createHorizontalBox();
JLabel label=new JLabel(" 欢迎进入学生基本信息管理系统");
label.setForeground(Color.red);
int style=Font.ITALIC;
Font fo=new Font("宋体",style,25);
label.setFont(fo);
box1.add(label,JLabel.CENTER);
p.setLayout(new BorderLayout());
p.add(box1,BorderLayout.CENTER);
Container contentPane=f.getContentPane();
contentPane.add(p,BorderLayout.CENTER);
JMenuBar mainmenubar=new JMenuBar();
JMenu titlebar=new JMenu("菜单选项");
JMenuItem inputs=new JMenuItem("录入学生基本信息");
inputs.addActionListener(this);
JMenuItem modify=new JMenuItem("修改学生基本信息");
modify.addActionListener(this);
JMenuItem inquest=new JMenuItem("查询学生基本信息");
inquest.addActionListener(this);
JMenuItem delete=new JMenuItem("删除学生基本信息");
delete.addActionListener(this);
titlebar.add(inputs);
titlebar.add(modify);
titlebar.add(inquest);
titlebar.add(delete);
JMenu messagebar=new JMenu("设置选项");
JMenuItem reset=new JMenuItem("重新设置");
reset.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
reenroll reenrolls=new reenroll();
}
});
JMenuItem otherset=new JMenuItem("退出");
otherset.addActionListener(this);
messagebar.add(reset);
messagebar.add(otherset);
mainmenubar.add(titlebar);
mainmenubar.add(messagebar);
f.pack();
f.setJMenuBar(mainmenubar);
f.setSize(400,400);
f.show();
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="录入学生基本信息")
{
p.setVisible(false);
p2.setVisible(false);
p3.setVisible(false);
p4.setVisible(false);
p5.setVisible(false);
StudentSituation stusituation=new StudentSituation(p2);
f.getContentPane().add(p2);
p2.setVisible(true);
}
else if(e.getActionCommand()=="修改学生基本信息")
{
p.setVisible(false);
p2.setVisible(false);
p3.setVisible(false);
p4.setVisible(false);
p5.setVisible(false);
ModifySituation modsituation=new ModifySituation(p3);
f.getContentPane().add(p3);
p3.setVisible(true);
}
else if(e.getActionCommand()=="查询学生基本信息")
{
p.setVisible(false);
p2.setVisible(false);
p3.setVisible(false);
p4.setVisible(false);
p5.setVisible(false);
Inquest quest=new Inquest(p4);
f.getContentPane().add(p4);
p4.setVisible(true);
}
else if(e.getActionCommand()=="删除学生基本信息")
{
p.setVisible(false);
p2.setVisible(false);
p3.setVisible(false);
p4.setVisible(false);
p5.setVisible(false);
Delete deletes=new Delete(p5);
f.getContentPane().add(p5);
p5.setVisible(true);
}
else if(e.getActionCommand()=="退出")
{
StudentManagers me=new StudentManagers();
f.setVisible(false);
}
}
}
class reenroll extends JFrame {
JTextField txtname,txtpwd,txtrepwd;
JFrame ff;
JPanel pp;
public reenroll(){
ff=new JFrame();
pp=new JPanel();
txtname=new JTextField(10);
txtpwd=new JTextField(10);
txtrepwd=new JTextField(10);
txtrepwd.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(!txtrepwd.getText().equals(txtpwd.getText())){
JOptionPane.showMessageDialog(null,"密码输入错误!","警告",JOptionPane.W ARNING_MESSAGE);
}
}
public void valueChanged(TextEvent e)
{
}
});
JButton ok=new JButton("重新注册");
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
jdbcenroll reset=new jdbcenroll();
reset.setloads(txtname.getText(),txtpwd.getText());
}
});
JButton cancel=new JButton("取消");
cancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
ff.setVisible(false);
}
});
Box box1=Box.createHorizontalBox();
box1.add(new JLabel("请输入新的管理员姓名:",JLabel.CENTER));
box1.add(txtname);
Box box2=Box.createHorizontalBox();
box2.add(new JLabel("请输入新的管理员密码:",JLabel.CENTER));
box2.add(txtpwd);
Box box3=Box.createHorizontalBox();
box3.add(new JLabel("请确认密码:",JLabel.CENTER));
box3.add(txtrepwd);
Box box4=Box.createHorizontalBox();
box4.add(ok);
box4.add(cancel);
Box boxH=Box.createV erticalBox();
boxH.add(box1);
boxH.add(box2);
boxH.add(box3);
boxH.add(box4);
boxH.setSize(300,200);
ff.setSize(300,200);
pp.add(boxH,BorderLayout.CENTER);
ff.add(pp);
ff.setVisible(true);
}
}
3.类StudentSituation
(1)成员变量(表6)
表6 成员变量
(2)方法(表7)
(3)代码实现
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class StudentSituation {
JTextField txtnumber,txtname;
JComboBox txtspecial,txtgrade,txtborn; JRadioButton radioboy,radiogirl;
ButtonGroup group=null;
JButton truein,resetin;
public StudentSituation(JPanel psituation)
{
psituation.removeAll();
psituation.setSize(300,300);
txtnumber=new JTextField(10);
txtname=new JTextField(10);
radioboy=new JRadioButton("男");
radiogirl=new JRadioButton("女");
group=new ButtonGroup();
txtspecial=new JComboBox();
txtspecial.addItem("计算机");
txtspecial.addItem("英语");
txtspecial.addItem("机械");
txtspecial.addItem("汽服");
txtspecial.addItem("音乐");
txtspecial.addItem("汉语学");
txtgrade=new JComboBox();
txtgrade.addItem("2004级");
txtgrade.addItem("2005级");
txtgrade.addItem("2006级");
txtgrade.addItem("2007级");
txtgrade.addItem("2008级");
txtgrade.addItem("2009级");
txtgrade.addItem("2010级");
txtborn=new JComboBox();
txtborn.addItem("1985");
txtborn.addItem("1986");
txtborn.addItem("1987");
txtborn.addItem("1988");
txtborn.addItem("1989");
txtborn.addItem("1990");
txtborn.addItem("1991");
truein=new JButton("录入");
truein.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ String number=txtnumber.getText();
String name=txtname.getText();
String sex = null;
if(radioboy.isSelected())
sex=radioboy.getText();
else if(radiogirl.isSelected())
sex=radiogirl.getText();
String special=null;
Object sp = null;
String grade=null;
Object gr = null;
String born=null;
Object bo = null;
int index1=txtspecial.getSelectedIndex(); switch(index1)
{
case 0: sp=txtspecial.getItemAt(0);break; case 1:sp=txtspecial.getItemAt(1);break; case 2:sp=txtspecial.getItemAt(2);break; case 3:sp=txtspecial.getItemAt(3);break; case 4:sp=txtspecial.getItemAt(4);break; case 5:sp=txtspecial.getItemAt(5);break; }
special=(String)sp;
int index2=txtgrade.getSelectedIndex(); switch(index2)
{
case 0:gr=txtgrade.getItemAt(0);break; case 1:gr=txtgrade.getItemAt(1);break; case 2:gr=txtgrade.getItemAt(2);break; case 3:gr=txtgrade.getItemAt(3);break; case 4:gr=txtgrade.getItemAt(4);break; case 5:gr=txtgrade.getItemAt(5);break; case 6:gr=txtgrade.getItemAt(6);break; }
grade=(String)gr;
int index3=txtborn.getSelectedIndex(); switch(index3)
{
case 0:bo=txtborn.getItemAt(0);break; case 1:bo=txtborn.getItemAt(1);break; case 2:bo=txtborn.getItemAt(2);break; case 3:bo=txtborn.getItemAt(3);break; case 4:bo=txtborn.getItemAt(4);break; case 5:bo=txtborn.getItemAt(5);break; case 6:bo=txtborn.getItemAt(6);break; }
born=(String)bo;
jdbcenroll inputs=new jdbcenroll();
inputs.input(number,name,sex,special,grade, born);
}
});
resetin=new JButton("重置");
resetin.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
txtnumber.setText("");
txtname.setText("");
radioboy.setSelected(false);
radiogirl.setSelected(false);
}
});
group.add(radioboy);
group.add(radiogirl);
Box box1=Box.createHorizontalBox();
box1.add(new JLabel("学号:",JLabel.CENTER));
box1.add(txtnumber);
Box box2=Box.createHorizontalBox();
box2.add(new JLabel("姓名:",JLabel.CENTER));
box2.add(txtname);
Box box3=Box.createHorizontalBox();
box3.add(new JLabel("性别:",JLabel.CENTER));
box3.add(radioboy);
box3.add(radiogirl);
Box box4=Box.createHorizontalBox();
box4.add(new JLabel("专业:",JLabel.CENTER));
box4.add(txtspecial);
Box box5=Box.createHorizontalBox();
box5.add(new JLabel("年级:",JLabel.CENTER));
box5.add(txtgrade);
Box box6=Box.createHorizontalBox();
box6.add(new JLabel("出生:"));
box6.add(txtborn);
Box box7=Box.createHorizontalBox();
box7.add(truein);
box7.add(resetin);
Box boxH=Box.createV erticalBox();
boxH.add(box1);
boxH.add(box2);
boxH.add(box3);
boxH.add(box4);
boxH.add(box5);
boxH.add(box6);
boxH.add(box7);
boxH.setSize(300,300);
psituation.add(boxH);
}
}
4.类Inquest
(1).成员变量(表8)
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Inquest{
JPanel p;
JTextField number,name,special,grade,born;
JButton search;
JRadioButton boy,girl;
ButtonGroup group=null;
public Inquest(JPanel p){
p.removeAll();
p.setSize(300,300);
number=new JTextField(10);
search=new JButton("查询");
//查询事件监听
search.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String[] searched=new String[5];
String searchnumber=number.getText();
jdbcenroll searchs=new jdbcenroll();
searched=searchs.search(searchnumber);
name.setText(searched[0]);
if(searched[1].equals("男"))
{
boy.setSelected(true);
girl.setSelected(false);
}
else if(searched[1].equals("女"))
{
girl.setSelected(true);
boy.setSelected(false);
}
special.setText(searched[2]);
grade.setText(searched[3]);
born.setText(searched[4]);
}
});
name=new JTextField(10);
name.setEditable(false);
group=new ButtonGroup();
boy=new JRadioButton("男");
girl=new JRadioButton("女");
group.add(boy);
group.add(girl);
special=new JTextField(10);
special.setEditable(false);
grade=new JTextField(10);
grade.setEditable(false);
born=new JTextField(10);
born.setEditable(false);
Box box1=Box.createHorizontalBox();
box1.add(new JLabel("输入要查询的学号:",JLabel.CENTER)); box1.add(number);
box1.add(search);
Box box2=Box.createHorizontalBox();
box2.add(new JLabel("姓名:",JLabel.CENTER));
box2.add(name);
Box box3=Box.createHorizontalBox();
box3.add(new JLabel("性别:",JLabel.CENTER));
box3.add(boy);
box3.add(girl);
Box box4=Box.createHorizontalBox();
box4.add(new JLabel("专业:",JLabel.CENTER));
box4.add(special);
Box box5=Box.createHorizontalBox();
box5.add(new JLabel("年级:",JLabel.CENTER));
box5.add(grade);
Box box6=Box.createHorizontalBox();
box6.add(new JLabel("出生:",JLabel.CENTER));
box6.add(born);
Box boxH=Box.createV erticalBox();
boxH.add(box1);
boxH.add(box2);
boxH.add(box3);
boxH.add(box4);
boxH.add(box5);
boxH.add(box6);
p.add(boxH);
}
}
5.类ModifySituation
(1).成员变量(表10)
表10 成员变量
(3)实现代码
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ModifySituation {
JTextField tnewnumber,tnewname;
JComboBox tnewspecial,tnewgrade,tnewborn; JRadioButton sexman,sexwom;
JButton startset,inset,reset;
ButtonGroup group=null;
String numbers=null;
public ModifySituation(JPanel mods)
{
mods.removeAll();
mods.setSize(300,300);
tnewnumber=new JTextField(10);
numbers=tnewnumber.getText();
tnewnumber.setEditable(true);
tnewname=new JTextField(10);
tnewname.setEditable(false);
sexman=new JRadioButton("男"); sexman.setSelected(false);
sexwom=new JRadioButton("女"); sexwom.setSelected(false);
tnewspecial=new JComboBox(); tnewspecial.setEditable(false); tnewspecial.addItem("计算机"); tnewspecial.addItem("英语"); tnewspecial.addItem("机械"); tnewspecial.addItem("汽服"); tnewspecial.addItem("音乐"); tnewspecial.addItem("汉语学");
// tnewspecial.setEditable(false); tnewgrade=new JComboBox(); tnewgrade.setEditable(false); tnewgrade.addItem("2004级"); tnewgrade.addItem("2005级"); tnewgrade.addItem("2006级"); tnewgrade.addItem("2007级"); tnewgrade.addItem("2008级"); tnewgrade.addItem("2009级"); tnewgrade.addItem("2010级");
//tnewgrade.setEditable(false);
tnewborn=new JComboBox(); tnewborn.setEditable(false);
tnewborn.addItem("1985");
tnewborn.addItem("1986");
tnewborn.addItem("1987");
tnewborn.addItem("1988");
tnewborn.addItem("1989");
tnewborn.addItem("1990");
tnewborn.addItem("1991");
//tnewborn.setEditable(false);
startset=new JButton("开始修改");
startset.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try{
tnewname.setEditable(true);
tnewspecial.setEditable(true);
tnewgrade.setEditable(true);
tnewborn.setEditable(true);
inset.setEnabled(true);
String[] searched=new String[5];
String searchnumber=tnewnumber.getText();
jdbcenroll searchs=new jdbcenroll();
searched=searchs.search(searchnumber);
tnewname.setText(searched[0]);
if(searched[1].equals("男"))
{
sexman.setSelected(true);
sexwom.setSelected(false);
}
else if(searched[1].equals("女"))
{
sexwom.setSelected(true);
sexman.setSelected(false);
}
tnewspecial.setSelectedItem((String)searched[2]);
tnewgrade.setSelectedItem((String)searched[3]);
tnewborn.setSelectedItem((String)searched[4]);
}catch(NullPointerException e1)
{
e1.printStackTrace();
}
}
});
inset=new JButton("录入修改");
inset.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String number=tnewnumber.getText(); String name=tnewname.getText();
String sex=null;
if(sexman.isSelected())
sex=sexman.getText();
else if(sexwom.isSelected())
sex=sexwom.getText();
String special=null;
Object sp = null;
String grade=null;
Object gr = null;
String born=null;
Object bo = null;
int index1=tnewspecial.getSelectedIndex(); switch(index1)
{
case 0:sp=tnewspecial.getItemAt(0);break; case 1:sp=tnewspecial.getItemAt(1);break; case 2:sp=tnewspecial.getItemAt(2);break; case 3:sp=tnewspecial.getItemAt(3);break; case 4:sp=tnewspecial.getItemAt(4);break; case 5:sp=tnewspecial.getItemAt(5);break; }
special=(String)sp;
int index2=tnewgrade.getSelectedIndex(); switch(index2)
{
case 0:gr=tnewgrade.getItemAt(0);break; case 1:gr=tnewgrade.getItemAt(1);break; case 2:gr=tnewgrade.getItemAt(2);break; case 3:gr=tnewgrade.getItemAt(3);break; case 4:gr=tnewgrade.getItemAt(4);break; case 5:gr=tnewgrade.getItemAt(5);break; case 6:gr=tnewgrade.getItemAt(6);break; }
grade=(String)gr;
int index3=tnewborn.getSelectedIndex(); switch(index3)
{
case 0:bo=tnewborn.getItemAt(0);break; case 1:bo=tnewborn.getItemAt(1);break; case 2:bo=tnewborn.getItemAt(2);break; case 3:bo=tnewborn.getItemAt(3);break; case 4:bo=tnewborn.getItemAt(4);break;
case 5:bo=tnewborn.getItemAt(5);break;
case 6:bo=tnewborn.getItemAt(6);break;
}
born=(String)bo;
jdbcenroll modifys=new jdbcenroll();
modifys.modify(number, name, sex, special, grade, born);
}
});
inset.setEnabled(false);
reset=new JButton("重置");
reset.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
tnewnumber.setText("");
tnewname.setText("");
sexman.setSelected(false);
sexwom.setSelected(false);
}
});
group=new ButtonGroup();
group.add(sexman);
group.add(sexwom);
Box box1=Box.createHorizontalBox();
box1.add(new JLabel("输入要修改信息的学号:",JLabel.LEFT)); box1.add(tnewnumber);
box1.add(startset);
Box box2=Box.createHorizontalBox();
box2.add(new JLabel("(新)姓名:",JLabel.LEFT));
box2.add(tnewname);
Box box3=Box.createHorizontalBox();
box3.add(new JLabel("(新)性别:",JLabel.LEFT));
box3.add(sexman);
box3.add(sexwom);
Box box4=Box.createHorizontalBox();
box4.add(new JLabel("(新)专业:",JLabel.LEFT));
box4.add(tnewspecial);
Box box5=Box.createHorizontalBox();
box5.add(new JLabel("(新)年级:",JLabel.LEFT));
box5.add(tnewgrade);
Box box6=Box.createHorizontalBox();
box6.add(new JLabel("(新)出生:",JLabel.LEFT));
box6.add(tnewborn);
Box box7=Box.createHorizontalBox();
box7.add(inset);
box7.add(reset);
Box boxH=Box.createVerticalBox();
boxH.add(box1);
boxH.add(box2);
boxH.add(box3);
boxH.add(box4);
boxH.add(box5);
boxH.add(box6);
boxH.add(box7);
boxH.setSize(300,300);
mods.add(boxH);
}
}
6.类Delete
(1)类成员变量(表12)
表12 成员变量
(2)方法(表13)
表13 方法
(3)实现代码
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Delete{
JTextField number,name,special,grade,born;
JButton delete;
JRadioButton boy,girl;
ButtonGroup group=null;
public Delete(JPanel delpanel)
{
delpanel.removeAll();
delpanel.setSize(300,300);
number=new JTextField(10);
//给文本框添加监听器,打回车显示要删除的学生信息number.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
String[] searched=new String[5];
String searchnumber=number.getText();
jdbcenroll searchs=new jdbcenroll();
searched=searchs.search(searchnumber);
name.setText(searched[0]);
if(searched[1].equals("男"))
{
boy.setSelected(true);
girl.setSelected(false);
}
else if(searched[1].equals("女"))
{
girl.setSelected(true);
boy.setSelected(false);
}
special.setText(searched[2]);
grade.setText(searched[3]);
born.setText(searched[4]);
}
});
name=new JTextField(10);
name.setEditable(false);
delete=new JButton("删除");
//删除学生基本信息事件监听器具
delete.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
String num=number.getText();
jdbcenroll del=new jdbcenroll();
del.delete(num);
}
});
boy=new JRadioButton("男");
girl=new JRadioButton("女");
group=new ButtonGroup();
group.add(boy);
group.add(girl);
special=new JTextField(10);
special.setEditable(false);
grade=new JTextField(10);
grade.setEditable(false);
born=new JTextField(10);
born.setEditable(false);
Box box1=Box.createHorizontalBox();
box1.add(new JLabel("输入要删除的学号:",JLabel.CENTER));
box1.add(number);
box1.add(delete);
Box box2=Box.createHorizontalBox();
box2.add(new JLabel("姓名:",JLabel.CENTER));
box2.add(name);
Box box3=Box.createHorizontalBox();
box3.add(new JLabel("性别:",JLabel.CENTER));
box3.add(boy);
box3.add(girl);
Box box4=Box.createHorizontalBox();
box4.add(new JLabel("专业:",JLabel.CENTER));
box4.add(special);
Box box5=Box.createHorizontalBox();
box5.add(new JLabel("年级:",JLabel.CENTER));
box5.add(grade);
Box box6=Box.createHorizontalBox();
box6.add(new JLabel("出生:",JLabel.CENTER));
box6.add(born);
Box boxH=Box.createVerticalBox();
boxH.add(box1);
boxH.add(box2);
boxH.add(box3);
boxH.add(box4);
boxH.add(box5);
boxH.add(box6);
boxH.setSize(300,300);
delpanel.add(boxH);
}
}
7.类jdbcenroll
(1)成员变量(表14)
表14
(2)方法(表15)
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
public class jdbcenroll
{
String
drivername="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String
dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=Students";
String userName="sa";
String userPwd="sa";
String namepwd="select * from loads where username=? and password=?";
String inputinformation="insert into stuinformation values(?,?,?,?,?,?)";
String selects="select * from stuinformation where stunumber=?";
String modifyinformation="update stuinformation set stuname=?,stusex=?,stuspecial=?,stugrade=?,stubirth=? where stunumber=?";
String searched="select * from stuinformation where stunumber=?";
String deleted="delete from stuinformation where stunumber=?";
String setload="update loads set username=?,password=?";
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
//判断用户名和密码是否正确
public boolean check(String name,String pwd)
{
try
{
Class.forName(drivername);
conn=DriverManager.getConnection(dbURL,userName,userPwd);
pstmt=conn.prepareStatement(namepwd);
pstmt.setString(1,name);
pstmt.setString(2,pwd);
rs=pstmt.executeQuery();
while(rs.next())
{
return true;
}
}
catch(Exception e){
e.printStackTrace();
}
finally
{
try
{
pstmt.close();
conn.close();
}catch(SQLException e)
{
e.printStackTrace();
}
}
return false;
}
//录入学生基本信息
public void input(String tnumber,String tname,String
tsex,String tspecial,String tgrade,String tborn )
{
try{
Class.forName(drivername);
conn=DriverManager.getConnection(dbURL,userName,userPwd);
//Date birth=(Date) new
java.text.SimpleDateFormat("yyy-MM-dd").parse(tborn);
if(tnumber.length()==0)
JOptionPane.showMessageDialog(null,"学号字段不能为空","警告",JOptionPane.WARNING_MESSAGE);
else
{
//检查输入的学号是否已经存在
pstmt=conn.prepareStatement(searched);
pstmt.setString(1,tnumber);
rs=pstmt.executeQuery();
if(rs.next())
{
JOptionPane.showMessageDialog(null,"该学号已存在!请输入新的学号!","警告",JOptionPane.WARNING_MESSAGE);
}
else
{
if(tname.length()==0 || tsex.length()==0 || tspecial.length()==0 || tgrade.length()==0)
JOptionPane.showMessageDialog(null,"选项不能为
空!","警告",JOptionPane.WARNING_MESSAGE);
else
{
pstmt=conn.prepareStatement(inputinformation);
pstmt.setString(1, tnumber);
pstmt.setString(2,tname);
pstmt.setString(3,tsex);
pstmt.setString(4,tspecial);
pstmt.setString(5,tgrade);
pstmt.setString(6,tborn);
pstmt.executeUpdate();
JOptionPane.showMessageDialog(null,"录入成功","成功",JOptionPane.WARNING_MESSAGE);
}
}
}
}catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
pstmt.close();
conn.close();
}catch(SQLException e)
{
e.printStackTrace();
}
}
}
//修改学生基本信息
public void modify(String tnumber,String tname,String tsex,String tspecial,String tgrade,String tborn)
{
try
{
Class.forName(drivername);
conn=DriverManager.getConnection(dbURL,userName,userPwd);
//Date birth=(Date) new
java.text.SimpleDateFormat("yyy-MM-dd").parse(tborn);
if(tnumber.length()==0){
JOptionPane.showMessageDialog(null,"学号字段不能不为空!","警告",JOptionPane.WARNING_MESSAGE);
}
else。

相关文档
最新文档