Simple DB Object Builder |
|||||||||||
|
Table Object 快速上手在本次的說明中,我們將會作如下的動作:
專案名稱為 TEST01
,專案路徑則自行選定。
開啟
Netbeans->Windows->Services 。
DB Obejct Builder 目前可以支援三種資料庫:Oracle, MSSQL 和 MySQL,在本範例中,我們是採用 MySQL 資料庫。 建立的連線如下:
按下 Generate Java Source,則會見到在指定的目錄下見到新產出的檔案:
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)如下:
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方法,就可將資料新增到資料庫中。
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)。
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物件後,就可以對其作刪除資料。
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物件後,就可以對其作資料異動,異動結束後,將其寫回到資料庫中。 上述的寫回資料庫方法是會針對 所有的欄位都作異動。
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 這二個欄位。 |
||||||||||