帮我写一份Java代码gui页面窗口登录账号密码是连接MySQL数据库的
时间: 2024-02-05 13:10:58 浏览: 20
好的,以下是一个简单的Java代码示例,可以通过GUI页面窗口登录账号密码,并连接到MySQL数据库。
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class LoginWindow extends JFrame implements ActionListener {
private JLabel usernameLabel, passwordLabel;
private JTextField usernameField;
private JPasswordField passwordField;
private JButton loginButton, registerButton;
public LoginWindow() {
super("Login Window");
// Initialize components
usernameLabel = new JLabel("Username:");
passwordLabel = new JLabel("Password:");
usernameField = new JTextField(20);
passwordField = new JPasswordField(20);
loginButton = new JButton("Login");
registerButton = new JButton("Register");
// Add components to panel
JPanel panel = new JPanel(new GridLayout(3, 2));
panel.add(usernameLabel);
panel.add(usernameField);
panel.add(passwordLabel);
panel.add(passwordField);
panel.add(loginButton);
panel.add(registerButton);
// Add panel to frame
add(panel);
// Register action listeners
loginButton.addActionListener(this);
registerButton.addActionListener(this);
// Set frame properties
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 150);
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
try {
// Connect to MySQL database
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "root", "password");
// Check if username and password are valid
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM users WHERE username=? AND password=?");
stmt.setString(1, username);
stmt.setString(2, password);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
JOptionPane.showMessageDialog(this, "Login successful!");
} else {
JOptionPane.showMessageDialog(this, "Invalid username/password.");
}
// Close database connection
rs.close();
stmt.close();
conn.close();
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage());
}
} else if (e.getSource() == registerButton) {
// Open register window
RegisterWindow registerWindow = new RegisterWindow();
registerWindow.setVisible(true);
}
}
public static void main(String[] args) {
LoginWindow loginWindow = new LoginWindow();
}
}
```
在此示例中,我们使用了Swing组件来创建GUI窗口,并通过JDBC驱动程序连接到MySQL数据库。注意,这里的数据库连接URL、用户名和密码应该替换为你自己的实际值。
另外,我们还创建了一个RegisterWindow类,可以在需要时使用它来显示注册窗口。
你可以根据自己的需求和实际情况进行修改和扩展。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)