Java 大作业之学生信息管理系统—The Five

目录

还有

点击学生基本信息管理即可进入管理窗口 由studentinfo.java实现

如图

实现代码 如下:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import java.sql.*;
///这是学生类信息系统的框架类
/// studentinfoframe是顶层窗口框架。
///通过八个文本域控件来分别可以显示不同班级、不同系的学生信息。
///三个按钮控件分别产生三个不同的动作事件简史事件,分别是添加,删除和更新。Addition是添加。Delete是删除,update是更新
///在布局方面有两个布局管理器,一个是lay即网格组布局。一个是jp是GridLayout布局。
///通过不同的SQL语句进入不同的班级和系的管理系统。
///使用将数据库中的数据先导入到vec中。然后在将vec中的数据导入学生类中。最后再将数据输出
class studentinfo extends JPanel
{
private static final long serialVersionUID = 1L;
static final int WIDTH=700;
static final int HEIGHT=400;

final JTextField stnameinput;
final JTextField stusernameinput;
final JTextField stsexinput;
final JTextField stzhuanyeinput;
final JTextField stsushenumberinput;
final JTextField stsushephoneinput;
final JTextField stqqinput;
final JTextField stemailinput;

final     JLabel name;
final     JLabel code;
final     JLabel sexy;
final     JLabel birthday;
final     JLabel address;
final      JLabel age;
final     JLabel grade;
final     JLabel major;
final     JLabel title;

final   JButton addition;
final   JButton delete;
final   JButton update;
final   JButton bereturn;
final   JButton qxingming;
final   JButton qxuehao;

static JFrame studentinfoframe;

//storesystem store=new storesystem();

// Connection con=store.getConnection();

//addframe af=new addframe();
//af.setVisible(false);

public void add(Component c,GridBagConstraints constraints,int x,int y,int w,int h)
{
constraints.gridx=x;
constraints.gridy=y;
constraints.gridwidth=w;
constraints.gridheight=h;
add(c,constraints);
}

private static studentinfo instance;

public static studentinfo getInstance(String str,String sql)
{
if ( instance == null )
{
instance = new studentinfo(str,sql);
return instance;
}
else
{    studentinfoframe.setVisible(true);
return instance;}
}
private studentinfo(String str,String sql)
{
studentinfoframe=new JFrame();
studentinfoframe.setTitle(str);
// studentinfoframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
studentinfoframe.setSize(WIDTH,HEIGHT);
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension screenSize=kit.getScreenSize();
int width=screenSize.width;
int height=screenSize.height;
int x=(width-WIDTH)/2;
int y=(height-HEIGHT)/2;
studentinfoframe.setLocation(x,y);
studentinfoframe.setVisible(true);
studentinfoframe.setResizable(false);
studentinfoframe.add(this,BorderLayout.CENTER);
GridBagLayout lay=new GridBagLayout();
setLayout(lay);
name=new JLabel("姓名");
code=new JLabel("学号");
sexy=new JLabel("性别");
birthday=new JLabel("宿舍号码");
address=new JLabel("宿舍电话");
age=new JLabel("专业");
grade=new JLabel("qq号码");
major=new JLabel("电子邮箱");
title=new JLabel(str);

stnameinput=new JTextField(10);

stusernameinput=new JTextField(10);
//stusernameinput.setEditable(false);

stsexinput=new JTextField(10);

stzhuanyeinput=new JTextField(10);

stsushenumberinput=new JTextField(10);

stsushephoneinput=new JTextField(10);

stqqinput=new JTextField(10);

stemailinput=new JTextField(10);

addition=new JButton("添加");
delete=new JButton("删除");
update=new JButton("修改");
bereturn=new JButton("查询");

qxingming=new JButton("按姓名查询");
qxuehao=new JButton("按学号查询");

sexy.setVisible(false);//隐藏jlabel
birthday.setVisible(false);
address.setVisible(false);
age.setVisible(false);
grade.setVisible(false);
major.setVisible(false);

stsexinput.setVisible(false);//隐藏jtextfield

stzhuanyeinput.setVisible(false);

stsushenumberinput.setVisible(false);

stsushephoneinput.setVisible(false);

stqqinput.setVisible(false);

stemailinput.setVisible(false);

addition.setVisible(false);////隐藏jbutton
delete.setVisible(false);
update.setVisible(false);
bereturn.setVisible(false);

GridBagConstraints constraints=new GridBagConstraints();
constraints.fill=GridBagConstraints.NONE;
constraints.weightx=4;
constraints.weighty=6;

//使用网格组布局添加控件

add(title,constraints,0,0,4,1); //第0列第0行

add(name,constraints,0,1,1,1); //第0列第1行
add(code,constraints,0,2,1,1); //第0列第2行
add(sexy,constraints,0,3,1,1); //第0列第3行
add(age,constraints,0,4,1,1);   //第0列第4行

add(stnameinput,constraints,1,1,1,1);
add(stusernameinput,constraints,1,2,1,1);
add(stsexinput,constraints,1,3,1,1);
add(stzhuanyeinput,constraints,1,4,1,1);

add(birthday,constraints,2,1,1,1);
add(address,constraints,2,2,1,1);
add(grade,constraints,2,3,1,1);
add(major,constraints,2,4,1,1);

add(qxingming,constraints,2,1,1,1);
add(qxuehao,constraints,2,2,1,1);

add(stsushenumberinput,constraints,3,1,1,1);
add(stsushephoneinput,constraints,3,2,1,1);
add(stqqinput,constraints,3,3,1,1);
add(stemailinput,constraints,3,4,1,1);

add(bereturn,constraints,0,5,1,1);
add(update,constraints,1,5,1,1);
add(delete,constraints,2,5,1,1);
add(addition,constraints,3,5,1,1);

qxingming.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent Event)
{
String sql ="select * from info where name='"+stnameinput.getText()+"'";
try
{ DBconnection con= new DBconnection();
Statement st=con.getConnection();

ResultSet rs=st.executeQuery(sql);
if(rs.next())

{

stusernameinput.setText(rs.getString(2));
stnameinput.setText(rs.getString(3));
stsexinput.setText(rs.getString(4));

stzhuanyeinput.setText(rs.getString(5));

stsushenumberinput.setText(rs.getString(6));

stsushephoneinput.setText(rs.getString(7));

stqqinput.setText(rs.getString(8));

stemailinput.setText(rs.getString(9));

stusernameinput.setEditable(false);

sexy.setVisible(true);
birthday.setVisible(true);
address.setVisible(true);
age.setVisible(true);
grade.setVisible(true);
major.setVisible(true);

stsexinput.setVisible(true);

stzhuanyeinput.setVisible(true);

stsushenumberinput.setVisible(true);

stsushephoneinput.setVisible(true);

stqqinput.setVisible(true);

stemailinput.setVisible(true);

addition.setVisible(true);
delete.setVisible(true);
update.setVisible(true);
bereturn.setVisible(true);

qxingming.setVisible(false);
qxuehao.setVisible(false);

rs.close();
st.close();

}
else //错误提示

{
JOptionPane.showMessageDialog(null,"你输入的姓名不存在!", "友情提示",JOptionPane.INFORMATION_MESSAGE);
stusernameinput.setText("");
stnameinput.setText("");
}
}
catch(SQLException e) {JOptionPane.showMessageDialog(null,e.getMessage(), "对不起,出错啦",JOptionPane.ERROR_MESSAGE); stusernameinput.setText("");
stnameinput.setText("");}

}
});

