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

再来一个

点击添加按钮即可进行添加 主要由addframe.java实现

如图

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

源代码如下:

[cce]
///主运行类
/// main指主框架
/// setDefaultCloseOperation指让框架右上角的按钮都有效
/// show让框架能显示出来
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.Vector;
import javax.swing.*;
///这个是学生信息系统添加信息框架类
///通过九个文本域将不同信息的领导分别输出在其中。
///通过一个添加按钮,将数据添加到数据库中。
///在这里的布局是通过网格组布局管理器来设定的
///在回送到信息界面时将学生对象ss设定为静态。放置新建对象时,其值会改变。

class addframe extends JPanel
{
String stnametext;
String stzhuanyetext;
String stsextext;
String stsushenumbertext;
String stsushephonetext;
String stqqtext;
String stemailtext;
public final JTextField stusernameinput=new JTextField(10);
private static final long serialVersionUID = 1L;
static final int WIDTH=700;
static final int HEIGHT=400;

static JFrame studentaddframe;
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 addframe instance;

public static addframe getInstance()
{
if ( instance == null )
{
instance = new addframe();
return instance;
}
else
{    studentaddframe.setVisible(true);
return instance;}
}
private addframe()
{
studentaddframe=new JFrame();

studentaddframe.setTitle("添加学生信息");
//studentaddframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
studentaddframe.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;
studentaddframe.setLocation(x,y);
studentaddframe.setVisible(true);
studentaddframe.setResizable(false);
studentaddframe.add(this,BorderLayout.CENTER);
GridBagLayout lay=new GridBagLayout();
setLayout(lay);

JLabel name=new JLabel("学号");
JLabel code=new JLabel("姓名");
JLabel sexy=new JLabel("性别");
JLabel birthday=new JLabel("宿舍号码");
JLabel address=new JLabel("宿舍电话");
JLabel age=new JLabel("专业");
JLabel grade=new JLabel("qq号码");
JLabel major=new JLabel("电子邮件");

final JTextField stnameinput=new JTextField(10);
final JTextField stsexinput=new JTextField(10);
final JTextField stzhuanyeinput=new JTextField(10);
final JTextField stsushenumberinput=new JTextField(10);
final JTextField stsushephoneinput=new JTextField(10);
final JTextField stqqinput=new JTextField(10);
final JTextField stemailinput=new JTextField(10);

JLabel title=new JLabel("添加学生基本信息");
JButton additionbutton=new JButton("添加");
GridBagConstraints constraints=new GridBagConstraints();
constraints.fill=GridBagConstraints.NONE;
constraints.weightx=4;
constraints.weighty=6;

add(title,constraints,0,0,4,1);                 //使用网格组布局添加控件
add(name,constraints,0,1,1,1);
add(code,constraints,0,2,1,1);
add(sexy,constraints,0,3,1,1);
add(age,constraints,0,4,1,1);

add(stusernameinput,constraints,1,1,1,1);
add(stnameinput,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(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(additionbutton,constraints,0,5,4,1);
///将每个文本域中的信息赋值给变量,再将变量以值的形式存储到数据库的表中
///最后,再将这些值存储到学生对象中,并且这个学生对象是一个静态的对象
additionbutton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent Event)
{
try
{
String stusernametext=stusernameinput.getText();

stnametext=stnameinput.getText();

stzhuanyetext=stzhuanyeinput.getText();

stsextext=stsexinput.getText();

stsushenumbertext=stsushenumberinput.getText();

stsushephonetext=stsushephoneinput.getText();

stqqtext=stqqinput.getText();

stemailtext=stemailinput.getText();
DBconnection con= new DBconnection();

Statement st=con.getConnection();

if(stusernameinput.getText().length()!=0&&stnameinput.getText().length()!=0&&stzhuanyeinput.getText().length()!=0&&stsexinput.getText().length()!=0&&stsushenumberinput.getText().length()!=0&&stsushephoneinput.getText().length()!=0&&stqqinput.getText().length()!=0&&stemailinput.getText().length()!=0)
{

String qsql="select * from info where username='"+stusernameinput.getText()+"'";

ResultSet rs= st.executeQuery(qsql);

if(rs.next())
{JOptionPane.showMessageDialog(null,"学号已存在", "友情提示",JOptionPane.INFORMATION_MESSAGE);}

else
{
String sql="insert into info(username,name,sex,zhuanye,sushenumber,sushephone,qq,email) values('"+stusernametext+"','"+stnametext+"','"+stsextext+"','"+stzhuanyetext+"','"+stsushephonetext+"','"+stsushenumbertext+"','"+stqqtext+"','"+stemailtext+"')";

if(st.executeUpdate(sql)!=0)
{JOptionPane.showMessageDialog(null,"插入记录成功", "友情提示",JOptionPane.INFORMATION_MESSAGE);
studentaddframe.dispose(); }
}
rs.close();
}
else
{
JOptionPane.showMessageDialog(null,"请检查信息是否输入完整", "友情提示",JOptionPane.INFORMATION_MESSAGE);

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

}
});
}
}
[/cce]

发表评论

电子邮件地址不会被公开。 必填项已用*标注