package com.itheima.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JFrame;
import com.mysql.jdbc.PreparedStatement;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Frame;
import java.awt.Label;
import java.awt.List;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.cert.PKIXRevocationChecker.Option;
import javax.print.attribute.standard.JobPriority;
import javax.sound.midi.Soundbank;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.WindowConstants;
import com.mysql.jdbc.PreparedStatement;
public class Example03 {
public static final String url = "jdbc:mysql://127.0.0.1:3308/Asql";// 声明数据库test的url
public static final String name = "com.mysql.jdbc.Driver";
public static final String user = "root";// 数据库用户名
public static final String password = "1920170241";// 数据库密码
public void show() {
JFrame jFrame = new JFrame("简易电话簿");
jFrame.setSize(new Dimension(500, 400));
jFrame.setLocationRelativeTo(null);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JTabbedPane jTabbedPane = new JTabbedPane();
JPanel jPanel = new JPanel();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JPanel jPanel3 = new JPanel();
JPanel jPanel4 = new JPanel();
JPanel jPanel5 = new JPanel();
JPanel jPanel6 = new JPanel();
JPanel jPanel7 = new JPanel();
JPanel jPanel8 = new JPanel();
JPanel jPanel9 = new JPanel();
JPanel jPanel0 = new JPanel();
JLabel jLabel = new JLabel("用户名称");
JLabel jLabel2 = new JLabel("电话号码");
JLabel jLabel3 = new JLabel("邮箱地址");
JLabel jLabel4 = new JLabel("用户名称");
JLabel jLabel5 = new JLabel("电话号码");
JLabel jLabel6 = new JLabel("<html>输入用户名称就可删除该用户资料<br>需要输入全部信息才可添加</html>");
JLabel jLabel7 = new JLabel("只需要输入用户名称或电话号码就可以查询");
JButton jButton = new JButton("添加");
JButton jButton2 = new JButton("删除");
JButton jButton3 = new JButton("确定");
JButton jButton4 = new JButton("确定");
JButton jButton5 = new JButton("查询所有");
JButton jButton6 = new JButton("修改");
JTextField jTextField = new JTextField();
JTextField jTextField2 = new JTextField();
JTextField jTextField3 = new JTextField();
JTextField jTextField4 = new JTextField();
JTextField jTextField5 = new JTextField();
Font font = new Font("宋体", Font.BOLD, 20);
Dimension dimension = new Dimension(200, 35);
Dimension dimension2 = new Dimension(80, 35);
jLabel.setFont(font);
jLabel2.setFont(font);
jLabel3.setFont(font);
jLabel4.setFont(font);
jLabel5.setFont(font);
jLabel6.setFont(font);
jLabel7.setFont(font);
jTextField.setPreferredSize(dimension);
jTextField2.setPreferredSize(dimension);
jTextField3.setPreferredSize(dimension);
jTextField4.setPreferredSize(dimension);
jTextField5.setPreferredSize(dimension);
jTextField.setFont(font);
jTextField2.setFont(font);
jTextField3.setFont(font);
jTextField4.setFont(font);
jTextField5.setFont(font);
jButton.setPreferredSize(dimension2);
jButton2.setPreferredSize(dimension2);
jButton3.setPreferredSize(dimension2);
jButton4.setPreferredSize(dimension2);
jButton6.setPreferredSize(dimension2);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e1) {
// TODO Auto-generated method stub
if ("".equals(jTextField.getText()) != true && "".equals(jTextField2.getText()) != true
&& "".equals(jTextField3.getText()) != true) {
try {
Class.forName(name);// 加载数据库驱动
// 建立数据库连接,获得连接对象conn
Connection conn = DriverManager.getConnection(url, user, password);
ResultSet resultSet = null;
// 生成一条SQL语句
String sql = "select * from first_user";
PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql);// 创建一个Statement对象
resultSet = ps.executeQuery();// 执行sql语句
String[] arr1 = new String[99];
String[] arr2 = new String[99];
String[] arr3 = new String[99];
int i=0;
Boolean open = true;
while (resultSet.next()) {
String name01 = resultSet.getString("name");
String telephoneNum01 = resultSet.getString("telephoneNum");
String email01 = resultSet.getString("email");
arr1[i]=name01;
arr2[i]=telephoneNum01;
arr3[i]=email01;
i++;
}
for(int x=0;x<i;x++) {
if(arr1[x].equals(jTextField.getText())) {
JOptionPane.showMessageDialog(
jFrame,
"该用户名已经存在!请重新输入!",
"操作结果",
JOptionPane.WARNING_MESSAGE
);
jTextField.setText(null);
open = false;
}
}
for(int x=0;x<i;x++) {
if(arr2[x].equals(jTextField2.getText())) {
JOptionPane.showMessageDialog(
jFrame,
"该号码已经存在!请重新输入!",
"操作结果",
JOptionPane.WARNING_MESSAGE
);
jTextField2.setText(null);
open = false;
}
}
for(int x=0;x<i;x++) {
if(arr3[x].equals(jTextField3.getText())) {
JOptionPane.showMessageDialog(
jFrame,
"该邮箱地址已经存在!请重新输入!",
"操作结果",
JOptionPane.WARNING_MESSAGE
);
jTextField3.setText(null);
open = false;
}
}
if(open == true) {
add(jTextField.getText(), jTextField2.getText(), jTextField3.getText());
jTextField.setText(null);
jTextField2.setText(null);
jTextField3.setText(null);
}
conn.close();
// 关闭数据库连接对
} catch (ClassNotFoundException e4) {
// TODO Auto-generated catch block
e4.printStackTrace();
} catch (SQLException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(jFrame, "输入为空!", "操作结果", JOptionPane.WARNING_MESSAGE, null);
jTextField.setText(null);
jTextField2.setText(null);
jTextField3.setText(null);
}
}
});
jButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if ("".equals(jTextField.getText()) != true) {
try {
Class.forName(name);// 加载数据库驱动
Connection conn = DriverManager.getConnection(url, user, password);
ResultSet resultSet = null;
// 生成一条SQL语句
String sql = "select * from first_user where name = ?";
PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql);// 创建一个Statement对象
ps.setString(1, jTextField.getText());// 为sql语句中第一个问号赋值
resultSet = ps.executeQuery();// 执行sql语句
boolean open = false;
while (resultSet.next()) {
String name = resultSet.getString("name");
String telephoneNum = resultSet.getString("telephoneNum");
String email = resultSet.getString("email");
String name01 = jTextField.getText();
if (name01.equals(name)) {
show01(jTextField.getText(), jFrame);
open = true;
}
}
if (open == false) {
JOptionPane.showMessageDialog(jFrame, "该用户不存在!请重新输入!");
}
conn.close();// 关闭数据库连接对
jTextField.setText(null);
} catch (ClassNotFoundException e4) {
// TODO Auto-generated catch block
e4.printStackTrace();
} catch (SQLException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(jFrame, "输入错误", "操作结果", JOptionPane.WARNING_MESSAGE, null);
jTextField.setText(null);
}
}
});
jButton3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if ("".equals(jTextField4.getText()) != true) {
try {
Class.forName(name);// 加载数据库驱动
// 建立数据库连接,获得连接对象conn
Connection conn = DriverManager.getConnection(url, user, password);
ResultSet resultSet = null;
// 生成一条SQL语句
String sql = "select * from first_user where name = ?";
PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql);// 创建一个Statement对象
ps.setString(1, jTextField4.getText());// 为sql语句中第一个问号赋值
resultSet = ps.executeQuery();// 执行sql语句
boolean open = false;
while (resultSet.next()) {
String name = resultSet.getString("name");
String telephoneNum = resultSet.getString("telephoneNum");
String email = resultSet.getString("email");
String name01 = jTextField4.getText();
if (name01.equals(name)) {
JOptionPane.showMessageDialog(jFrame,
"姓名:" + name + "\n\r" + "电话号码:" + telephoneNum + "\n\r" + "邮箱地址:" + email,
"操作结果", JOptionPane.PLAIN_MESSAGE, null);
open = true;
}
}
if (open == false) {
JOptionPane.showMessageDialog(jFrame, "该用户不存在!请重新输入!");
}
conn.close();// 关闭数据库连接对
jTextField4.setText(null);
} catch (ClassNotFoundException e4) {
// TODO Auto-generated catch block
e4.printStackTrace();
} catch (SQLException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(jFrame, "输入错误", "操作结果", JOptionPane.WARNING_MESSAGE, null);
}
}
});
jButton4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if ("".equals(jTextField5.getText()) != true) {
try {
Class.forName(name);// 加载数据库驱动
// 建立数据库连接,获得连接对象conn
Connection conn = DriverManager.getConnection(url, user, password);
ResultSet resultSet = null;
// 生成一条SQL语句
String sql = "select * from first_user where telephoneNum = ?";
PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql);// 创建一个Statement对象
ps.setString(1, jTextField5.getText());// 为sql语句中第一个问号赋值
resultSet = ps.executeQuery();// 执行sql语句
boolean open = false;
while (resultSet.next()) {
String name = resultSet.getString("name");
String telephoneNum = resultSet.getString("telephoneNum");
String email = resultSet.getString("email");
String telephoneNumString = jTextField5.getText();
if (telephoneNum.equals(telephoneNumString)) {
JOptionPane.showMessageDialog(jFrame,
"姓名:" + name + "\n\r" + "电话号码:" + telephoneNum + "\n\r" + "邮箱地址:" + email,
"操作结果", JOptionPane.PLAIN_MESSAGE, null);
open = true;
}
}
if (open == false) {
JOptionPane.showMessageDialog(jFrame, "该用户不存在!请重新输入!");
}
conn.close();// 关闭数据库连接对象
jTextField5.setText(null);
} catch (ClassNotFoundException e4) {
// TODO Auto-generated catch block
e4.printStackTrace();
} catch (SQLException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(jFrame, "输入错误!", "操作结果", JOptionPane.WARNING_MESSAGE, null);
}
}
});
jButton5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
Class.forName(name);// 加载数据库驱动
// 建立数据库连接,获得连接对象conn
Connection conn = DriverManager.getConnection(url, user, password);
ResultSet resultSet = null;
// 生成一条SQL语句
String sql = "select * from first_user";
PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql);// 创建一个Statement对象
resultSet = ps.executeQuery();// 执行sql语句
JScrollPane scrollPane = new JScrollPane();
String[] array = new String[999];
int i = 1;
array[0] = "姓名 " + "电话号码 " + "邮箱地址 ";
while (resultSet.next()) {
String name = resultSet.getString("name");
String telephoneNum = resultSet.getString("telephoneNum");
String email = resultSet.getString("email");
array[i] = name + " " + telephoneNum + " " + email;
i++;
}
show(array, i, jFrame);
conn.close();// 关闭数据库连接对
} catch (ClassNotFoundException e4) {
// TODO Auto-generated catch block
e4.printStackTrace();
} catch (SQLException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
}
});
jButton6.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
Class.forName(name);
Connection connection = DriverManager.getConnection(url,user,password);
ResultSet resultSet = null;
String sql = "select * from first_user where name=?";
PreparedStatement statement = (PreparedStatement)connection.prepareStatement(sql);
statement.setString(1, jTextField.getText());
resultSet = statement.executeQuery();
boolean open = true;
while(resultSet.next()) {
String name = resultSet.getString("name");
if((name.equals(jTextField.getText()))==true) {
update(jTextField.getText(),jTextField2.getText(),jTextField3.getText());
open = false;
jTextField.setText(null);
jTextField2.setText(null);
jTextField3.setText(null);
}
}
if(open==true) {
JOptionPane.showMessageDialog(jFrame, "該用戶不存在!",null, JOptionPane.WARNING_MESSAGE);
jTextField.setText(null);
jTextField2.setText(null);
jTextField3.setText(null);
}
connection.close();
}catch (SQLException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
jPanel.add(jLabel);
jPanel.add(jTextField);
jPanel1.add(jLabel2);
jPanel1.add(jTextField2);
jPanel2.add(jLabel3);
jPanel2.add(jTextField3);
jPanel3.add(jButton);
jPanel3.add(jButton2);
jPanel3.add(jButton6);
jPanel7.add(jLabel6);
Box vBox = Box.createVerticalBox();
vBox.add(jPanel7);
vBox.add(jPanel);
vBox.add(jPanel1);
vBox.add(jPanel2);
vBox.add(jPanel3);
jTabbedPane.addTab("添加/删除/修改", vBox);
jPanel4.add(jLabel4);
jPanel4.add(jTextField4);
jPanel4.add(jButton3);
jPanel5.add(jLabel5);
jPanel5.add(jTextField5);
jPanel5.add(jButton4);
jPanel8.add(jLabel7);
JPanel jPanel11 = new JPanel();
jPanel11.add(jButton5);
Box vBox2 = Box.createVerticalBox();
Component vStuct01 = Box.createVerticalStrut(10);
Component vStuct02 = Box.createVerticalStrut(10);
jPanel0.add(vStuct02);
vBox2.add(jPanel9);
vBox2.add(jPanel8);
vBox2.add(jPanel4);
vBox2.add(jPanel5);
vBox2.add(jPanel0);
vBox2.add(jPanel11);
jTabbedPane.addTab("查询", vBox2);
jFrame.add(jTabbedPane);
jFrame.setVisible(true);
}
public boolean equals(Object anObject) {
String value = String.valueOf(anObject);
String value1 = String.valueOf(this);
boolean result = false;
if(value==value1) {
result=true;
}
return result;
}
public void show(String[] array, int i, Frame jFrame) {
Font font = new Font("宋体", Font.BOLD, 20);
JFrame jf = new JFrame("测试窗口");
jf.setSize(500, 400);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
Box vBox = Box.createVerticalBox();
for (int z = 0; z < i; z++) {
vBox.add(new JLabel(array[z])).setFont(font);
}
JScrollPane scrollPane = new JScrollPane(vBox, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jf.setContentPane(scrollPane);
jf.setVisible(true);
}
public void show01(String jTextField, JFrame jFrame) {
try {
Class.forName(name);// 加载数据库驱动
// 建立数据库连接,获得连接对象conn
Connection conn = DriverManager.getConnection(url, user, password);
// 生成一条SQL语句
String sql = "delete from first_user where name = ?";
PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql);// 创建一个Statement对象
ps.setString(1, jTextField);// 为sql语句中第一个问号赋值
ps.executeUpdate();// 执行sql语句
conn.close();// 关闭数据库连接对象
JOptionPane.showOptionDialog(jFrame, "该用户存在,是否删除?", "操作结果", JOptionPane.YES_NO_OPTION, 0, null, null, ps);
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
public void add(String jTextField, String jTextField2, String jTextField3) {
try {
Class.forName(name);// 加载数据库驱动
// 建立数据库连接,获得连接对象conn
Connection conn = DriverManager.getConnection(url, user, password);
// 生成一条SQL语句
String sql = "insert into first_user(name,telephoneNum,email) values(?,?,?)";
PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql);// 创建一个Statement对象
ps.setString(1, jTextField);// 为sql语句中第一个问号赋值
ps.setString(2, jTextField2);// 为sql语句中第二个问号赋值
ps.setString(3, jTextField3);// 为sql语句第三个问号赋值
ps.executeUpdate();// 执行sql语句
JOptionPane.showMessageDialog(null, "添加成功!","操作结果",JOptionPane.OK_OPTION);
conn.close();// 关闭数据库连接对象
// JOptionPane.showMessageDialog(jFrame, "添加成功!", "操作结果",
// JOptionPane.PLAIN_MESSAGE, null);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void update(String name01,String telephone,String email){
try {
Class.forName(name);
Connection connection = DriverManager.getConnection(url, user, password);
String sql = "update first_user set telephoneNum = ?,email = ?Where name = ?";
PreparedStatement preparedStatement = (PreparedStatement)connection.prepareStatement(sql);
preparedStatement.setString(1, telephone);
preparedStatement.setString(2, email);
preparedStatement.setString(3, name01);
preparedStatement.executeUpdate();
JOptionPane.showMessageDialog(null, "修改成功!","操作结果",JOptionPane.OK_OPTION);
connection.close();
}catch (ClassNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();}
}
public static void main(String[] args) throws SQLException {
// TODO Auto-generated method stub
Example03 example03 = new Example03();
example03.show();
}
}
```## 中山大学南方学院19届学生
简易电话簿
最新推荐文章于 2024-08-19 17:49:21 发布