avaswing与mysql结合 登录注册界面代码
时间: 2023-11-22 08:53:56 浏览: 78
java MySQL注册登录系统
以下是一个简单的基于Java Swing和MySQL的登录注册界面代码,供参考:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class LoginRegisterDemo extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
JLabel title = new JLabel("Login / Register Demo");
JLabel usernameLabel = new JLabel("Username:");
JLabel passwordLabel = new JLabel("Password:");
JTextField usernameField = new JTextField(20);
JPasswordField passwordField = new JPasswordField(20);
JButton loginButton = new JButton("Login");
JButton registerButton = new JButton("Register");
public LoginRegisterDemo() {
super("Login / Register Demo");
setLayout(new BorderLayout());
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints cs = new GridBagConstraints();
cs.fill = GridBagConstraints.HORIZONTAL;
cs.gridx = 0;
cs.gridy = 0;
cs.gridwidth = 2;
panel.add(title, cs);
cs.gridx = 0;
cs.gridy = 1;
cs.gridwidth = 1;
panel.add(usernameLabel, cs);
cs.gridx = 1;
cs.gridy = 1;
cs.gridwidth = 1;
panel.add(usernameField, cs);
cs.gridx = 0;
cs.gridy = 2;
cs.gridwidth = 1;
panel.add(passwordLabel, cs);
cs.gridx = 1;
cs.gridy = 2;
cs.gridwidth = 1;
panel.add(passwordField, cs);
JPanel buttonPanel = new JPanel();
buttonPanel.add(loginButton);
buttonPanel.add(registerButton);
add(panel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.PAGE_END);
loginButton.addActionListener(this);
registerButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = String.valueOf(passwordField.getPassword());
if (e.getSource() == loginButton) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
PreparedStatement ps = con.prepareStatement("SELECT * FROM users WHERE username=? AND password=?");
ps.setString(1, username);
ps.setString(2, password);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
JOptionPane.showMessageDialog(this, "Login successful!");
} else {
JOptionPane.showMessageDialog(this, "Invalid username or password.");
}
con.close();
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (e.getSource() == registerButton) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
PreparedStatement ps = con.prepareStatement("INSERT INTO users (username, password) VALUES (?, ?)");
ps.setString(1, username);
ps.setString(2, password);
int result = ps.executeUpdate();
if (result > 0) {
JOptionPane.showMessageDialog(this, "Registration successful!");
} else {
JOptionPane.showMessageDialog(this, "Registration failed.");
}
con.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
LoginRegisterDemo demo = new LoginRegisterDemo();
demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
demo.setSize(400, 200);
demo.setVisible(true);
}
}
```
在这个例子中,我们使用了MySQL数据库来存储用户的登录信息。`loginButton`和`registerButton`的`ActionListener`处理程序在用户单击按钮时分别执行登录和注册逻辑。当用户登录时,我们会查询数据库以查找匹配的用户名和密码。如果有匹配项,则显示“登录成功”消息框,否则显示“无效的用户名或密码”消息框。当用户注册时,我们将用户名和密码插入到数据库中。如果插入成功,则显示“注册成功”消息框,否则显示“注册失败”消息框。
请确保你已经安装了MySQL JDBC驱动程序并且正确配置了你的MySQL数据库。替换上面代码中的数据库名称、用户名和密码以适合你自己的环境。
阅读全文