qxuehao.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent Event)
{
String sql ="select * from info where username='"+stusernameinput.getText()+"'";
try
{ DBconnection con= new DBconnection();
Statement st=con.getConnection();

ResultSet rs=st.executeQuery(sql);
if(rs.next())

{

stusernameinput.setText(rs.getString(2));
stnameinput.setText(rs.getString(3));
stsexinput.setText(rs.getString(4));

stzhuanyeinput.setText(rs.getString(5));

stsushenumberinput.setText(rs.getString(6));

stsushephoneinput.setText(rs.getString(7));

stqqinput.setText(rs.getString(8));

stemailinput.setText(rs.getString(9));

stusernameinput.setEditable(false);
sexy.setVisible(true);
birthday.setVisible(true);
address.setVisible(true);
age.setVisible(true);
grade.setVisible(true);
major.setVisible(true);

stsexinput.setVisible(true);

stzhuanyeinput.setVisible(true);

stsushenumberinput.setVisible(true);

stsushephoneinput.setVisible(true);

stqqinput.setVisible(true);

stemailinput.setVisible(true);

addition.setVisible(true);
delete.setVisible(true);
update.setVisible(true);
bereturn.setVisible(true);

qxingming.setVisible(false);
qxuehao.setVisible(false);

rs.close();
st.close();
}
else //错误提示

{
JOptionPane.showMessageDialog(null,"你输入的学号不存在!", "友情提示",JOptionPane.INFORMATION_MESSAGE);
stusernameinput.setText("");
stnameinput.setText("");
}
}
catch(SQLException e) {JOptionPane.showMessageDialog(null,e.getMessage(), "对不起,出错啦",JOptionPane.ERROR_MESSAGE); stusernameinput.setText("");
stnameinput.setText("");}
}
});

