📄 indexint.java~2~
字号:
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;
JLabel lblCustID,lblNumber,lblUserName,lblUserSource,lblTel,lblMail,lblAddr;
GridBagLayout g;
GridBagConstraints c;
JTextField txtCustID, txtNumber, txtName, txtSource,txtTel,txtMail,txtAddr;
JButton btnQuery, btnInsert, btnDelete, btnUpdate;
BorderLayout bl;
FlowLayout fl;
GridLayout gl;
public IndexInt()
{
inframe=new JFrame("学生信息录入");
panel = new JPanel();
panelup = new JPanel();
paneldown = new JPanel();
panelcenter = new JPanel();
lblCustID = new JLabel("学生ID");
txtCustID = new JTextField(10);
lblNumber = new JLabel("学号:");
lblUserName = new JLabel("姓名:");
lblUserSource = new JLabel("分数:");
lblTel=new JLabel("电话号码:");
lblMail=new JLabel("邮箱地址:");
lblAddr=new JLabel("家庭住址:");
btnQuery = new JButton("查询");
btnInsert = new JButton("增加");
btnDelete = new JButton("删除");
btnUpdate = new JButton("修改");
txtNumber = new JTextField(10);
txtName = new JTextField(10);
txtSource = new JTextField(10);
txtTel=new JTextField(10);
txtMail= new JTextField(10);
txtAddr= new JTextField(10);
bl = new BorderLayout();
fl = new FlowLayout(FlowLayout.LEFT);
gl = new GridLayout(1, 3);
g = new GridBagLayout();
c = new GridBagConstraints();
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);
panel.add(paneldown, BorderLayout.SOUTH);
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(lblUserSource, c);
panelcenter.add(lblUserSource);
c.anchor = GridBagConstraints.WEST;
c.gridx = 1;
c.gridy = 7;
g.setConstraints(txtSource, c);
panelcenter.add(txtSource);
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 8;
g.setConstraints(lblTel, c);
panelcenter.add(lblTel);
c.anchor = GridBagConstraints.WEST;
c.gridx = 1;
c.gridy = 8;
g.setConstraints(txtTel, c);
panelcenter.add(txtTel);
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 9;
g.setConstraints(lblMail, c);
panelcenter.add(lblMail);
c.anchor = GridBagConstraints.WEST;
c.gridx = 1;
c.gridy = 9;
g.setConstraints(txtMail, c);
panelcenter.add(txtMail);
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 10;
g.setConstraints(lblAddr, c);
panelcenter.add(lblAddr);
c.anchor = GridBagConstraints.WEST;
c.gridx = 1;
c.gridy = 10;
g.setConstraints(txtAddr, c);
panelcenter.add(txtAddr);
inframe.getContentPane().add(panel);
Query q = new Query();
btnQuery.addActionListener(q);
btnInsert.addActionListener(q);
btnDelete.addActionListener(q);
btnUpdate.addActionListener(q);
}
class Query implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
Object obj = evt.getSource(); //获取引发事件的事件源
if (obj==btnQuery)
{
String strCustID=txtCustID.getText().trim();
/*
String strNumber=txtNumber.getText().trim();
String strName=txtName.getText().trim();
String strSource=txtSource.getText().trim();
String strTel=txtTel.getText().trim();
String strMail=txtMail.getText().trim();
String strAddr=txtAddr.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));
txtSource.setText(rs.getString(3));
txtTel.setText(rs.getString(4));
txtMail.setText(rs.getString(5));
txtAddr.setText(rs.getString(6));
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)
{
String strNumber=txtNumber.getText().trim();
String strName=txtName.getText().trim();
String strSource=txtSource.getText().trim();
String strTel=txtTel.getText().trim();
String strMail=txtMail.getText().trim();
String strAddr=txtAddr.getText().trim();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:SDB");
String str = "insert into Tabel values(?,?,?,?,?,?)";
PreparedStatement stmt = con.prepareStatement(str);
//传递参数
stmt.setString(1,strNumber);
stmt.setString(2,strName);
stmt.setString(3,strSource);
stmt.setString(4,strTel);
stmt.setString(5,strMail);
stmt.setString(6,strAddr);
ResultSet rs;
int i = stmt.executeUpdate();
//返回结果不为空时
if(i!=0)
{
JOptionPane.showMessageDialog(inframe, "添加记录成功!");
con.close();
txtNumber.setText("");
txtName.setText("");
txtSource.setText("");
txtTel.setText("");
txtMail.setText("");
txtAddr.setText("");
con.close();
}
else
{
JOptionPane.showMessageDialog(inframe, "添加记录失败!");
txtNumber.setText("");
txtName.setText("");
txtSource.setText("");
txtTel.setText("");
txtMail.setText("");
txtAddr.setText("");
}
}
catch(ClassNotFoundException e1)
{
JOptionPane.showMessageDialog(inframe,e1.getMessage());
}
catch(SQLException e2)
{
JOptionPane.showMessageDialog(inframe,e2.getMessage());
}
}
if(obj==btnDelete)
{
String strNumber=txtNumber.getText().trim();
String strName=txtName.getText().trim();
String strSource=txtSource.getText().trim();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:SDB");
String str = "delete from Tabel where Number=?";
PreparedStatement stmt = con.prepareStatement(str);
}
catch(ClassNotFoundException e1)
{
JOptionPane.showMessageDialog(inframe,e1.getMessage());
}
catch(SQLException e2)
{
JOptionPane.showMessageDialog(inframe,e2.getMessage());
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -