Database Access with Jython, Hibernate and SQLAlchemy - Oracle
Database Access with Jython, Hibernate and SQLAlchemy - Oracle
Database Access with Jython, Hibernate and SQLAlchemy - Oracle
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
<strong>Database</strong> <strong>Access</strong> <strong>with</strong> <strong>Jython</strong>, <strong>Hibernate</strong> <strong>and</strong><br />
<strong>SQLAlchemy</strong><br />
Frank Wierzbicki, <strong>Jython</strong> Project Lead<br />
http://www.jython.org
Why <strong>Jython</strong>?<br />
<strong>Jython</strong> brings the Python language to the JVM technology<br />
Python is a very mature language which was designed to be<br />
easy to pick up but powerful to use<br />
<strong>Jython</strong> has full <strong>and</strong> nearly seamless integration into any Java<br />
platform <strong>and</strong> code<br />
<strong>Jython</strong> can access many of the libraries <strong>and</strong> frameworks<br />
written in Python<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 2
Basic <strong>Jython</strong> Demo<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 3
DB-API, JDBC API, <strong>and</strong> <strong>Jython</strong><br />
<strong>Jython</strong> has built in support for DB-API, Python's st<strong>and</strong>ard<br />
database interface in the zxJDBC package<br />
zxJDBC is a thin wrapper around JDBC API, Java platform's<br />
st<strong>and</strong>ard database interface<br />
Python programmers get access to any database <strong>with</strong> a JDBC<br />
API driver<br />
Java platform programmers get access to any Python<br />
frameworks that are built on DB-API<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 4
Basic Connection<br />
from com.ziclix.python.sql import zxJDBC<br />
try:<br />
cursor = None<br />
db = None<br />
try:<br />
db = zxJDBC.connect("jdbc:mysql://localhost/test",<br />
'user', 'pass', "org.gjt.mm.mysql.Driver")<br />
cursor = db.cursor()<br />
cursor.execute("select name from user")<br />
for row in cursor.fetchall():<br />
print row<br />
finally:<br />
if cursor is not None:<br />
cursor.close()<br />
if db is not None:<br />
db.close()<br />
except Exception, reason:<br />
print reason<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 5
Connection from DataSource<br />
#Use connectx if you are connecting to a DataSource.<br />
#This is often useful when you are working from an<br />
#application server that uses connection pooling<br />
from com.ziclix.python.sql import zxJDBC<br />
dbInfo = {'user':'username', 'password':'pass',<br />
'mydb':'test', 'server':'localhost',<br />
'port':3306}<br />
con = zxJDBC.connectx("org.gjt.mm.mysql.MysqlDataSource",<br />
**dbInfo)<br />
#Or you can connect via JNDI<br />
factory = "com.sun.jndi.fscontext.RefFSContextFactory"<br />
db = zxJDBC.lookup('context/myDataSource'),<br />
INITIAL_CONTEXT_FACTORY=factory)<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 6
Using Swing Tables From <strong>Jython</strong><br />
from javax.swing import JTable<br />
from javax.swing import JFrame<br />
rowdata = [('bill', 'Bill Williams')]<br />
colnames = ['user name', 'full name']<br />
table = JTable(rowdata, colnames)<br />
frame = JFrame("Table")<br />
frame.getContentPane().add( table )<br />
frame.size = 400, 300<br />
frame.visible = 1<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 7
Basic zxJDBC Demo<br />
DB-API <strong>and</strong> JDBC API taste great together<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 8
Object Relational Mappings (ORMs)<br />
H<strong>and</strong>ling raw SQL in object oriented programs can feel<br />
tedious for many programming tasks<br />
Object Relational Mappers help make Table based data feel<br />
more like objects from an OO style program<br />
Two styles of ORM in common use: Active Record <strong>and</strong> Mapper<br />
H<strong>and</strong>ling raw SQL in object oriented programs can feel<br />
tedious for many programming tasks<br />
Object Relational Mappers help make Sun <strong>Access</strong> Table<br />
Editor (Table) software based data feel more like objects<br />
from an OO style program<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 9
Active Record<br />
Active Record style ORMs tend to be simpler to use<br />
Generally there is a one to one mapping between tables <strong>and</strong><br />
objects<br />
Very easy to reason about the relationships between objects<br />
<strong>and</strong> tables<br />
Django uses an active record pattern<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 10
Django Demo<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 11
Data Mapper<br />
Data Mapper style ORMs are more “DBA friendly – DBAs don't<br />
like 1:1 object:table<br />
The mapping between objects <strong>and</strong> tables needs to be more<br />
explicit<br />
Data Mappers tend to be more flexible <strong>and</strong> can be used for<br />
harder problems (for example: legacy databases that can't be<br />
made to fit ActiveRecord)<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 12
<strong>Hibernate</strong><br />
As if <strong>Hibernate</strong> needs an introduction here :)<br />
<strong>Hibernate</strong> is a native Java platform ORM based on Data<br />
Mapper<br />
Very mature, very stable technology<br />
Has a very good reputation<br />
Has a huge install base<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 13
<strong>Hibernate</strong> From <strong>Jython</strong> Demo<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 14
<strong>SQLAlchemy</strong><br />
Data Mapper style ORMs are more “DBA friendly – DBAs don't<br />
like 1:1 object:table<br />
The mapping between objects <strong>and</strong> tables needs to be more<br />
explicit<br />
Data Mappers tend to be more flexible <strong>and</strong> can be used for<br />
harder problems (for example: many existing databases that<br />
can't be made to fit ActiveRecord)<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 15
<strong>SQLAlchemy</strong> Demo<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 16
For More Information<br />
http://www.python.org<br />
http://wiki.python.org/jython<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 17
Frank Wierzbicki<br />
http://www.jython.org<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 18
Title: Sun Sans Semibold; 34pt Max,<br />
30 pt Min; Two Lines Max<br />
Level One bullet point<br />
Sun Sans Regular, 24 pt maximum size; 16 pt minimum<br />
• Level Two bullet<br />
• 20 pt Sun Sans Regular<br />
• Level three: 18pt<br />
• Level four: 18pt<br />
• Level five: 18 pt<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 19
Dual Text Blocks<br />
Subhead: Sun Sans Semibold, 26 pt max, 22 pt min.<br />
Level One bullet point<br />
20 pt<br />
• Level Two: 18 pt<br />
• Level Three:16 pt<br />
• Level Four:16 pt<br />
Level One bullet point<br />
20 pt<br />
• Level Two:18 pt<br />
• Level Three:16 pt<br />
• Level Four:16 pt<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 20
Learn how to architect <strong>and</strong> build<br />
3-tier business systems using<br />
Enterprise JavaBeans (EJB) technology<br />
[sample goal statement—replace <strong>with</strong> your own; this slide appears directly after<br />
your title slide]<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 21
Agenda<br />
Agenda item one [use this color AND font to highlight agenda items]<br />
Agenda item two<br />
Agenda item three<br />
Agenda item four<br />
Agenda item five<br />
And so on<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 22
Code Sample<br />
Code<br />
Recommended font size for short blocks of code.<br />
Font used here is Courier New Bold at 20 points<br />
{<br />
apply orange to code you want to discuss or<br />
highlight<br />
}<br />
If you have room, feel free to enlarge the type for<br />
additional readability<br />
}<br />
Other Notes for Code<br />
Recommended font size for short blocks of code.<br />
Font used here is Courier New Bold at 20 points<br />
{<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 23
Video Title<br />
Video Company, etc.<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 24
Demo Title<br />
Demo Company, etc.<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 25
Summary<br />
Summary point one<br />
Summary point two<br />
Summary point three<br />
Summary point…<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 26
For More Information<br />
List<br />
• Cross-references to other sessions<br />
• BOFs<br />
• URLs<br />
• Related books, etc.<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 27
Default Color Palette<br />
Primary Colors<br />
Sun Blue<br />
Sun Orange<br />
Sun Green<br />
Sun Yellow<br />
Sun Gray<br />
Secondary Colors<br />
Sun Dark Blue<br />
Sun Dark Orange<br />
Sun Dark Green<br />
Sun Dark Yellow<br />
Sun Light Blue<br />
Sun Light Orange<br />
Sun Light Yellow<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 28
Sample Graphics<br />
Primary default color graphics<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 29
Sample Bar Chart Using Star Calc<br />
10<br />
9.5<br />
9<br />
8.5<br />
8<br />
7.5<br />
7<br />
6.5<br />
6<br />
5.5<br />
5<br />
4.5<br />
4<br />
3.5<br />
3<br />
2.5<br />
2<br />
1.5<br />
Source: [Please add the source of your data here]<br />
Chart Title<br />
Row 1 Row 2 Row 3 Row 4<br />
Column 1<br />
Column 2<br />
Column 3<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 30
Sample Bar Chart Using Microsoft Graph<br />
10<br />
9<br />
8<br />
7<br />
6<br />
5<br />
4<br />
3<br />
2<br />
1<br />
0<br />
Source: [Please add the source of your data here]<br />
Chart Title Chart<br />
Column 1 Column 2 Column 3 Column 4<br />
Row 1<br />
Row 2<br />
Row 3<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 31
Sample Pie Chart Using Star Calc<br />
Source: [Please add the source of your data here]<br />
2.4<br />
3.1<br />
Chart Title<br />
9.1<br />
4.3<br />
Row 1<br />
Row 2<br />
Row 3<br />
Row 4<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 32
Sample Pie Chart Using Microsoft Graph<br />
9.1<br />
Source: [Please add the source of your data here]<br />
Chart Title Here<br />
2.4<br />
4.3<br />
3.1<br />
Column 1<br />
Column 2<br />
Column 3<br />
Column 4<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 33
Sample Data Matrix<br />
Q2 ’03 % to Q2 ’02<br />
Units 1K + 1%<br />
Revenue $1,000 +1%<br />
Gross margin 1% + 1. 0 points<br />
OPEX $1 +1%<br />
Net income $1 +1%<br />
EPS $.01 +1%<br />
Source: [Please add the source of your data here]<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 34
Sample Table<br />
Heading<br />
Body Content<br />
Body Content<br />
Heading<br />
Body Content<br />
Body Content<br />
Heading<br />
Body Content<br />
Body Content<br />
Heading<br />
Body Content<br />
Body Content<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 35
Speaker Name, Speaker Title, 24 pt.<br />
ID#, Misc., 16 pt.<br />
Speaker’s logo here<br />
(optional)<br />
2008 JavaOne SM Conference | java.sun.com/javaone | 36