2015-07-20 32 views
1

我有一些付款必須裝入TableView。爲此,我寫了兩堂課。訪問數據庫並寫入Bean |最佳實踐

這一個創建與數據庫的連接,從.properties文件

package jdbc; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 
import java.util.Properties; 
/** 
* JDBCAccess bietet Zugriff auf eine Datenbank. 
* Hierzu werden die Zugangsdaten aus einer Properties Datei geladen welche sich im im Ordner 
* 'db' befinden. 
* @author Christian 
* 
*/ 
public final class JDBCAccess { 

    private Connection connection; 
    private String username; 
    private String password; 
    private String dbURL; 
    private String driverURL; 

    private static final String dbPropsPath = "db/db-props.properties"; 

    /** 
    * Standar Konstrukter der Klasse. 
    * Beim Aufruf dieses Konstruktors wird die connectTo Methode automatisch mit aufgerufen 
    */ 
    public JDBCAccess() { 
     super(); 
    } 

    /** 
     * Lädt die Parameter aus der Properties Datei direkt in die Variabeln 
     */ 
    private void loadParameters() throws FileNotFoundException, IOException { 
     Properties dbProps = new Properties(); 
     File dbPropsFile = new File(dbPropsPath); 
     dbProps.load(new FileInputStream(dbPropsFile)); 
     username = dbProps.getProperty("username"); 
     password = dbProps.getProperty("password"); 
     dbURL = dbProps.getProperty("dbURL"); 
     driverURL = dbProps.getProperty("driverURL"); 
    } 

    /** 
    * Verbindung zur Datenbank herstellen 
    * 
    * @throws FileNotFoundException db-props.properties Datei konnte nicht gefunden werden 
    * @throws IOException db-props.properties Datei, laden fehlgeschlagen 
    * @throws ClassNotFoundException Fehler in Treiber URL 
    * @throws SQLException Verbindungsaufbau zur Datenbank fehlgeschlagen 
    */ 
    public void connectTo() throws FileNotFoundException, IOException, ClassNotFoundException, SQLException { 
     if (connection == null || connection.isClosed()) { 
      loadParameters(); 
      Class.forName(driverURL); 
      connection = DriverManager.getConnection(dbURL, username, password); 
     } 

    } 

    /** 
    * Schließt die Datenbankverbindung 
    * 
    * @throws SQLException Schließen der Datenbank fehlgeschlagen 
    */ 
    public void close() throws SQLException { 
     if (connection != null && !connection.isClosed()) { 
      connection.close(); 
     } 
    } 

    public Connection getConnection() { 
     return connection; 
    } 

} 

這一個負載獲取此信息,並保存在數據庫中的豆:

package beans; 

import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 

import javafx.beans.property.SimpleDoubleProperty; 
import javafx.beans.property.SimpleIntegerProperty; 
import javafx.beans.property.SimpleStringProperty; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import jdbc.JDBCAccess; 

public class PaymentBean { 

    private SimpleIntegerProperty id; //ID 
    private SimpleDoubleProperty payment; //Betrag 
    private SimpleStringProperty category; //Kategories 
    private SimpleStringProperty usage; //Verwendungszweck 
    private SimpleStringProperty date; //Datum 

    private static Connection connection; 

    private static final String EARNINGS_SQL = "SELECT * FROM PAYMENT WHERE PAYMENT >= 0;"; 
    private static final String SPENDING_SQL = "SELECT * FROM PAYMENT WHERE PAYMENT < 0;"; 
    private static final String ALL_SQL = "SELECT * FROM PAYMENT;"; 

    /** 
    * Standarkonstruktor, initialisiert alle privaten Felder, sodass sie 
    * benutzt werden können 
    */ 
    public PaymentBean() { 
     super(); 
     id = new SimpleIntegerProperty(); 
     payment = new SimpleDoubleProperty(); 
     category = new SimpleStringProperty(); 
     usage = new SimpleStringProperty(); 
     date = new SimpleStringProperty(); 
    } 


    public final int getId() { 
     return id.get(); 
    } 

    public final void setId(int id) { 
     this.id.set(id); 
    } 

    public final double getPayment() { 
     return payment.get(); 
    } 

    public final void setPayment(double payment) { 
     this.payment.set(payment); 
    } 

