Simple DB Object Builder

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

Table Object 產出碼說明

  • 表格欄位會轉換成相對 應的屬性欄位:TOP
在產出 Java 碼中,會產生屬性欄位對應到將當下該表格內的所有欄位,如下:

    public static final String FLD_CUSTOMERNUMBER = "customerNumber" ;
    public static final String FLD_CUSTOMERNAME = "customerName" ;
    public static final String FLD_CONTACTLASTNAME = "contactLastName" ;
    。。。
    public static final String FLD_CREDITLIMIT = "creditLimit" ;

其中, FLD 代表 Field ,而 FLD_ 之後則是欄位名稱,而該屬性的值則是原欄位名稱。
其設計用意為:在作 where clause 設定時,有時會參照到欄位名稱,如下:

    String whereClause = String.format(" where %s < 1000 ",Customer.FLD_CUSTID);
    Customer[] customers = Customer.queryDB(conn, whereClause);

將欄位名稱轉換成 Java 碼中的屬性,有二個好處:
  1. 在撰寫程式時,不用記憶欄位名稱,透過 IDE 的 Code Completion 就可以將欄位帶出來。
  2. 當欄位名稱有異動時,在重新產出 Java 碼後,該屬性名稱也會跟著變動。若程式未作相對應的修改時,則會有編譯錯誤產生,可以強迫設計師修正該問題。
Note:
若是將欄位名稱包在字串中(eg: whrStr = "select * from customer where custid < 1000"),當改變欄位名稱為 customer_id 時,上述的程式碼可能未被找出並作相對應的修正。等到使用者用到該項功能後,發現該功能的運作結果不正確後,才發現 Bug。

  • 自動遞增的流水號欄位:TOP
在 MySQL 和 MSSQL 中,可以定義某一個欄位為自動遞增的流水號欄位。Table Object 在產出程式碼時,會判斷該欄位是否為自動遞增的流水號欄位,並將該欄位註記在 FLD_AUTO_INCREMENT 屬性中。
當使用者透過 Table Object 產出的程式作新增時,Table Object 會從資料庫取得本次新增的流水號並將該值回填到該欄位。
使用方式如下:
    Cust cust = new Cust();    // custid 為 自動遞增的流水號欄位
    cust.setName("John");
    cust.setAge(10);
    cust.insertIntoDB(conn);    //  新增一筆資料到資料庫
    int custId = cust.getCustid();  //  取出資料庫所配發的流水號


  • 取出及設定對 應的欄位屬性:TOP
  • 取出欄位屬性: 
