帮我用(Javaswing和连接MySQL的)分别写1.可视化的用户修改密码2.可视化的注销账号的代码
时间: 2024-02-12 20:02:25 浏览: 65
1. 可视化的用户修改密码(Java Swing + MySQL)
下面是一个可视化的用户修改密码的代码示例:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class ChangePassword extends JFrame implements ActionListener {
JLabel l1, l2, l3;
JTextField tf1, tf2;
JPasswordField p1, p2;
JButton btn1, btn2;
public ChangePassword() {
setTitle("修改密码");
l1 = new JLabel("当前密码:");
l2 = new JLabel("新密码:");
l3 = new JLabel("确认新密码:");
tf1 = new JTextField(20);
p1 = new JPasswordField(20);
p2 = new JPasswordField(20);
btn1 = new JButton("确认修改");
btn2 = new JButton("取消");
setLayout(new GridLayout(4, 2));
add(l1);
add(p1);
add(l2);
add(p2);
add(l3);
add(tf1);
add(btn1);
add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
setSize(300, 200);
setVisible(true);
}
public static void main(String[] args) {
new ChangePassword();
}
public void actionPerformed(ActionEvent e) {
String s1 = p1.getText();
String s2 = p2.getText();
String s3 = tf1.getText();
if (e.getSource() == btn1) {
if (s2.equals(s3)) {
try {
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mydatabase", "root", "password");
PreparedStatement ps = con.prepareStatement(
"UPDATE users SET password=? WHERE username=? AND password=?");
ps.setString(1, s2);
ps.setString(2, LoginForm.username);
ps.setString(3, s1);
int i = ps.executeUpdate();
if (i > 0) {
JOptionPane.showMessageDialog(null, "密码修改成功");
dispose();
} else {
JOptionPane.showMessageDialog(null, "修改失败,密码错误");
p1.setText("");
p2.setText("");
tf1.setText("");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(null, "两次输入的密码不一致");
p1.setText("");
p2.setText("");
}
} else if (e.getSource() == btn2) {
dispose();
}
}
}
```
这个程序使用Java Swing来创建一个简单的用户界面,用户可以输入当前密码、新密码和确认新密码。当用户点击“确认修改”按钮时,程序将检查当前密码是否正确,并将新密码写入MySQL数据库中。
2. 可视化的注销账号(Java Swing + MySQL)
下面是一个可视化的注销账号的代码示例:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class DeleteAccount extends JFrame implements ActionListener {
JLabel l1, l2;
JPasswordField p1;
JButton btn1, btn2;
public DeleteAccount() {
setTitle("注销账号");
l1 = new JLabel("请输入密码:");
p1 = new JPasswordField(20);
btn1 = new JButton("确认注销");
btn2 = new JButton("取消");
setLayout(new GridLayout(2, 2));
add(l1);
add(p1);
add(btn1);
add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
setSize(300, 100);
setVisible(true);
}
public static void main(String[] args) {
new DeleteAccount();
}
public void actionPerformed(ActionEvent e) {
String s1 = p1.getText();
if (e.getSource() == btn1) {
try {
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mydatabase", "root", "password");
PreparedStatement ps = con.prepareStatement(
"DELETE FROM users WHERE username=? AND password=?");
ps.setString(1, LoginForm.username);
ps.setString(2, s1);
int i = ps.executeUpdate();
if (i > 0) {
JOptionPane.showMessageDialog(null, "账号注销成功");
new LoginForm();
dispose();
} else {
JOptionPane.showMessageDialog(null, "注销失败,密码错误");
p1.setText("");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
} else if (e.getSource() == btn2) {
dispose();
}
}
}
```
这个程序使用Java Swing来创建一个简单的用户界面,用户需要输入密码来确认注销账号。当用户点击“确认注销”按钮时,程序将检查密码是否正确,并将用户信息从MySQL数据库中删除。如果注销成功,程序将返回到登录界面。
阅读全文