    public final String getCategory() { 
     return category.get(); 
    } 

    public final void setCategory(String category) { 
     this.category.set(category); 
    } 

    public final String getUsage() { 
     return usage.get(); 
    } 

    public final void setUsage(String usage) { 
     this.usage.set(usage); 
    } 

    public final String getDate() { 
     return date.get(); 
    } 

    public final void setDate(String date) { 
     this.date.set(date); 
    } 


    /****************************** 
    * DATENBANK ZUGRIFFEDER BEAN * 
    *****************************/ 

    /** 
    * Diese Methode wird von allen Methoden benutzt, die Daten aus 
    * der Datenbank in ein ResultSet laden. Dieses ResultSet wird in eine 
    * ObservableList<PaymentBean> geladen, um somit ein Abbild in einer Tabelle zu erstellen 
    * @param connection Datenbankverbindung, muss schon geöffnet sein 
    * @param sql SQL Befehl, welcherv ausgeführt werden soll 
    * @return Liste mit Datenbankergebnissen 
    * @throws SQLException 
    * @throws IOException 
    * @throws ClassNotFoundException 
    * @throws FileNotFoundException 
    */ 
    private static ObservableList<PaymentBean> loadFromDB(final JDBCAccess jdbc, String sql) throws SQLException, FileNotFoundException, ClassNotFoundException, IOException { 
     jdbc.connectTo(); 
     connection = jdbc.getConnection(); 
     if (connection != null) { 
      ObservableList<PaymentBean> data = FXCollections.observableArrayList(); 
      Statement stmt = connection.createStatement(); 
      ResultSet rs = stmt.executeQuery(sql); 
      while (rs.next()) { 
       PaymentBean pb = new PaymentBean(); 
       pb.setId(rs.getInt(1)); 
       pb.setPayment(rs.getDouble(2)); 
       pb.setDate(rs.getString(3)); 
       pb.setUsage(rs.getString(4)); 
       pb.setCategory(rs.getString(5)); 
       data.add(pb); 
      } 

      stmt.close(); 
      rs.close(); 
      jdbc.close(); 
      return data; 
     } 

     return null; 
    } 

    /** 
    * Statische Methode um alle PaymentBeans aus der angegebenen Datenbank zu laden 
    * @param connection Verbindung zur Datenbank 
    * @return Gibt alle Zahlungen in Form einer ObservableList<PaymentBean> zurück 
    * @throws SQLException 
    * @throws IOException 
    * @throws ClassNotFoundException 
    * @throws FileNotFoundException 
    */ 
    public static ObservableList<PaymentBean> loadBeansFromDB(JDBCAccess jdbc) throws SQLException, FileNotFoundException, ClassNotFoundException, IOException { 
     return loadFromDB(jdbc, ALL_SQL); 
    } 

    /** 
    * Statische Methode um alle Einzahlungen aus der angegebenen Datenbank zu laden 
    * @param connection Datenbankverbindung 
    * @return Gibt alle Einzahlungen in Form einer ObservableList<PaymentBean> zurück 
    * @throws SQLException 
    * @throws IOException 
    * @throws ClassNotFoundException 
    * @throws FileNotFoundException 
    */ 
    public static ObservableList<PaymentBean> loadEarningsFromDB(JDBCAccess jdbc) throws SQLException, FileNotFoundException, ClassNotFoundException, IOException { 
     return loadFromDB(jdbc, EARNINGS_SQL); 
    } 

    /** 
    * Statische Methode um alle Auszahlungen aus der angegebenen Datenbank zu laden 
    * @param connection Datenbankverbindung 
    * @return Gibt alle Auszahlungen in Form einer ObservableList<PaymentBean> zurück 
    * @throws SQLException 
    * @throws IOException 
    * @throws ClassNotFoundException 
    * @throws FileNotFoundException 
    */ 
    public static ObservableList<PaymentBean> loadSpendingsFromDB(JDBCAccess jdbc) throws SQLException, FileNotFoundException, ClassNotFoundException, IOException { 
     return loadFromDB(jdbc, SPENDING_SQL); 
    } 

