This example shows how to use EasyORM to add records to a database. The example below inserts 2 records (related) into the Address and Employee tables respectively. This involves first creating the 2 objects, and then calling the insert method on them. Notice, however, that the code below uses transaction management (dbTrx object), which means that we must call the commit() method to make the insertions persistent.

Two record inserts using transaction control:


ConnectionPool connPool = ConnectionPool.getInstance(jdbcDriver, jdbcURL, user, password);
DBTransaction dbTrx = new DBTransaction(connPool);
//creating an address object
//by passing dbTrx (DBTransaction) to the constructor we make the Address object a part of the current trasaction
AddressDB address = new AddressDB(dbTrx);
address.setCity(request.getParameter("city").trim());
address.setStreetName(request.getParameter("street_name").trim());
address.setStreetNumber(request.getParameter("street_num").trim());
address.setDateAdded(sqlDate);
address.setDateUpdated(sqlDate);
address.insertAndReturnGenKey();

//creating an employee object
EmployeeDB employee = new EmployeeDB(dbTrx);
employee.setName(request.getParameter("name").trim());
employee.setSurame(request.getParameter("surname").trim());
employee.setYearsAtWork(Integer.parseInt(request.getParameter("years").trim()));
employee.setDepartment(request.getParameter("dept").trim());
employee.setDateAdded(sqlDate);
employee.setDateUpdated(sqlDate);
employee.setAddressId(address.getId());
employee.insert();

//the actual insertions will not take place until we call commit()
dbTrx.commit();

Run this example