Jython Basics - Article 1
Simple Java-Based Data Access Model Using Oracle
Submitted By: Josh Juneau
Article Date: 04/15/2008
Almost every application written today requires data access of some kind. The most common forms of data access today are based upon selection, update, delete processes against a database or XML. While data access has been around for many years now and may be quite trivial, it is an essential concept for any programming language. Not only is it important to know the concepts behind data access, but it is also important to adhere to the basics of object orientation while doing so. A well formed application will use objects to pass data around, which makes it more flexible and easy to maintain.
In this short article, I will quickly walk through the basics of setting up and using a simple data access model based upon well known Java models. Please note that this model is incomplete because it only touches upon the beginnings of creating such a model. Most Java applications use interfaces to access data from a controller class, whereas this simple model leaves that concept out completely.
Setting Up the Database
I'll create a simple object for our Oracle database, although this same process can be used with almost every other RDBMS just as efficiently. If you don't have access to an Oracle database server, then I recommend downloading Oracle Database Express edition for free.
Nothing new here at all...I want to create a database table and insert some data. In a real world application the table would definitely include more fields, but you should get the idea.
create table person( id number, first varchar2(30), last varchar2(50), constraint person_pk primary key (id)); insert into person values(1, 'PETER','PARKER'); insert into person values(2, 'MARY','JANE'); insert into person values(3, 'HARRY','OSBORNE');
Create An Object Container
I want to create a simple Jython object to use for passing these person data rows around. I'll create a POJO "Plain Old Jython Object" for this purpose. The structure of this model is as follows.
org.jythonMonthly.bean - Object Container Package
org.jythonMonthly.data - Data Access Objects
org.jythonMonthly.control - Data Controller Classes
# File: Person.py # Package: org.jythonMonthly.data import java from org.jythonMonthly.data import PersonDA # Person object for passing the data around class Person(java.lang.Object): def __init__(self, id, first, last): self.id = id self.first = first self.last = last def getId(self): return self.id def getFirst(self): return self.first def getLast(self): return self.last def setId(self): self.id = id def setFirst(self): self.first = first def setLast(self): self.last = last def toString(self): return self.first + " " + self.last
Data Access Object
The data access object performs all of the database work in most Java apps. Similarly, we can implement the same type of object with Jython...less coding too! I chose to use a Java prepared statement for the data access in this example.
# File: PersonDA.py # Package: org.jythonMonthly.data import java from java.util import ArrayList from oracle.jdbc.driver import OracleDriver from java.sql import DriverManager from org.jythonMonthly.bean import Person class PersonDA(java.lang.Object): def __init__(self, un, pw, sid, host, port): driver = OracleDriver() DriverManager.registerDriver(driver) connection_string = "jdbc:oracle:thin:@%s:%s:%s" % (host, port, sid) self.connection = DriverManager.getConnection(connection_string, un, pw) # This method is used for searching the person table def findPersonByName(self, person): sql = "SELECT * FROM PERSON WHERE FIRST = UPPER(?) OR LAST = UPPER(?)" stmt = self.connection.prepareStatement(sql) stmt.setString(1, person.getFirst()) stmt.setString(2, person.getLast()) personList = ArrayList() rset = stmt.executeQuery(); while (rset.next()): id = rset.getInt("id") first = rset.getString("first") last = rset.getString("last") person = Person.Person(id,first,last) personList.add(person) stmt.close() self.connection.close() return personList # This method is an example for saving values into our database table def persist(self, person): sql = "INSERT INTO PERSON VALUES (?,?,?)" stmt = self.connection.prepareStatement(sql) stmt.setInt(1,person.getId()) stmt.setString(2, person.getFirst()) stmt.setString(3, person.getLast()) stmt.executeUpdate() stmt.close() self.connection.close()
Using the Data
Once that these two objects have been defined, it is easy to create a facade or controller class to obtain and use the data. This controller class can then be used to invoke methods on the data access object from ant desktop or web based application. Here is a simple controller example. While it does not effectively do much, hopefully you can get the idea of how to write something that will be more useful in a real world application.
# PersonController.py # Package: org.jythonMonthly.controller import java from java.util import ArrayList from org.jythonMonthly.bean import Person from org.jythonMonthly.data import PersonDA class PersonController(java.lang.Object): def __init__(self): self.data = PersonDA.PersonDA("USER","PASSWORD","SID","HOST","PORT") def findPerson(self, search): person = Person.Person(search.id, search.first, search.last) #personArray = ArrayList() personArray = self.data.findPersonByName(person) return personArray def save(self, person): self.data.persist(person) # Initiate the process using this main method # First, we will create a new person object and # populate it with the person whom we'd like to find. # Next, we will pass that person object to the controller.findPerson method # and return a list of matching objects. if __name__ == "__main__": person = Person.Person(1, 'none','PARKER') controller = PersonController() personList = controller.findPerson(person) for x in range(0,personList.size()): person = personList.get(x) print person.toString()
All in all, this is an old and simple recipe for obtaining data in software development. If you don't follow a model such as this, your application can become unscalable and hard to maintain. Hopefully this article will help some Jython newcomers in the data realm. Next month, I will choose another basic concept to explain.