A Reflection on Implementation

Recall how we need to fill in the attributes one by one using PreparedStatement:

Author a = new Author ("Franz Kafka", 16, "Czechoslovakian");
String sql = "INSERT INTO Authors (id, name, numOfBooks, nationality)" +
"VALUES (NULL, ?, ?, ?);";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1, a.getName());
pst.setString(2, String.valueOf(a.getNumOfBooks()));
pst.setString(3, a.getNationality());

The above peice of code is, in a way, a mapping between an Author object and a record in the Authors table.

To map an object into a record, it requires some work to get the value of each attribute and set it in the corresponding column in the table. Imagine if Authors had many more fields; it seems like there must be a better way to map an object into a record (in a relational database); someone must have had this problem and thought of writing a library to solve this problem (at least as long as the fields - in a class - correspond directly to the columns in a table)!

Well, you will not be surprised to learn there exist libraries called Object Relational Mapping (ORM) to solve this problem. We will next explore a simple ORM called Sql2o.


Inserting instance field values one by one into a SQL query is not only mechanical, but also fragile. If we add a new field to the Author class, we need to modify the above parameterized statement to accommodate for the newly added parameter.