import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.Vector;
class GUI implements ActionListener, ItemListener{
JFrame jf;
JButton binsert, bdelete, bsearch;
JTextField tfname, tfnum;
JLabel lname, lsex, lnum;
JRadioButton rbman, rbwoman;
ButtonGroup bgsex;
JTable table;
DefaultTableModel defaultTableModel;
JScrollPane sp;
Vector vdata, vcolumn;
Connection conn;
Statement stmt;
ResultSet rs;
public GUI() throws Exception {
SqlConnection();
jf = new JFrame("通讯录管理");
binsert = new JButton("添加");
bdelete = new JButton("删除");
bsearch = new JButton("搜索");
tfname = new JTextField(15);
tfnum = new JTextField(15);
lname = new JLabel("姓名");
lsex = new JLabel("性别");
lnum = new JLabel("电话号码");
rbman = new JRadioButton("男");
rbwoman = new JRadioButton("女");
bgsex = new ButtonGroup();
bgsex.add(rbman);
bgsex.add(rbwoman);
vdata = new Vector();
vcolumn = new Vector();
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
jf.setSize(600, 350);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setResizable(false);
jf.setLayout(new BorderLayout());
jp1.add(lname);
jp1.add(tfname);
jp1.add(lsex);
jp1.add(rbman);
jp1.add(rbwoman);
jp1.add(lnum);
jp1.add(tfnum);
jf.add(jp1, BorderLayout.NORTH);
jp2.add(binsert);
jp2.add(bdelete);
jp2.add(bsearch);
jf.add(jp2, BorderLayout.SOUTH);
vcolumn.add("姓名");
vcolumn.add("性别");
vcolumn.add("电话号码");
selectData("select * from exam");
defaultTableModel = new DefaultTableModel(vdata, vcolumn);
table = new JTable(defaultTableModel);
sp = new JScrollPane(table);
jf.add(sp);
binsert.addActionListener(this);
bdelete.addActionListener(this);
bsearch.addActionListener(this);
jf.setVisible(true);
}
public void selectData(String sql) throws SQLException {
rs = stmt.executeQuery(sql);
while (rs.next()) {
Vector vrow = new Vector();
vrow.add(rs.getString(1));
vrow.add(rs.getString(2));
vrow.add(rs.getInt(3));
vdata.add(vrow);
}
}
public void SqlConnection() throws Exception {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/exam?serverTimezone=UTC";
String name = "root";
String password = "123456";
conn = DriverManager.getConnection(url, name, password);
stmt = conn.createStatement();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == binsert) {
// 插入
String gender = null;
if (rbman.isSelected()) {
gender = rbman.getText();
} else {
gender = rbwoman.getText();
}
try {
PreparedStatement pstmt = conn.prepareStatement("insert into exam values(?,?,?)");
pstmt.setString(1, tfname.getText());
pstmt.setString(2, gender);
pstmt.setInt(3, Integer.parseInt(tfnum.getText()));
pstmt.executeUpdate();
} catch (SQLException e1) {
e1.printStackTrace();
}
Vector vector = new Vector();
vector.add(tfname.getText());
vector.add(gender);
vector.add(tfnum.getText());
defaultTableModel.addRow(vector);
table.setModel(defaultTableModel);
tfname.setText("");
tfnum.setText("");
rbman.setSelected(false);
rbwoman.setSelected(false);
} else if(e.getSource() == bdelete) {
//删除
int i = table.getSelectedRow();
try {
PreparedStatement pstmt = conn.prepareStatement("delete from exam where name=?");
pstmt.setString(1, table.getValueAt(i, 0) + "");
pstmt.executeUpdate();
}catch (SQLException e1) {
e1.printStackTrace();
}
defaultTableModel.removeRow(i);
} else if(e.getSource() == bsearch) {
//根据姓名查询
vdata.removeAllElements();
try {
if(tfname.getText().equals("")) {
selectData("select * from exam");
} else {
PreparedStatement pstmt = conn.prepareStatement("select * from exam where name=?");
pstmt.setString(1, tfname.getText());
rs = pstmt.executeQuery();
while (rs.next()) {
Vector vrow = new Vector();
vrow.add(rs.getString(1));
vrow.add(rs.getString(2));
vrow.add(rs.getInt(3));
vdata.add(vrow);
}
}
} catch(SQLException e1) {
e1.printStackTrace();
}
defaultTableModel = new DefaultTableModel(vdata, vcolumn);
table.setModel(defaultTableModel);
tfname.setText("");
tfnum.setText("");
rbman.setSelected(false);
rbwoman.setSelected(false);
}
}
public void itemStateChanged(ItemEvent e) {}
}
public class Main{
public static void main(String[] args){
GUI gui = new GUI();
}
}
[JAVA]通讯录管理系统代码
于 2023-12-12 10:14:48 首次发布