Jython Basics - Article 1

Simple Java-Based Data Access Model Using Oracle

Submitted By: Joshua Juneau

Article Date: 04/15/2008

Introduction

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 code.

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. 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