package ejp.examples; import com.mysql.jdbc.CallableStatement; import ejp.Database; import ejp.Database.InParameter; import ejp.Database.OutParameter; import ejp.DatabaseException; import ejp.DatabaseManager; import ejp.Result; import ejp.TransactionManager; import java.sql.SQLException; import java.sql.Types; import java.util.ArrayList; import java.util.Collection; /* Easy Java Persistence and JDBC Toolbox * * Easy Java Persistence (EJP) is a configuration and annotation free Java persistence Framework * that allows you to use normal (true) POJO classes to interact with your database. * * EJP is an A-O/RM (automatic object/relational mapping) persistence framework. Any class can * be mapped to data returned from queries provided it has methods that can be matched to columns * in the result data. And any class that can be matched to a table in your database can be mapped, * saved (insert), and persisted, allowing for more saves (updates). Objects don't belong to any * EJP instance, and they never expire. * * ejp.DatabaseManager is the starting point with EJP. ejp.DatabaseManager wraps functionality * from ejp.Database and ejp.Result to provide simple one-line of code methods to most of EJP's * functionality. ejp.Databasemanager is the highest and easiest level of EJP, while ejp.Database * and ejp.Result are the next highest level, and are also very easy to work with. * * ejp.DatabaseManager is thread safe when connection pooling is in use. ejp.Database is also thread * safe when connection pooling is used, and you make sure to use one ejp.Database (dbm.getDatabase()) * instance per thread. * * EJP also manages all associations automatically with collections (automatically lazy loaded), * arrays, and single instance objects. * * EJP is also a JDBC toolbox. EJP provides you the ability to do anything you can do with JDBC * and makes it a whole lot easier. You can perform simple and complex queries, EJP allows for * simple statements (executeQuery/executeUpdate) and/or prepared statements * (parameterizedquery/parameterizedUpdate). You can call stored procedures and handle multiple * results. */ @SuppressWarnings("unchecked") public class IntroducingEjp { /* ejp.DatabaseManager * * The main difference between ejp.DatabaseManager and ejp.Database is that ejp.DatabaseManager returns * objects and collections from queries, while ejp.Database returns ejp.Result (a database cursor) from * queries. * * ejp.Result is a bidirectional database cursor (wraps java.sql.ResultSet), and can be * used to iterate trough huge query result sets. ejp.Result also implements ListIterator and Iterable, * so you can use next() and previous(), and you can use ejp.Result in a foreach statement. next() and * previous() return newly created instances of whatever class is associated with the ejp.Result instance. * Since ejp.Result is a database resource, it needs to be closed as soon as you are done with it. All * ejp.Results are closed automatically when closing ejp.Database. * * There are multiple ways to obtain an ejp.DatabaseManager. You can define the following: * *
*
*
*
*
*
* in a file named "databases.xml" that's locatable in your classpath. You can call one of ejp.DatabaseManagers's
* getDatabasemanager() methods, or you can simply create a new instance of ejp.DatabaseManager.
*
* ejp.DatabaseManager excepts a name from the databases.xml file, a javax.sql.DataSource, a JNDI resource name, or driver and url information.
*/
static DatabaseManager getTheDatabaseManager()
{
// return DatabaseManager.getDatabaseManager("nameFromDatabase.xml");
// return DatabaseManager.getDatabaseManager("name", poolSize, dataSource);
// return DatabaseManager.getDatabaseManager("name", poolSize, jndiUrl);
// return DatabaseManager.getDatabaseManager("name", poolSize, "driver", "url", "user", "passwd");
// Returns a databases.xml defined database for HSQLDB
return DatabaseManager.getDatabaseManager("ejp_hsql");
}
/* Inserting and Updating
*
* Inserting and updating objects, with both ejpDatabaseManager and ejp.Database, is done with saveObject().
* SaveObject() always knows weather it's an insert or an update, so there's only one method for saving. You
* can also use executeUpdate() and parameterizedUpdate(), if you want to do it with plain SQL.
*/
static void insertingAndUpdatingObjects(DatabaseManager dbm) throws DatabaseException
{
Customer customer = new Customer("jjohnson", "mypasswd5", "John", "Johnson", "ABCProducts", "jjohnson@abcproducts.com");
customer.getOrders().add(new Order("NFL Denver Broncos Coffee Cup", 100, 1.00, "unverified"));
dbm.saveObject(customer);
customer = new Customer("ssmith", "mypasswd", "Scott", "Smith", "ABCProducts", "ssmith@abcproducts.com");
customer.getOrders().add(new Order("NFL Seattle Seahawks Team T-Shirts", 150, 12.00, "unverified"));
dbm.saveObject(customer);
customer.setCompanyName("ACB Products");
customer.getOrders().add(new Order("NFL Greenbay Packers Team T-Shirts", 150, 12.00, "verified"));
dbm.saveObject(customer);
// the old fashion way
dbm.parameterizedUpdate("insert into customers (customer_id, password, first_name, last_name, company_name, email) values(?,?,?,?,?,?)",
"jstevens", "mypasswd5", "John", "Stevens", "ABCProducts", "jstevens@abcproducts.com");
dbm.parameterizedUpdate("insert into orders (customer_id, product, quantity, price, status) values(?,?,?,?,?)",
"jstevens", "NFL Dallas Cowboys Coffee Cup", 100, 1.00, "unverified");
}
/* Queries
*
* Retrieving data from your database is done with loadObject() for a single object, and loadObjects()
* for a collection. You can also use executeQuery(), and parameterizedQuery().
*/
static void databaseManagerQueries(DatabaseManager dbm) throws DatabaseException
{
Customer customer = dbm.loadObject(new Customer("jjohnson"));
System.out.println(customer);
customer = dbm.loadObject(Customer.class, "where customer_id = ?", "jjohnson");
System.out.println(customer);
for (Customer c : dbm.loadObjects(new ArrayList
* {?= call [(?, ?, ?, ...)]}
* {call [(?, ?, ?, ...)]}
*
*
* and one or more of InParameter, OutParameter, and InOutParameter. storedprocedure()
* returns a Result that may have one or more results and/or update counts that can be
* accessed with getMoreResults(), isUpdateCount(), getUpdateCount().
*
* When done, you can access the out parameters by retrieving the CallableStatement with
* getStatement().
*/
static void storedProcedures(DatabaseManager dbm) throws DatabaseException, SQLException
{
Database db = dbm.getDatabase();
try
{
Result result = db.storedProcedure("{call getCustomerOrdersAndSupport(?,?)}",
new InParameter(1, "deisenhower"),
new OutParameter(2, Types.DOUBLE, 2));
// Map orders from the orders cursor
for (Order order : (Result