package net.titaniclinux.daogen.examples; import java.io.*; import java.sql.*; import java.util.*; /** * This sourcecode is a programming example for the DaoGen generator. * The usage of generated code is restricted to OpenSource software projects * only. DaoGen is available in http://titaniclinux.net/daogen/ * * DaoGen license: The following DaoGen generated source code is licensed * under the terms of GNU GPL license. The full text for license is available * in GNU project's pages: http://www.gnu.org/copyleft/gpl.html */ public class Example1 { public static void main(String[] args) { System.out.println("DaoGen Programming Example1: Batch Run Program\n"); // Database Connection information. In real applications the database // connections are usually pooled and these connection parameters are // not hard-coded, but instead configurable in properties files or // web.xml files. However, for simplicity we keep them here now. String jdbcDriver = "jdbc:mysql"; String jdbcUrl = "localhost/test"; String jdbcLogin = "username"; String jdbcPasswd = "password"; Connection conn = null; // Create the DAO component instance. This object can be created // easily, since the constructor takes no arguments. We will need // the Database resources (Connection) only when we want to actually // use this DAO for something. Note: All DAO classes that are generated // with DaoGen are thread safe! This means, that for multi threaded // programs (like HttpServlets) you need to create only one instance // and you can then share it for all requests (threads). The advantage // of doing so is that your programs will use less memory. CustomerDao customerHandler = new CustomerDao(); try { System.out.println("Creating database connection:"); // Connect to database. We need only one connection, -> no pooling here. // If you do not use MySQL database, you need to change the MySQL driver // to your own JDBC database driver. (See the line below:) DriverManager.registerDriver(new org.gjt.mm.mysql.Driver()); // Now we use the DriverManager to get the connection to Database: conn = DriverManager.getConnection (jdbcDriver + "://" + jdbcUrl, jdbcLogin, jdbcPasswd); System.out.println("Database connection ready.\n"); } catch (Exception error) { System.out.println("Database connection can not be created!"); System.out.println("Error description: " + error.toString()); System.exit(-1); } // Next, we proceed to actual database handling. // We will use the DaoGen generated functionality. try { // Customer valueObject reference: Customer tempCustomer = null; // Prepare the Date object, we will need it multiple times: java.sql.Date currentDate = new java.sql.Date(System.currentTimeMillis()); // First, we delete all records from the customer table: customerHandler.deleteAll(conn); // Now, just create the new records first as // objects and then store them in DB: // This is the simplest way for creating and filling the value object: tempCustomer = new Customer(1); tempCustomer.setName("Scooby Doo"); tempCustomer.setAddress("DaoGen street 1A"); tempCustomer.setCreated(currentDate); tempCustomer.setBalance(100); // Create the object in database (This will excecute SQL INSERT command): customerHandler.create(conn, tempCustomer); // But you can also fill the value objects in a more compact way: tempCustomer = new Customer(); tempCustomer.setAll(2, "George Bush", "The White House", currentDate, 200); customerHandler.create(conn, tempCustomer); tempCustomer = new Customer(); tempCustomer.setAll(3, "Nemo, the Fish", "Pasific Ocean", currentDate, 500); customerHandler.create(conn, tempCustomer); tempCustomer = new Customer(); tempCustomer.setAll(4, "Aku Ankka", "Ankkalinna", currentDate, 0); customerHandler.create(conn, tempCustomer); } catch (SQLException error) { // SQLException means problems. Generated DAO classes will not // throw SQLExceptions unless they are thrown directly from JDBC drivers // (or something goes really wrong with the database updates). These are // almost allways that kind of errors where your application should at // least write the error.toString() to log file and possibly exit program. System.out.println("SQL Error occurred when handling the database!"); System.out.println("Error description: " + error.toString()); } finally { // This block is really important step in Java Database proramming and it // the source for most errors in all applications. With Java being // automatically garbage collecting all "left over" objects when they are // no longer used, programmers tend to forget that this automatical cleanup // does not work as supposed when using resources outside the Java VM. // Database connection is much more than just one network connection from // Java VM to the database server. The connection is actually reserving // resources from the Database server (cpu, memory, possibly locks in DB) // and you must never count on Java VM to cleanup those resources. To // make things easier for you, DaoGen will be extra careful to close all // PreparedStatement- and Resultset objects for you. However, since the // Connection level (creating and destroying connections) is not handled // in DaoGen, we will need need to make sure to close connection after // we are done. try { conn.close(); } catch (Exception ignore) { } } } }