使用java实现(1)在数据库stuDB中建立user表(username,psw) (2)建立用户界面如图 (3)完成登录功能,根据用户名查询,用户名和密码从数据库中读出,进行验证。(不考虑多线程的问题)
时间: 2024-01-21 09:02:00 浏览: 44
好的,下面是使用Java实现在stuDB数据库中建立user表,建立用户界面并完成登录功能的代码示例。
1. 在stuDB数据库中建立user表:
```java
import java.sql.*;
public class CreateUserTable {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/stuDB";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
String sql = "CREATE TABLE user (username VARCHAR(20) PRIMARY KEY, psw VARCHAR(20))";
stmt.executeUpdate(sql);
System.out.println("user表创建成功");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
2. 建立用户界面:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class LoginFrame extends JFrame {
private JTextField usernameField;
private JPasswordField passwordField;
private JButton loginButton;
public LoginFrame() {
setTitle("登录");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2, 5, 5));
panel.add(new JLabel("用户名:"));
usernameField = new JTextField();
panel.add(usernameField);
panel.add(new JLabel("密码:"));
passwordField = new JPasswordField();
panel.add(passwordField);
loginButton = new JButton("登录");
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
if (login(username, password)) {
JOptionPane.showMessageDialog(LoginFrame.this, "登录成功");
} else {
JOptionPane.showMessageDialog(LoginFrame.this, "用户名或密码错误");
}
}
});
panel.add(loginButton);
setContentPane(panel);
setVisible(true);
}
private boolean login(String username, String password) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/stuDB";
String user = "root";
String passwordDB = "123456";
conn = DriverManager.getConnection(url, user, passwordDB);
String sql = "SELECT * FROM user WHERE username=? AND psw=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, username);
pstmt.setString(2, password);
rs = pstmt.executeQuery();
if (rs.next()) {
return true;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
public static void main(String[] args) {
new LoginFrame();
}
}
```
其中login方法用于根据用户名查询数据库中的记录并进行验证,如果验证通过则返回true,否则返回false。
这里使用了JDBC连接MySQL数据库,并使用JOptionPane实现了一个简单的弹窗提示登录结果。需要注意的是,这里为了简化代码,直接将数据库连接参数写死在了代码中,实际应用中应该将这些参数提取到配置文件中。
相关推荐
![](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)