2017-04-26 21 views
0

我想將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

歡迎來到Stack Overflow!請[參觀](http://stackoverflow.com/tour)以查看網站的工作原理和問題,並相應地編輯您的問題。另請參閱:[爲什麼「有人可以幫我嗎?」不是一個真正的問題?](http://meta.stackoverflow.com/q/284236) –

+0

是的,這是一個好主意。問題是什麼? – MadProgrammer

+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()的數據庫連接的參考。

您也不應將數據庫憑據存儲在源代碼中。將祕密賬戶和密碼硬編碼到您的軟件中非常方便 - 對於熟練的反向工程師。如果所有軟件的密碼相同,那麼當密碼不可避免地變爲已知時,每個客戶都會變得易受攻擊。而且因爲它是硬編碼的,所以修復這是一個巨大的痛苦。

對於一個快速和骯髒的解決方案/小破解它很好,但出於生產目的,您應該將配置信息(包括密碼)存儲在應用程序在啓動時讀取的單獨文件中。這是防止密碼因反編譯而泄漏的唯一真正方法(從不將它編譯到二進制文件中開始)

+0

我是新程序員,但我感謝你的小技巧。 –

0

您可以使您的連接靜態並在首次調用getConnection方法時初始化它。這樣的代碼:

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。