
ollin
Ollin offers a fluent API over JDBC to simplify the development of applications using databases.
Gives some other benefits: * Ollin receives a DataSource and takes care of creating and closing the connections. * You can use rollback and commit. * Reduces the amount the code. That could help in the maintainability of your programs.
This is an example of Ollin.
DataSource dataSource = //can get a dataSource via JNDI, inyection, DBCP or some other way.
DBSession dbSession = new DBSession(dataSource);
Integer count=dbSession.createQuery("select count(*) from APP.CUSTOMER").get();
System.out.println("row count2: "+count);
Another example that prints all rows from a query.
//RowPrinter is a kind of ResultSet iterator that is included in Ollin. It prints each row in a simple format.
dbSession.createQuery("select * from APP.EMPLOYEE").forEachRow(new RowPrinter());
Ollin has update (insert, update, delete) operations too:
dbSession.createUpdate("insert into app.person(id,name) values(99, 'X')").execute();
An example with transactions:
dbSession.startTransaction();
try{
dbSession.createUpdate("insert into app.person(id,name) values(99, 'X')").execute();
dbSession.createUpdate("insert into app.employee(employee_id,name) values(100, 'Y')").execute();
dbSession.commitTransaction();
}catch(Exception ex){
dbSession.rollbackTransaction();
throw ex;
}
Query the database
Query uses an approach of visitor based in rows. The result set is exposed to the visitor as if were a row (only operations related to the row are exposed, but you can still access the ResultSet object if you want).
The method forEachRow initiates the iteration. The connection is open when the request to visit the rows is made and is always closed after iterating all the rows (even if a exception occurs while iterating or calling the visitor). All the iteration is made internally by the Ollin API and, in every row, a call to the visitor is made.
The forEachRow method return the RowVisitor so you can chain the calls. A especial sub interface, ValuedRowVisitor, is available so you can return a value after iterating the rows (such return a collection of object represented in each row, a counter, etc.).
This is an example of counting rows "manually":
``` ValuedRowVisitor rowCounter=dbSession.createQuery("select * from app.employee").forEachRow(new ValuedRowVisitor() { private int counter;
public Integer getValue() {
return counter;
}
public void visit(ResultSetRow row) throws SQLException {
counter++;
}
}); System.out.println("Count of rows: "+rowCounter.getValue()); ```
And this is an example of retreiving of objects.
``` //the bean class public class Employee{ private String id; private String name;
public Employee(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
}
ValuedRowVisitor> rowVisitor=new ValuedRowVisitor>(){ private List employees=new LinkedList();
public List<Employee> getValue() {
return employees;
}
public void visit(ResultSetRow row) throws SQLException {
employees.add(new Employee(row.getString(1), row.getString(2)));
}
};
dbSession.createQuery("select employee_id, name from app.employee").forEachRow(rowVisitor); List employees=rowVisitor.getValue();
//or using chainability employees=dbSession.createQuery("select employee_id, name from app.employee").forEachRow(rowVisitor).getValue(); ```
Project Information
- License: Apache License 2.0
- hg-based source control