///查询主菜单
bereturn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent Event)
{

qxingming.setVisible(true);
qxuehao.setVisible(true);

stusernameinput.setEditable(true);
stusernameinput.setText("");
stnameinput.setText("");

sexy.setVisible(false);//隐藏jlabel
birthday.setVisible(false);
address.setVisible(false);
age.setVisible(false);
grade.setVisible(false);
major.setVisible(false);

stsexinput.setVisible(false);//隐藏jtextfield

stzhuanyeinput.setVisible(false);

stsushenumberinput.setVisible(false);

stsushephoneinput.setVisible(false);

stqqinput.setVisible(false);

stemailinput.setVisible(false);

addition.setVisible(false);////隐藏jbutton
delete.setVisible(false);
update.setVisible(false);
bereturn.setVisible(false);

}
});
///进入添加主界面
addition.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent Event)
{
addframe    addfr=addframe.getInstance();
//addframe addfr=new addframe();
// addfr.setVisible(true);
}
});
///修改按钮,使得从数据库表中,将数据提取显示到界面中
update.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent Event)
{

String sql ="update info set name='"+stnameinput.getText()+"',sex='"+stsexinput.getText()+"',zhuanye='"+stzhuanyeinput.getText()+"',sushenumber='"+stsushenumberinput.getText()+"',sushephone='"+stsushephoneinput.getText()+"',qq='"+stqqinput.getText()+"',email='"+stemailinput.getText()+"'"+"where username='"+stusernameinput.getText()+"'";
try

{
DBconnection con= new DBconnection();

Statement st=con.getConnection();

if(st.executeUpdate(sql)!=0)
{
JOptionPane.showMessageDialog(null,"修改记录成功", "友情提示",JOptionPane.INFORMATION_MESSAGE);
}
st.close();

}
catch(SQLException e) {JOptionPane.showMessageDialog(null,e.getMessage(), "对不起,出错啦",JOptionPane.ERROR_MESSAGE);}

String qsql ="select * from info where username='"+stusernameinput.getText()+"'";
try
{ DBconnection con= new DBconnection();
Statement st=con.getConnection();

ResultSet rs=st.executeQuery(qsql);
if(rs.next())

{

stusernameinput.setText(rs.getString(2));
stnameinput.setText(rs.getString(3));
stsexinput.setText(rs.getString(4));

stzhuanyeinput.setText(rs.getString(5));

stsushenumberinput.setText(rs.getString(6));

stsushephoneinput.setText(rs.getString(7));

stqqinput.setText(rs.getString(8));

stemailinput.setText(rs.getString(9));

rs.close();
st.close();
}
else //错误提示

{
JOptionPane.showMessageDialog(null,"读取数据出错啦", "友情提示",JOptionPane.INFORMATION_MESSAGE);

}
}
catch(SQLException e) {JOptionPane.showMessageDialog(null,e.getMessage(), "对不起,出错啦",JOptionPane.ERROR_MESSAGE);}

}
});
///删除数据,且更新到数据库中
delete.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent Event)
{

String sql ="delete * from info where username='"+stusernameinput.getText()+"'";
try

{
DBconnection con= new DBconnection();

Statement st=con.getConnection();

//ResultSet rs=st.executeQuery(sql);

if(st.executeUpdate(sql)!=0)
{ JOptionPane.showMessageDialog(null,"删除记录成功", "友情提示",JOptionPane.INFORMATION_MESSAGE);
}
st.close();
}
catch(SQLException e) {JOptionPane.showMessageDialog(null,e.getMessage(), "对不起,出错啦",JOptionPane.ERROR_MESSAGE);}

stusernameinput.setText("");
stnameinput.setText("");
stsexinput.setText("");

stzhuanyeinput.setText("");

stsushenumberinput.setText("");

stsushephoneinput.setText("");

stqqinput.setText("");

stemailinput.setText("");

}
});

}
}