Table Object 會產出對應的方法,如:
public Long getCustid()
其回傳型態會依據表格內欄位的型態而決定。
  • 設定欄位屬性: setXXX(
Table Object 會產出對應的方法,如:
public void setCustid(Long custid)
其傳入參數型態會依據表格內欄位的型態而決定。

  • 查詢資料庫的方 式:TOP
在產出 Java 碼中,所提供的查詢資料庫方式有下列數種(以 MyCust 表格為例):
  • public static Mycust queryFirstRow(Connection conn,String whereStr)
若在 whereStr 的條件下可以查到一筆以上的資料時,則回傳第一筆資料。若無資料時則回傳 null。
  • conn: JDBC 的 Connection Object。
  • whereStr:本字串會直接貼附在產出的 SQL 之後,可能的用法為 "where columnA > 100 " 或者 "order by columnA"
  • public static Mycust[] queryDB(Connection conn,String whereStr)
若在 whereStr 的條件下可以查到一筆以上的資料時,則將所有的資料包裝成陣列,並將其回傳。若無資料時,則回傳空陣列。
  • conn: JDBC 的 Connection Object。
  • whereStr:本字串會直接貼附在產出的 SQL 之後。
  • public static Mycust queryFirstRow(Connection conn,String whereStr,ArrayList pStmtParamAL)
回傳第一筆資料。若無資料時則回傳 null。
  • conn: JDBC 的 Connection Object。
  • whereStr:本字串會直接貼附在產出的 SQL 之後。
  • pStmtParamAL:PrepareStatement 的參數陣列
Note:Table Object 內部的運作方式是動態產出 SQL 語句,然後將 whereSt 字串貼附在其後,最後將合成的 SQL 創建出 PreparedStatement 物件。換句話說,你可以在 whereStr 內加入 PreparedStatment 的參數。用法如下:
String whereStr = "where custId = ? and birthday = ? order by custId ";
ArrayList pStmtParamAL = new ArrayList();
pStmtParamAL.add(new Long(1000));
pStmtParamAL.add(new Timestamp(...));
Mycust mycust = Mycust.queryFirstRow(conn, whereStr, pStmtParamAL);
  • public static Mycust[] queryDB(Connection conn,String whereStr,ArrayList pStmtParamAL)
回傳查詢結果陣列。若無資料時,則回傳空陣 列。
  • conn: JDBC 的 Connection Object。
  • whereStr:本字串會直接貼附在產出的 SQL 之後。
  • pStmtParamAL:PrepareStatement 的參數陣列
  • public static Mycust[] queryDB(Connection conn,String whereStr, int maxRows)
回傳查詢結果陣列,但該陣列最大不會超過 maxRows, 並將其回傳。若無資料時,則回傳空陣列。
  • conn: JDBC 的 Connection Object。
  • whereStr:本字串會直接貼附在產出的 SQL 之後。
  • maxRows:回傳陣列的最大元素數目
Note:maxRows 的處理方式為每次 呼叫 resultSet.next() 時,判斷呼叫的次數是否達到該上限值,若為真,則跳出迴圏不再往向下讀取。
  • public static Mycust[] queryDB(Connection conn,String whereStr,ArrayList pStmtParamAL , int maxRows)
回傳查詢結果陣列。若無資料時,則回傳空陣 列。
  • conn: JDBC 的 Connection Object。
  • whereStr:本字串會直接貼附在產出的 SQL 之後。
  • maxRows:回傳陣列的最大元素數目
  • pStmtParamAL:PrepareStatement 的參數陣列
  • public static Mycust[] queryDB(Connection conn,String colStr,String whereStr, ArrayList pStmtParamAL)
回傳查詢結果陣列。若無資料時,則回傳空陣 列。
  • conn: JDBC 的 Connection Object。
  • whereStr:本字串會直接貼附在產出的 SQL 之後。
  • maxRows:回傳陣列的最大元素數目
  • pStmtParamAL:PrepareStatement 的參數陣列
  • colStr:指定那些欄位的資料需要讀出。
Note:在某些情況下,我們不需將所有的欄 位都擷取出來。例如,你只需處理一二個欄位,但該表格中確有一百多個欄位,多餘的欄位資料的傳輸會浪費網路資源。
其設定範例如下:
String colStr=String.format("%s, %s", Mycust.FLD_CUDTID, Mycust.FLD_BIRTHDAY);
  • public static Mycust[] queryDB(Connection conn,String colStr,String whereStr, ArrayList pStmtParamAL, int maxRows)
回傳查詢結果陣列。若無資料時,則回傳空陣 列。
  • conn: JDBC 的 Connection Object。
  • colStr:指定那些欄位的資料需要讀出。
  • whereStr:本字串會直接貼附在產出的 SQL 之後。
  • maxRows:回傳陣列的最大元素數目
  • pStmtParamAL:PrepareStatement 的參數陣列
  • maxRows:回傳陣列的最大元素數目

  • 關於 PRIMARY KEY:TOP
Table Object 在設計時是假設所有表格一定有設定主鍵。而 Table Object 也會根據主鍵來產出相關的查詢方式:
  • public static Mycust queryByPK(Connection conn, Long custid)
根據主鍵值回傳查詢結果,若該主鍵對應不到資 料,則回傳 null。
  • conn: JDBC 的 Connection Object。
  • custid:主鍵值。
Note:若為複合式主鍵,則會列出所有主鍵 的欄位。
  • public static Mycust queryByPK(Connection conn, String colStr, Long custid)
根據主鍵值回傳查詢結果,若該主鍵對應不到資 料,則回傳 null。
  • conn: JDBC 的 Connection Object。
  • colStr:指定那些欄位的資料需要讀出。
  • custid:主鍵值。

  • 關於 FOREIGN KEY:TOP
Table Object 遇到 Foreign Key 時,會產出相對應的查詢方式。例如, Mycust 代表客戶,而 Myorder 代表訂單。在 Myorder 有一個欄位為 custid ,該欄位為 Foreign Key (指向 Mycust)。則會產出下列的查詢方式:
  • public static Myorder[] queryByMycust(Connection conn, Mycust obj)
可根據傳入的 Mycust (客戶物件) 來反查和其相關的 Myorder[] (訂單物件陣列)。
  • conn: JDBC 的 Connection Object。
  • obj:Foreign Key 所對應到的 Table Object。
  • public static Myorder[] queryByMycust(Connection conn, String colStr,Mycust obj)
可根據傳入的 Mycust (客戶物件) 來反查和其相關的 Myorder[] (訂單物件陣列)。
  • conn: JDBC 的 Connection Object。
  • colStr:指定那些欄位的資料需要讀出。
  • obj:Foreign Key 所對應到的 Table Object。
  • public static Myorder[] queryByMycust(Connection conn, String colStr,Mycust obj, String orderStr)
可根據傳入的 Mycust (客戶物件) 來反查和其相關的 Myorder[] (訂單物件陣列)。
  • conn: JDBC 的 Connection Object。
  • colStr:指定那些欄位的資料需要讀出。
  • obj:Foreign Key 所對應到的 Table Object。
  • orderStr:Myorder[] 內資料的排序方式。
Note:Table Object 會根據 Mycust 產出對應的查詢 SQL ,然後將 orderStr 貼附在該 SQL 之後,其主要的目的是讓該 SQL 有 ORDER BY 敍述。
可能的用法如下:
String orderStr = String.format("order by %s ",Myorder.FLD_ORDERID);

  • 新增到資料庫:TOP
透過 Table Object 新增一筆資料到資料庫的方法如下:
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);
  • 將資料刪除:TOP
若要將一筆資料作刪除時,則需先將該筆資料取 出,方法如下:
Customers customer = Customers.queryByPK(conn, 1000);
customer.deleteFromDB(conn);
  • 更新資料:TOP
若要將一筆資料作異動時,則需先將該筆資料取 出更改完成後,再寫回到資料庫中。
更新的方法有二個,分別如下:
  • public boolean updateDB(Connection conn)
該方法會將該 Table Object 物件內所有的欄位值更新到資料庫中。
  • public boolean updateDB(Connection conn,String... colStrs)
該方法只會將你指定的欄位值更新到資料庫中。