我想将db连接与其他类分开,因此我不需要为我想要创建的每个类再次编写db连接。
这是数据库连接类
package bloodbank;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class dbconnection {
PreparedStatement pst = null;
ResultSet rs = null;
public dbconnection() {}
public void connect() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con =
DriverManager.getConnection("jdbc:sqlserver://localhost:1433;
databaseName=BloodManagementSystem;user=yusuf;password=ali1234");
} catch (ClassNotFoundException | SQLException e) {
}
}
public PreparedStatement prepareStatement(String select__from_Users_where_Username_and_Pas) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
这是带登录按钮的登录类
import bloodbank.dbconnection;
import java.awt.HeadlessException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
public class login extends javax.swing.JFrame {
public login() {
initComponents();
}
@SuppressWarnings("unchecked")
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
try
{
dbconnection con = new dbconnection();
con.connect();
PreparedStatement pst = con.prepareStatement("Select * from Users where Username=? and Password=?");
pst.setString(1, jTextField1.getText());
pst.setString(2, jTextField2.getText());
ResultSet rs = pst.executeQuery();
if(rs.next()) {
JOptionPane.showMessageDialog(null, "Username and Password correct");
Mainform field = new Mainform();
field.setVisible(true);
setVisible(false);
} else {
JOptionPane.showMessageDialog(null, "invalid username and password");
}
}
catch(SQLException | HeadlessException e){
JOptionPane.showMessageDialog(null, e);
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(() -> {
new login().setVisible(true);
});
}
}
答案 0 :(得分:0)
我假设您只想在整个应用程序中使用一个连接。您应该为dbconnection
使用单例样式的类。有一个private static Connection cnx
变量来存储连接,并在每次尝试创建新连接之前检查是否建立了连接。
public class dbconnection {
private static Connection cnx = null;
public Connection getConnection() {
if(cnx == null) {
cnx = (... initialize the connection ...)
return cnx
}
}
致电Connection con = dbconnection.getConnection()
以获取对当前.connect()
的数据库连接的引用。
您也不应该在源代码中存储数据库凭据。对于熟练的逆向工程师来说,将秘密帐户和密码硬编码到您的软件中非常方便。如果所有软件的密码相同,那么当密码不可避免地被人知晓时,每个客户都会变得脆弱。而且因为它是硬编码的,修复起来非常痛苦。
对于快速而肮脏的解决方案/小黑客它很好,但出于生产目的,您应该将配置信息(包括密码)存储在应用程序启动时读取的单独文件中。这是防止密码因反编译而泄漏的唯一真正方法(永远不要将其编译成二进制文件开头)
答案 1 :(得分:0)
您可以在第一次调用getConnection方法时使Connection保持静态并初始化它。喜欢这段代码:
package com.stackoverflow.json;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class dbconnection {
PreparedStatement pst = null;
ResultSet rs = null;
private static Connection con;
public dbconnection() {
}
private static void connect() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433; databaseName=BloodManagementSystem;user=yusuf;password=ali1234");
} catch (ClassNotFoundException | SQLException e) {
}
}
public PreparedStatement prepareStatement(String select__from_Users_where_Username_and_Pas) {
throw new UnsupportedOperationException("Not supported yet.");
}
public static Connection getConnection() {
if (con == null)
connect();
return con;
}
}
您还可以创建名为DBUtils的类,其中包含使用jdbc查询数据库的所有方法。