如何将java数据库连接连接到许多jFrames?
Posted
技术标签:
【中文标题】如何将java数据库连接连接到许多jFrames?【英文标题】:How to connect java database connection to many jFrames? 【发布时间】:2015-05-04 04:25:34 【问题描述】:我创建了一个名为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
更改为更像工厂,它提供了您可以请求它代表您进行查询的方法。也使用构造函数为连接属性播种,然后简单地将DBConnection
的实例传递给需要对数据库进行查询的类
【参考方案1】:
改变你的设计。 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
接口,类似于...
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
s,而应该使用 char[]
数组。
另外,您可以考虑看看The Use of Multiple JFrames, Good/Bad Practice? 和How to Use CardLayout
【讨论】:
【参考方案2】:只是想在此处添加一些更改点... isValidUser 可以是静态方法,因为不涉及任何状态。 因此“UserQueryManager userQuertManager = new UserQueryManager()” 每次单击按钮时都会创建新对象...
dbconnection 也可以作为接口注入到 UserQueryManager 成员。 同样对于主要的提问者..如果您以 MVC 的角度看待这些类,那么如果您查看
,代码的分解会更好AdminLogin 是一个作为 View 组件的框架 ...因此,将 DBConnection 或 QueryManager 对象视为该组件的成员变量不是很干净。
【讨论】:
以上是关于如何将java数据库连接连接到许多jFrames?的主要内容,如果未能解决你的问题,请参考以下文章