📄 indexint.java
字号:
package studentmessage;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class IndexInt
{
JFrame inframe;
JPanel panel, panelup, paneldown, panelcenter, panelcard;
JLabel lblCustID, lblNumber, lblUserName, lblSex, lblAge, lblTel, lblMail,
lblAddr, lblyw, lblsx, lblyy, lblzy;
JLabel lblpic;
GridBagLayout g;
GridBagConstraints c;
JTextField txtCustID, txtNumber, txtName, txtSex, txtAge, txtTel, txtMail,
txtAddr, txtyw, txtsx, txtyy, txtzy;
JButton btnQuery, btnInsert, btnDelete, btnUpdate, btnexit;
BorderLayout bl;
FlowLayout fl;
GridLayout gl;
Icon image;
public IndexInt() {
inframe = new JFrame("学生信息录入");
panel = new JPanel();
panelup = new JPanel();
paneldown = new JPanel();
panelcenter = new JPanel();
panelcard = new JPanel();
lblCustID = new JLabel("学生ID");
txtCustID = new JTextField(15);
lblNumber = new JLabel("学号:");
lblUserName = new JLabel("姓名:");
lblSex = new JLabel("性别:");
lblAge = new JLabel("年龄:");
lblTel = new JLabel("电话号码:");
lblMail = new JLabel("邮箱地址:");
lblAddr = new JLabel("家庭住址:");
lblyw = new JLabel("语文:");
lblsx = new JLabel("数学:");
lblyy = new JLabel("英语:");
lblzy = new JLabel("专业:");
lblpic = new JLabel();
btnQuery = new JButton("查询");
btnInsert = new JButton("增加");
btnDelete = new JButton("删除");
btnUpdate = new JButton("修改");
btnexit = new JButton("退出");
txtNumber = new JTextField(15);
txtName = new JTextField(15);
txtSex = new JTextField(15);
txtAge = new JTextField(15);
txtTel = new JTextField(15);
txtMail = new JTextField(15);
txtAddr = new JTextField(15);
txtyw = new JTextField(15);
txtsx = new JTextField(15);
txtyy = new JTextField(15);
txtzy = new JTextField(15);
bl = new BorderLayout();
fl = new FlowLayout(FlowLayout.LEFT);
gl = new GridLayout(1, 4);
g = new GridBagLayout();
c = new GridBagConstraints();
image = new ImageIcon("NO.gif");
lblpic = new JLabel(image);
panelcard.add(lblpic);
panelcenter.setLayout(g);
panel.setLayout(bl);
panelup.setLayout(fl);
paneldown.setLayout(gl);
panelup.add(lblCustID);
panelup.add(txtCustID);
panelup.add(btnQuery);
panel.add(panelup, BorderLayout.NORTH);
paneldown.add(btnInsert);
paneldown.add(btnDelete);
paneldown.add(btnUpdate);
paneldown.add(btnexit);
panel.add(paneldown, BorderLayout.SOUTH);
panel.add(panelcard, BorderLayout.EAST);
panel.add(panelcenter, BorderLayout.WEST);
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 5;
g.setConstraints(lblNumber, c);
panelcenter.add(lblNumber);
c.anchor = GridBagConstraints.WEST;
c.gridx = 1;
c.gridy = 5;
g.setConstraints(txtNumber, c);
panelcenter.add(txtNumber);
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 6;
g.setConstraints(lblUserName, c);
panelcenter.add(lblUserName);
c.anchor = GridBagConstraints.WEST;
c.gridx = 1;
c.gridy = 6;
g.setConstraints(txtName, c);
panelcenter.add(txtName);
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 7;
g.setConstraints(lblSex, c);
panelcenter.add(lblSex);
c.anchor = GridBagConstraints.WEST;
c.gridx = 1;
c.gridy = 7;
g.setConstraints(txtSex, c);
panelcenter.add(txtSex);
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 8;
g.setConstraints(lblAge, c);
panelcenter.add(lblAge);
c.anchor = GridBagConstraints.WEST;
c.gridx = 1;
c.gridy = 8;
g.setConstraints(txtAge, c);
panelcenter.add(txtAge);
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 9;
g.setConstraints(lblTel, c);
panelcenter.add(lblTel);
c.anchor = GridBagConstraints.WEST;
c.gridx = 1;
c.gridy = 9;
g.setConstraints(txtTel, c);
panelcenter.add(txtTel);
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 10;
g.setConstraints(lblMail, c);
panelcenter.add(lblMail);
c.anchor = GridBagConstraints.WEST;
c.gridx = 1;
c.gridy = 10;
g.setConstraints(txtMail, c);
panelcenter.add(txtMail);
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 11;
g.setConstraints(lblAddr, c);
panelcenter.add(lblAddr);
c.anchor = GridBagConstraints.WEST;
c.gridx = 1;
c.gridy = 11;
g.setConstraints(txtAddr, c);
panelcenter.add(txtAddr);
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 12;
g.setConstraints(lblyw, c);
panelcenter.add(lblyw);
c.anchor = GridBagConstraints.WEST;
c.gridx = 1;
c.gridy = 12;
g.setConstraints(txtyw, c);
panelcenter.add(txtyw);
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 13;
g.setConstraints(lblsx, c);
panelcenter.add(lblsx);
c.anchor = GridBagConstraints.WEST;
c.gridx = 1;
c.gridy = 13;
g.setConstraints(txtsx, c);
panelcenter.add(txtsx);
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 14;
g.setConstraints(lblyy, c);
panelcenter.add(lblyy);
c.anchor = GridBagConstraints.WEST;
c.gridx = 1;
c.gridy = 14;
g.setConstraints(txtyy, c);
panelcenter.add(txtyy);
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 15;
g.setConstraints(lblzy, c);
panelcenter.add(lblzy);
c.anchor = GridBagConstraints.WEST;
c.gridx = 1;
c.gridy = 15;
g.setConstraints(txtzy, c);
panelcenter.add(txtzy);
inframe.getContentPane().add(panel);
Query q = new Query();
btnQuery.addActionListener(q);
btnInsert.addActionListener(q);
btnDelete.addActionListener(q);
btnUpdate.addActionListener(q);
btnexit.addActionListener(q);
}
class Query
implements ActionListener {
public void actionPerformed(ActionEvent evt) {
Object obj = evt.getSource(); //获取引发事件的事件源
if (obj == btnQuery) {
if (txtCustID.getText().length() <= 0) {
JOptionPane.showMessageDialog(inframe, "请输入要查询的学生学号");
}
else {
String strCustID = txtCustID.getText().trim();
try { //加载驱动程序
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//连接数据库
Connection con = DriverManager.getConnection("jdbc:odbc:SDB");
//执行SQL语句
String sql = "select * from Tabel where 学号=?";
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString(1, strCustID);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
txtNumber.setText(rs.getString(1));
txtName.setText(rs.getString(2));
txtSex.setText(rs.getString(3));
txtAge.setText(rs.getString(4));
txtTel.setText(rs.getString(5));
txtMail.setText(rs.getString(6));
txtAddr.setText(rs.getString(7));
txtyw.setText(rs.getString(8));
txtsx.setText(rs.getString(9));
txtyy.setText(rs.getString(10));
txtzy.setText(rs.getString(11));
txtCustID.setText("");
con.close();
}
else {
JOptionPane.showMessageDialog(inframe, "没有您查询的学生信息!");
txtCustID.setText("");
}
}
catch (ClassNotFoundException e1) {
JOptionPane.showMessageDialog(inframe, e1.getMessage());
}
catch (SQLException e2) {
JOptionPane.showMessageDialog(inframe, e2.getMessage());
}
}
}
if (obj == btnInsert) {
Add add = new Add();
add.addframe.setBounds(350, 200, 250, 325);
add.addframe.setVisible(true);
add.addframe.show();
inframe.setVisible(false);
}
if (obj == btnDelete) {
Delete d = new Delete();
d.delframe.setVisible(true);
d.delframe.setBounds(350, 200, 350, 150);
d.delframe.show();
inframe.setVisible(false);
}
if (obj == btnUpdate) {
String strNumber = txtNumber.getText().trim();
String strName = txtName.getText().trim();
if (txtNumber.getText().length() <= 0 ||txtName.getText().length() <= 0)
JOptionPane.showMessageDialog(null, "学号、姓名均不能为空!");
else {
String strSex = txtSex.getText().trim();
String s1 = "0";
String s2 = "1";
if (!strSex.equals(s1) && !strSex.equals(s2)) {
JOptionPane.showMessageDialog(null, "请用0代表男、1代表女!");
}
else {
if (strSex.equals(s1)) strSex = "男";
if (strSex.equals(s2)) strSex = "女";
String strAge = txtAge.getText().trim();
String strTel = txtTel.getText().trim();
String strMail = txtMail.getText().trim();
String strAddr = txtAddr.getText().trim();
String stryw = txtyw.getText().trim();
String strsx = txtsx.getText().trim();
String stryy = txtyy.getText().trim();
String strzy = txtzy.getText().trim();
if (txtSex.getText().length() > 1
|| txtAge.getText().length() > 3
|| txtTel.getText().length() >= 12
|| txtyw.getText().length() > 3
|| txtsx.getText().length() > 3
|| txtyy.getText().length() > 3
|| txtzy.getText().length() > 3)
JOptionPane.showMessageDialog(null, "请检查输入格式!");
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:SDB");
String sql = "Update Tabel Set 姓名=?,性别=?,年龄=?,电话号码=?,Email=?,家庭住址=?,语文=?,数学=?,英语=?,专业=? where 学号=?";
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString(1, strName);
stmt.setString(2, strSex);
stmt.setString(3, strAge);
stmt.setString(4, strTel);
stmt.setString(5, strMail);
stmt.setString(6, strAddr);
stmt.setString(7, stryw);
stmt.setString(8, strsx);
stmt.setString(9, stryy);
stmt.setString(10, strzy);
stmt.setString(11, strNumber);
ResultSet rs;
int i = stmt.executeUpdate();
if (i != 0) {
if (txtNumber.getText().length() <= 0
|| txtName.getText().length() <= 0
|| txtSex.getText().length() > 1
|| txtAge.getText().length() >= 3
|| txtTel.getText().length() >= 12
|| txtyw.getText().length() > 3
|| txtsx.getText().length() > 3
|| txtyy.getText().length() > 3
|| txtzy.getText().length() > 3)
JOptionPane.showMessageDialog(inframe, "格式不符!");
else {
JOptionPane.showMessageDialog(inframe, "修改记录成功!");
con.close();
txtNumber.setText("");
txtName.setText("");
txtSex.setText("");
txtAge.setText("");
txtTel.setText("");
txtMail.setText("");
txtAddr.setText("");
txtyw.setText("");
txtsx.setText("");
txtyy.setText("");
txtzy.setText("");
con.close();
}
}
else {
JOptionPane.showMessageDialog(inframe, "修改记录失败!");
txtNumber.setText("");
txtName.setText("");
txtSex.setText("");
txtAge.setText("");
txtTel.setText("");
txtMail.setText("");
txtAddr.setText("");
txtyw.setText("");
txtsx.setText("");
txtyy.setText("");
txtzy.setText("");
}
}
catch (ClassNotFoundException e1) {
JOptionPane.showMessageDialog(inframe, e1.getMessage());
}
catch (SQLException e2) {
JOptionPane.showMessageDialog(inframe, e2.getMessage());
}
}
}
}
if (obj == btnexit) {
System.exit(0);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -