用javaSwing写一个含有登录,注册,连接数据库,有增、删、改、查功能的页面
时间: 2024-03-17 22:41:09 浏览: 22
好的,我可以为您提供一个简单的Java Swing程序,包含登录、注册和连接数据库功能。但是,增删改查功能需要结合具体的数据库进行编写,您可以根据自己的需求进行扩展。
以下是程序代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class LoginDemo extends JFrame implements ActionListener {
private JLabel label1, label2;
private JTextField textField;
private JPasswordField passwordField;
private JButton button1, button2;
private JPanel panel1, panel2, panel3;
private CardLayout cardLayout;
private Connection conn;
public LoginDemo() {
super("登录界面");
label1 = new JLabel("用户名:");
label2 = new JLabel("密 码:");
textField = new JTextField(20);
passwordField = new JPasswordField(20);
button1 = new JButton("登录");
button2 = new JButton("注册");
panel1 = new JPanel(new GridLayout(2, 1));
panel2 = new JPanel(new GridLayout(2, 1));
panel3 = new JPanel();
cardLayout = new CardLayout();
panel3.setLayout(cardLayout);
panel1.add(label1);
panel1.add(label2);
panel2.add(textField);
panel2.add(passwordField);
panel3.add(panel1, "label");
panel3.add(panel2, "field");
button1.addActionListener(this);
button2.addActionListener(this);
setLayout(new BorderLayout());
add(panel3, BorderLayout.CENTER);
add(button1, BorderLayout.SOUTH);
add(button2, BorderLayout.EAST);
setSize(300, 150);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
connectDB();
}
private void connectDB() {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url, user, password);
System.out.println("连接数据库成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
String username = textField.getText().trim();
String password = new String(passwordField.getPassword());
if (validate(username, password)) {
JOptionPane.showMessageDialog(this, "登录成功!");
} else {
JOptionPane.showMessageDialog(this, "用户名或密码错误!");
}
} else if (e.getSource() == button2) {
String username = JOptionPane.showInputDialog(this, "请输入用户名:");
String password = JOptionPane.showInputDialog(this, "请输入密码:");
if (username != null && password != null) {
if (register(username, password)) {
JOptionPane.showMessageDialog(this, "注册成功!");
} else {
JOptionPane.showMessageDialog(this, "用户名已存在!");
}
}
}
}
private boolean validate(String username, String password) {
try {
String sql = "select * from user where username='" + username + "' and password='" + password + "'";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
return rs.next();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
private boolean register(String username, String password) {
try {
String sql = "select * from user where username='" + username + "'";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
return false;
} else {
sql = "insert into user(username,password) values('" + username + "','" + password + "')";
stmt.executeUpdate(sql);
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static void main(String[] args) {
new LoginDemo();
}
}
```
其中,connectDB() 方法用于连接数据库,validate() 方法用于验证登录信息,register() 方法用于注册用户信息。您可以根据自己的需求进行修改和扩展。