📄 stueditframe.java.bak
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.sql.*;
class StuEditFrame extends JFrame implements ActionListener
{
JFrame f=new JFrame("学生基本信息维护 ");
JLabel lbsid=new JLabel("请输入查询的学号"),
lbname=new JLabel("姓名 "),
lbsex=new JLabel("性别 "),
lbbirth=new JLabel("生日 "),
lbmajor=new JLabel("专业 "),
lbphone=new JLabel("电话 "),
lbjiguan=new JLabel("籍贯 ");
JTextField tfsid=new JTextField(10),
tfname=new JTextField(10),
tfsex=new JTextField(10),
tfbirth=new JTextField(10),
tfmajor=new JTextField(10),
tfphone=new JTextField(10),
tfjiguan=new JTextField(10);
JPanel p1=new JPanel(),
p2=new JPanel(),
p3=new JPanel(),
p4=new JPanel(),
p5=new JPanel(),
p6=new JPanel(),
p7=new JPanel(),
p8=new JPanel();
JButton btnserch=new JButton("查询 "),
btnedit=new JButton("修改 "),
btnclear=new JButton("清空 "),
btnadd=new JButton("添加 "),
btndelete=new JButton("删除 "),
btnexit=new JButton("退出 ");
public StuEditFrame()
{ Container cp=f.getContentPane();
cp.setLayout(new GridLayout(8,1));
p1.add(lbsid);p1.add(tfsid);p1.add(btnserch);
p2.add(lbname);p2.add(tfname);
p3.add(lbsex);p3.add(tfsex);
p4.add(lbbirth);p4.add(tfbirth);
p5.add(lbmajor);p5.add(tfmajor);
p6.add(lbphone);p6.add(tfphone);
p7.add(lbjiguan);p7.add(tfjiguan);
p8.add(btnedit);p8.add(btnclear);p8.add(btnadd);p8.add(btndelete);p8.add(btnexit);
cp.add(p1);cp.add(p2);cp.add(p3);cp.add(p4);cp.add(p5);cp.add(p6);cp.add(p7);cp.add(p8);
btnserch.addActionListener(this);
btnedit.addActionListener(this);
btnclear.addActionListener(this);
btnadd.addActionListener(this);
btndelete.addActionListener(this);
btnexit.addActionListener(this);
f.setSize(400,400);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==btnserch)
{ try
{
Connection con=DriverManager.getConnection("jdbc:odbc:Xsstudent");
Statement s=con.createStatement();
String id=tfsid.getText();
String strSQL = "select name,sex,birth,major,phone,jiguan from stuinfor where id= '"+id+"' ";
ResultSet rs = s.executeQuery(strSQL);
if(rs.next())
{
tfname.setText(rs.getString("name"));
tfsex.setText(rs.getString("sex"));
tfbirth.setText(rs.getString("birth"));
tfmajor.setText(rs.getString("major"));
tfphone.setText(rs.getString("phone"));
tfjiguan.setText(rs.getString("jiguan"));
}
else
{
JOptionPane.showMessageDialog(null,"对不起,没有您要查找的学生资料!,或者您的输入不符合查询的条件,请重新输入");
tfname.setText(null);
tfsex.setText(null);
tfbirth.setText(null);
tfmajor.setText(null);
tfphone.setText(null);
tfjiguan.setText(null);
tfsid.setText(null);
}
}
catch(Exception ee)
{ System.out.println("SQLException 错误信息 "+ee.getMessage() ); }
}
if (e.getSource()==btnedit)
{ try{
if((tfsid.getText().equals(""))||(tfname.getText().equals("")) ||(tfsex.getText().equals("")) ||(tfbirth.getText().equals("")) ||(tfmajor.getText()==null)||(tfphone.getText().equals(""))|| (tfjiguan.getText().equals("")))
{JOptionPane.showMessageDialog(null,"抱歉!您还有相关的信息没有输入。请输入相应的信息再点击修改按钮!"); }
else
{String strSQL="update stuinfor set name='"+tfname.getText()+"',sex='"+tfsex.getText()+"',birth='"+tfbirth.getText()+"',major='"+tfmajor.getText()+"',phone='"+tfphone.getText()+"',jiguan='"+tfjiguan.getText()+"'where id=?";
Connection con=DriverManager.getConnection("jdbc:odbc:Xsstudent");
PreparedStatement ps=con.prepareStatement(strSQL);
ps.setString(1,tfid.getText().trim());
ps.executeUpdate();
JOptionPane.showMessageDialog(null,"恭喜你,修改成功! ");
tfname.setText(null);
tfsex.setText(null);
tfbirth.setText(null);
tfmajor.setText(null);
tfphone.setText(null);
tfjiguan.setText(null);
tfsid.setText(null);
}
}
catch(Exception ee)
{System.out.println("修改信息出错:"+ee.getMessage()); }
}
if (e.getSource()==btnclear)
{
tfname.setText(null);
tfsex.setText(null);
tfbirth.setText(null);
tfmajor.setText(null);
tfphone.setText(null);
tfjiguan.setText(null);
tfsid.setText(null);
}
if (e.getSource()==btnadd)
{
try{
if((tfsid.getText().equals(""))||(tfname.getText().equals("")) ||(tfsex.getText().equals("")) ||(tfbirth.getText().equals("")) ||(tfmajor.getText()==null)||(tfphone.getText().equals(""))|| (tfjiguan.getText().equals("")))
{JOptionPane.showMessageDialog(null,"抱歉!您还有相关的信息没有输入。请输入相应的信息再点击添加按钮!"); }
else
{String strSQL="insert into stuinfor(id,name,sex,birth,major,phone,jiguan) values('"+tfsid.getText()+"','"+tfname.getText()+"','"+tfsex.getText()+"','"+tfbirth.getText()+"','"+tfmajor.getText()+"','"+tfphone.getText()+"','"+tfjiguan.getText()+"')";
Connection con=DriverManager.getConnection("jdbc:odbc:Xsstudent");
Statement s=con.createStatement();
s.executeUpdate(strSQL);
JOptionPane.showMessageDialog(null,"恭喜你,添加成功! ");
tfname.setText(null);
tfsex.setText(null);
tfbirth.setText(null);
tfmajor.setText(null);
tfphone.setText(null);
tfjiguan.setText(null);
tfsid.setText(null);
}
}
catch(Exception ee)
{System.out.println("添加信息出错:"+ee.getMessage()); }
}
if (e.getSource()==btndelete)
{
if(tfsid.getText().trim().equals("")||tfname.getText().trim().equals(""))
{ JOptionPane.showMessageDialog(this,"抱歉,学号和姓名不可以为空,无法删除");}
else{
try{
Connection con=DriverManager.getConnection("jdbc:odbc:Xsstudent");
Statement s=con.createStatement();
String id = tfsid.getText();
String strSQL = "delete * from stuinfor where id= '"+id+"' ";
s.executeUpdate(strSQL);
}
catch(Exception exx)
{JOptionPane.showMessageDialog(null,"抱歉,出错了");}
JOptionPane.showMessageDialog(null,"删除成功!");
tfname.setText(null);
tfsex.setText(null);
tfbirth.setText(null);
tfmajor.setText(null);
tfphone.setText(null);
tfjiguan.setText(null);
tfsid.setText(null);
}
}
if (e.getSource()==btnexit) { f.setVisible(false);}
}
public static void main(String args[])
{
String JDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String conURL="jdbc:odbc:Xsstudent";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception e)
{System.out.println("加载驱动失败");}
new StuEditFrame();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -