Simple DB Object Builder

簡介
下 載
安裝
Table Object
快速上手
產出碼說明
Query Object
編譯原 始碼

Table Object 快速上手

在本次的說明中,我們將會作如下的動作:
  1. 創建一個新的 Netbeans 專案
  2. 在 Netbeans IDE 中建立一個資料庫連線
  3. 新增一個 Table Object 的定義檔案
  4. 產出對應資料庫表格的 Java 檔案
  5. 使用所產出的檔案,對資料庫作查詢多筆資料
  6. 使用所產出的檔案,對資料庫作新增資料
  7. 使用所產出的檔案,對資料庫作查詢單筆資料
  8. 使用所產出的檔案,對資料庫作刪除資料
  9. 使用所產出的檔案,對資料庫作修改資料
  10. 使用所產出的檔案,對資料庫作修改特定欄位內的資料
  • 在 Netbeans 建立一個測試用的專案。
專案名稱為 TEST01 ,專案路徑則自行選定。
  • 在 Netbeans IDE 中建立一個資料庫連線。
開啟 Netbeans->Windows->Services 。
DB Obejct Builder 目前可以支援三種資料庫:Oracle, MSSQL 和 MySQL,在本範例中,我們是採用 MySQL 資料庫。
建立的連線如下:

  • 新增一個 Table Object 的定義檔案
  • 開啟 Netbeans->File->New File,Categories 選擇 DBObject ,File Types 選擇 TableTemplate,按下一步。
  • File Name 填入 tableDef ,Folder 填入 src\test01\dbObjDef,按 Finish。
  • 新產出的定義檔如下:
  • 產出對應資料庫表格的 Java 檔案
  • 當開啟 tableDef時,會見到如下的畫面:
  • 指定剛所設定的資料庫連線後,按 Connection 。
  • 設定那些表格要產出對應的 Java 檔案,及產出的目錄 (Package) 。

按下 Generate Java Source,則會見到在指定的目錄下見到新產出的檔案:



  • 使用所產出的檔案,對資料庫作查詢多筆資料
  • 假設我們需要查出客戶編號小於 150 的所有客戶姓名。
  • 建立查詢範例程式 test01.examples.a01_qry.java,程式碼如下:
public static void main(String[] args) {
        try {
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/classicmodels", "root", "root");
            String whereString = String.format("where %s < 150", Customers.FLD_CUSTOMERNUMBER);
            Customers[] customers = Customers.queryDB(conn, whereString);
            for (Customers customer : customers) {
                int custNumber = customer.getCustomernumber();
                String firstName = customer.getContactfirstname();
                String lastName = customer.getContactlastname();
                System.out.println(String.format("Number:%s, Name:%s %s", custNumber, firstName, lastName));
            }
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
  • 程式碼說明
從上述的程式碼我們可以看到,僅需準備 whereString ,然後透過Customers的靜態方法來取得所有符合要求的Customers陣列。
當取得Customers物件後,就可以輕易取出內含的屬性(透過 IDE 的 Code Completion)如下:


  • 使用所產出的檔案,對資料庫作新增資料
  • 假設我們需要新增一筆資料到資料庫
  • 建立新增範例程式 test01.examples.a02_insert.java,程式碼如下:
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/classicmodels", "root", "root");
Customers customers = new Customers();
customers.setCustomernumber(1000);
customers.setCustomername("John Smith");
customers.setContactfirstname("John");
customers.setContactlastname("Smith");
customers.setPhone("0123456789");
customers.setAddressline1("Smith's Address");
customers.setCity("Taipei");
customers.setCountry("Taiwan");
customers.insertIntoDB(conn);
System.out.println("Insert OK");
conn.close();
  • 程式碼說明
從上述的程式碼我們可以看到,僅需創建一個 Customers物件,就可以輕易設定Customers內含的屬性(透過 IDE 的 Code Completion)。
最後再呼叫Customers的insertIntoDB方法,就可將資料新增到資料庫中。
  • 使用所產出的檔案,對資料庫作查詢單筆資料
  • 假設我們需要查詢客戶代碼為1000的資料
  • 建立查詢範例程式 test01.examples.a03_qry02.java,程式碼如下:
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/classicmodels", "root", "root");
Customers customer = Customers.queryByPK(conn, 1000);
int custNumber = customer.getCustomernumber();
String firstName = customer.getContactfirstname();
String lastName = customer.getContactlastname();
System.out.println(String.format("Number:%s, Name:%s %s", custNumber, firstName, lastName));
conn.close();
  • 程式碼說明
我們僅需呼叫 Customers 的靜態方法 queryByPK(conn,...) 就可以取得對應的 Customers 物件。
當取得Customers物件後,就可以輕易取出內含的屬性(透過 IDE 的 Code Completion)。
  • 使用所產出的檔案,對資料庫作刪除資料
  • 假設我們需要刪除客戶代碼為1000的資料
  • 建立查詢範例程式 test01.examples.a04_delete.java,程式碼如下:
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/classicmodels", "root", "root");
Customers customer = Customers.queryByPK(conn, 1000);
customer.deleteFromDB(conn);
System.out.println("Delete OK!");
conn.close();
  • 程式碼說明
需呼叫 Customers 的靜態方法 queryByPK(conn,...) 以取得對應的 Customers 物件。
當取得Customers物件後,就可以對其作刪除資料。
  • 使用所產出的檔案,對資料庫作修改資料
  • 假設我們需要異動客戶代碼為1000的資料
  • 建立查詢範例程式 test01.examples.a05_update.java,程式碼如下:
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/classicmodels", "root", "root");
Customers customer = Customers.queryByPK(conn, 1000);
customer.setPhone("987654321");
customer.updateDB(conn);
conn.close();
  • 程式碼說明
需呼叫 Customers 的靜態方法 queryByPK(conn,...) 以取得對應的 Customers 物件。
當取得Customers物件後,就可以對其作資料異動,異動結束後,將其寫回到資料庫中。
上述的寫回資料庫方法是會針對 所有的欄位都作異動
  • 使用所產出的檔案,對資料庫作修改特定欄位內的資料
  • 假設我們需要異動客戶代碼為1000中部份的資料
  • 建立查詢範例程式 test01.examples.a06_update02.java,程式碼如下:
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/classicmodels", "root", "root");
Customers customer = Customers.queryByPK(conn, 1000);
customer.setPhone("88888888");
customer.setPostalcode("111");
customer.updateDB(conn, Customers.FLD_PHONE, Customers.FLD_POSTALCODE);
conn.close();
  • 程式碼說明
需呼叫 Customers 的靜態方法 queryByPK(conn,...) 以取得對應的 Customers 物件。
當取得Customers物件後,就可以對其作資料異動。
異動結束後,僅將異動的欄位寫回到資料庫中,在本例中則為 PHONE POSTALCODE 這二個欄位。