    public static double getTotalSum(JDBCAccess jdbc) throws SQLException, FileNotFoundException, ClassNotFoundException, IOException { 
     jdbc.connectTo(); 
     connection = jdbc.getConnection(); 
     String sql = "SELECT SUM(PAYMENT) FROM PAYMENT;"; 
     Statement stmt = connection.createStatement(); 
     ResultSet rs = stmt.executeQuery(sql); 
     rs.next(); 
     jdbc.close(); 
     return rs.getDouble(1); 
    } 

    /** 
    * Speichert die Bean in der Datenbank ab 
    * @param connection Datanbankverbindung 
    * @throws SQLException 
    * @throws IOException 
    * @throws ClassNotFoundException 
    * @throws FileNotFoundException 
    */ 
    public void saveInDB(JDBCAccess jdbc) throws SQLException, FileNotFoundException, ClassNotFoundException, IOException { 
     jdbc.connectTo(); 
     connection = jdbc.getConnection(); 
     if (connection != null) { 
      String sql = "INSERT INTO PAYMENT (PAYMENT,DATE,USAGE,CATEGORY) " 
        + "VALUES (?,?,?,?);"; 
      PreparedStatement insert = connection.prepareStatement(sql); 
      insert.setDouble(1, payment.get()); 
      insert.setString(2, date.get()); 
      insert.setString(3, usage.get()); 
      insert.setString(4, category.get()); 
      insert.executeUpdate(); 
      insert.close(); 
      jdbc.close(); 
     } 
    } 

    /** 
    * Updatet die Bean in der Datenbank 
    * @param connection Verbindung zur Datenbank 
    * @param id ID der zu updatenden Bean 
    * @throws SQLException 
    * @throws IOException 
    * @throws ClassNotFoundException 
    * @throws FileNotFoundException 
    */ 
    public void updateBeanInDB(JDBCAccess jdbc, int id) throws SQLException, FileNotFoundException, ClassNotFoundException, IOException { 
     jdbc.connectTo(); 
     connection = jdbc.getConnection(); 
     if (connection != null) { 
      String sql = "UPDATE PAYMENT " 
        + "SET PAYMENT = ? ,DATE = ? ,USAGE=? ,CATEGORY=? " 
        + "WHERE ID = ?;"; 
      PreparedStatement insert = connection.prepareStatement(sql); 
      insert.setDouble(1, payment.get()); 
      insert.setString(2, date.get()); 
      insert.setString(3, usage.get()); 
      insert.setString(4, category.get()); 
      insert.setInt(5, id); 
      insert.executeUpdate(); 
      insert.close(); 
      jdbc.close(); 
     } 

    } 

    /** 
    * Löscht die Bean aus der Datenbank 
    * @param connection Verbindung zur Datenbank 
    * @throws SQLException 
    * @throws IOException 
    * @throws ClassNotFoundException 
    * @throws FileNotFoundException 
    */ 
    public void delete(JDBCAccess jdbc) throws SQLException, FileNotFoundException, ClassNotFoundException, IOException { 
     jdbc.connectTo(); 
     connection = jdbc.getConnection(); 
     String sql = "DELETE FROM PAYMENT WHERE ID = ?;"; 
     PreparedStatement stmt = connection.prepareStatement(sql); 
     stmt.setInt(1, this.id.get()); 
     stmt.executeUpdate(); 
     stmt.close(); 
     jdbc.close(); 
    } 

} 

隨着SimpleXXXProperty中,可以在TableView中表示沒有任何代碼的屬性。

我能做些什麼更好?我可以從付款bean方法寫入數據庫,還是應該從這個bean的外部訪問數據庫?

回答

1

我能做得更好嗎?

爲什麼不使用像休眠一樣ORM

可以從支付bean方法寫入數據庫,還是 我應該從這個bean的外部訪問數據庫嗎?

您正在做的數據庫訪問不完全包含在bean內部。客戶端需要傳遞一個JDBCAccess對象來執行任何數據庫任務 - 因此不是很方便。它只是將模型& dao合併到一個類中。

也要考慮這個bean是否是發佈API的一部分,或者只在您的應用中用作持久性模型。如果它的API,那麼你將持久性邏輯的內部分發給客戶端,並強制依賴於JDBCAccess類。

對代碼的評論:考慮不要拋出檢查異常。最後做像connection.close()這樣的操作。

+0

感謝您的回答。知道我在尋找一些關於休眠的東西。 – Chris