import javax.swing.; import java.awt.; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.*; public class demo { public static void main(String[] args) throws RuntimeException { JFrame jFrame=new JFrame("登录窗口"); JLabel oo=new JLabel("账号",JLabel.CENTER); JLabel qq=new JLabel("密码",JLabel.CENTER); JTextField ii = new JTextField(10); JPasswordField kk = new JPasswordField(10); JButton mm=new JButton("确定"); JButton ss=new JButton("注册"); JPanel p=new JPanel(); p.setLayout(new GridLayout(3,2)); JPanel p1=new JPanel(); p1.add(oo); p1.add(ii); JPanel p2=new JPanel(); p2.add(qq); p2.add(kk); JPanel p3=new JPanel(); p3.add(mm); p3.add(ss); p.add(p1); p.add(p2); p.add(p3); jFrame.setSize(400,300); jFrame.setDefaultCloseOperation(jFrame.EXIT_ON_CLOSE); jFrame.setVisible(true); jFrame.add(p); ss.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { demo d=new demo(); d.pp(p1,p2); } }); } public void pp(JPanel i, JPanel name){ try{ Class.forName("com.mysql.cj.jdbc.Driver"); Connection configuration= DriverManager.getConnection("jdbc:mysql://localhost:3306/xscj?db_student","root","123456"); Statement system=configuration.createStatement(); String sql=("insert into opop values("+i +","+"'"+name+"')"); system.executeUpdate(sql); String ll="select * from opop"; ResultSet set=system.executeQuery(ll); while(set.next()){ String id =set.getString("ID"); String pp=set.getString("name"); System.out.println(id+pp); } System.out.println(" "); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } catch (SQLException e) { throw new RuntimeException(e); } } public void uu(){ } }优化这段代码
时间: 2024-01-23 19:03:34 浏览: 118
java语言详细教程学习 java自学攻略总结.docx
以下是对代码进行优化的建议:
1. 将`demo`这个类重命名为更具描述性的类名,比如`LoginWindow`。
2. 将所有的Swing组件创建和UI布局放在一个独立的方法里,如`createUI()`,然后在主方法中调用该方法。这样可以提高代码的可读性和可维护性。
3. 避免在主线程中进行数据库操作。应该将数据库操作放在单独的线程中,以免阻塞UI线程,导致用户体验不佳。
4. 将数据库操作封装在独立的类中,比如`DatabaseHelper`,以提高代码的可重用性和可维护性。
5. 在处理异常时,应该将异常抛出或记录到日志中,而不是简单地抛出`RuntimeException`。
下面是优化后的代码示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginWindow {
private JTextField accountTextField;
private JPasswordField passwordField;
public static void main(String[] args) {
LoginWindow loginWindow = new LoginWindow();
loginWindow.createUI();
}
private void createUI() {
JFrame jFrame = new JFrame("登录窗口");
JLabel accountLabel = new JLabel("账号", JLabel.CENTER);
JLabel passwordLabel = new JLabel("密码", JLabel.CENTER);
accountTextField = new JTextField(10);
passwordField = new JPasswordField(10);
JButton loginButton = new JButton("确定");
JButton registerButton = new JButton("注册");
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2));
JPanel panel1 = new JPanel();
panel1.add(accountLabel);
panel1.add(accountTextField);
JPanel panel2 = new JPanel();
panel2.add(passwordLabel);
panel2.add(passwordField);
JPanel panel3 = new JPanel();
panel3.add(loginButton);
panel3.add(registerButton);
panel.add(panel1);
panel.add(panel2);
panel.add(panel3);
jFrame.setSize(400, 300);
jFrame.setDefaultCloseOperation(jFrame.EXIT_ON_CLOSE);
jFrame.setVisible(true);
jFrame.add(panel);
registerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String account = accountTextField.getText();
String password = new String(passwordField.getPassword());
DatabaseHelper.addUser(account, password);
}
});
}
}
class DatabaseHelper {
private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/xscj?db_student";
private static final String USERNAME = "root";
private static final String PASSWORD = "123456";
public static void addUser(String account, String password) {
new Thread(() -> {
try (Connection connection = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);
Statement statement = connection.createStatement()) {
String sql = "INSERT INTO opop VALUES ('" + account + "', '" + password + "')";
statement.executeUpdate(sql);
String query = "SELECT * FROM opop";
ResultSet resultSet = statement.executeQuery(query);
while (resultSet.next()) {
String id = resultSet.getString("ID");
String name = resultSet.getString("name");
System.out.println(id + name);
}
System.out.println(" ");
} catch (SQLException e) {
e.printStackTrace();
}
}).start();
}
}
```
希望这些优化可以对您有所帮助。
阅读全文