gpt4 book ai didi

java - 如何将 java 数据库连接到多个 jFrames?

转载 作者:行者123 更新时间:2023-11-29 05:07:04 26 4
gpt4 key购买 nike

我创建了一个名为DBConnection 的类来连接sql 数据库。我还有一个名为 AdminLogin 的 jFrame。我想知道的是,如何将数据库连接传递给 AdminLogin 类,而无需键入整个数据库连接代码。

这是我的 DBConnection 类

package ltdasystem;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;



public class DBConnection {

private Connection conn = null;
private String query = null;
private ResultSet rs;
private final String url=("jdbc:sqlserver://localhost:1433;databaseName=LeisureToursDatabase");
private final String usrName = "sa";
private final String pwd= "p@ssword13";

public DBConnection() {
try{
conn = DriverManager.getConnection(url, usrName, pwd);
query = "SELECT * from UserATable";
PreparedStatement stm = conn.prepareStatement(query);
rs = stm.executeQuery();
while (rs.next()){
String userName = rs.getString("Username");
String password = rs.getString("Password");

System.out.println("Username : " + userName + "\n" + "Password : " + password);

}
conn.close();

}catch (SQLException e){

System.out.println(e.getMessage());

}

}

}

这是我的 AdminLogin。我完成它的方式现在工作正常,但我想这样做而不必键入所有数据库连接代码。

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;


/**
*
* @author Yasitha
*/
public class AdminLogin extends javax.swing.JFrame {

Connection conn = null;
String query = null;
ResultSet rs;
private final String url=("jdbc:sqlserver://localhost:1433;databaseName=LeisureToursDatabase");
private final String usrName = "sa";
private final String pwd= "p@ssword13";


/**
* Creates new form AdminLogin
*/
public AdminLogin() {
initComponents();
}



private void LoginButtonActionPerformed(java.awt.event.ActionEvent evt) {
//DBConnection db = new DBConnection();

try{

conn = DriverManager.getConnection(url, usrName, pwd);
query = "SELECT * FROM UserATable WHERE Username = '" + userName.getText()
+ "' AND Password = " + password.getText();


PreparedStatement stm = conn.prepareStatement(query);
rs = stm.executeQuery();
if(rs.next()){
dispose();
String x = rs.getString("Username");
AdminMenu admenu = new AdminMenu();
admenu.setVisible(true);
admenu.generate(x);

}

}catch (SQLException ex) {

JOptionPane.showMessageDialog(null, ex);
//return null;

} finally {


}
}


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new AdminLogin().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton BackButton;
private javax.swing.JButton LoginButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPasswordField password;
private javax.swing.JTextField userName;
// End of variables declaration
}

最佳答案

改变你的设计。 DBConnection 应该是一个工厂/经理,它包装了数据库连接和查询的核心功能和管理(或者至少形成了基础)

public interface DBConnection {

public Connection getConnection() throws SQLException;

}

然后您可以创建查询管理类或方法来处理实际繁重的工作....

public class UserQueryManager {

public boolean isValidUser(DBConnection dBConnection, String name, String password) throws SQLException {

boolean isValidUser = false;
try (Connection con = dBConnection.getConnection()) {

try (PreparedStatement stmt = con.prepareStatement("select count(*) from UserATable where name=? and password=?")) {

try (ResultSet rs = stmt.executeQuery()) {

if (rs.next()) {

int count = rs.getInt(1);
isValidUser = count > 0; // Maybe count == 1 would be more valid

}

}

}

}

return isValidUser;

}

}

现在,我们需要一个 DBConnection 接口(interface)的实现,比如...

public class DefaultDBConnection implements DBConnection {

private final String url;
private final String userName;
private final String password;

public DefaultDBConnection(String url, String userName, String password) {
this.url = url;
this.userName = userName;
this.password = password;
}

@Override
public Connection getConnection() throws SQLException {

return DriverManager.getConnection(url, userName, password);

}
}

然后您将创建 DBConnection 的实例...

DefaultDBConnection dBConnection = new DefaultDBConnection("jdbc:sqlserver://localhost:1433;databaseName=LeisureToursDatabase", "sa", "p@ssword13");

并将它传递给你的框架(或任何其他可能喜欢使用它的人)

AdminLogin adminLogin = new AdminLogin(dBConnection);

然后您需要修改 AdminLogin 以通过构造函数接受参数...

public class AdminLogin extends javax.swing.JFrame {

private DBConnection dBConnection;

/**
* Creates new form AdminLogin
*/
public AdminLogin(DBConnection dBConnection) {
this.dBConnection = dBConnection;
initComponents();
}

LoginButtonActionPerformed 设置 UserQueryManager 并调用 isValidUser...

private void LoginButtonActionPerformed(java.awt.event.ActionEvent evt) {
try {
UserQueryManager userQuertManager = new UserQueryManager();
if (userQuertManager.isValidUser(dBConnection, userName.getText(), password.getText())) {
dispose();
AdminMenu admenu = new AdminMenu();
admenu.setVisible(true);
admenu.generate(userName.getText());
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex);
//return null;
}
}

请注意,您真的不应该将密码作为 String 传递,而应该使用 char[] 数组。

此外,您可以考虑查看 The Use of Multiple JFrames, Good/Bad Practice?How to Use CardLayout

关于java - 如何将 java 数据库连接到多个 jFrames?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30022597/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com