Database Access with Jython, Hibernate and SQLAlchemy
Frank Wierzbicki, Jython Project Lead
http://www.jython.org
2008 JavaOneSM Conference | java.sun.com/javaone | 2
Why Jython?
Jython brings the Python language to the JVM™ technologyPython is a very mature language which was designed to be easy to pick up but powerful to useJython has full and nearly seamless integration into any Java™ platform and codeJython can access many of the libraries and frameworks written in Python
2008 JavaOneSM Conference | java.sun.com/javaone | 4
DB-API, JDBC™ API, and Jython
Jython has built in support for DB-API, Python's standard database interface in the zxJDBC packagezxJDBC is a thin wrapper around JDBC API, Java platform's standard database interfacePython programmers get access to any database with a JDBC API driverJava platform programmers get access to any Python frameworks that are built on DB-API
2008 JavaOneSM Conference | java.sun.com/javaone | 5
Basic Connectionfrom com.ziclix.python.sql import zxJDBCtry: cursor = None db = None try: db = zxJDBC.connect("jdbc:mysql://localhost/test", 'user', 'pass', "org.gjt.mm.mysql.Driver") cursor = db.cursor()
cursor.execute("select name from user") for row in cursor.fetchall(): print row finally: if cursor is not None: cursor.close() if db is not None: db.close()except Exception, reason: print reason
2008 JavaOneSM Conference | java.sun.com/javaone | 6
Connection from DataSource#Use connectx if you are connecting to a DataSource.#This is often useful when you are working from an#application server that uses connection pooling
from com.ziclix.python.sql import zxJDBCdbInfo = {'user':'username', 'password':'pass', 'mydb':'test', 'server':'localhost', 'port':3306}con = zxJDBC.connectx("org.gjt.mm.mysql.MysqlDataSource", **dbInfo)
#Or you can connect via JNDIfactory = "com.sun.jndi.fscontext.RefFSContextFactory"db = zxJDBC.lookup('context/myDataSource'), INITIAL_CONTEXT_FACTORY=factory)
2008 JavaOneSM Conference | java.sun.com/javaone | 7
Using Swing Tables From Jython
from javax.swing import JTablefrom javax.swing import JFrame
rowdata = [('bill', 'Bill Williams')]colnames = ['user name', 'full name']table = JTable(rowdata, colnames)frame = JFrame("Table")frame.getContentPane().add( table )frame.size = 400, 300frame.visible = 1
2008 JavaOneSM Conference | java.sun.com/javaone | 8
Basic zxJDBC DemoDB-API and JDBC API taste great together
2008 JavaOneSM Conference | java.sun.com/javaone | 9
Object Relational Mappings (ORMs)
Handling raw SQL in object oriented programs can feel tedious for many programming tasksObject Relational Mappers help make Table based data feel more like objects from an OO style programTwo styles of ORM in common use: Active Record and MapperHandling raw SQL in object oriented programs can feel tedious for many programming tasksObject Relational Mappers help make Sun Access Table Editor™ (Table) software based data feel more like objects from an OO style program
2008 JavaOneSM Conference | java.sun.com/javaone | 10
Active Record
Active Record style ORMs tend to be simpler to useGenerally there is a one to one mapping between tables and objectsVery easy to reason about the relationships between objects and tablesDjango uses an active record pattern
2008 JavaOneSM Conference | java.sun.com/javaone | 12
Data Mapper
Data Mapper style ORMs are more “DBA friendly – DBAs don't like 1:1 object:tableThe mapping between objects and tables needs to be more explicitData Mappers tend to be more flexible and can be used for harder problems (for example: legacy databases that can't be made to fit ActiveRecord)
2008 JavaOneSM Conference | java.sun.com/javaone | 13
Hibernate
As if Hibernate needs an introduction here :)Hibernate is a native Java platform ORM based on Data MapperVery mature, very stable technologyHas a very good reputationHas a huge install base
2008 JavaOneSM Conference | java.sun.com/javaone | 15
SQLAlchemy
Data Mapper style ORMs are more “DBA friendly – DBAs don't like 1:1 object:tableThe mapping between objects and tables needs to be more explicitData Mappers tend to be more flexible and can be used for harder problems (for example: many existing databases that can't be made to fit ActiveRecord)
2008 JavaOneSM Conference | java.sun.com/javaone | 17
For More Information
http://www.python.orghttp://wiki.python.org/jython
2008 JavaOneSM Conference | java.sun.com/javaone | 19
Title: Sun Sans Semibold; 34pt Max,30 pt Min; Two Lines Max
Level One bullet pointSun Sans Regular, 24 pt maximum size; 16 pt minimum• Level Two bullet• 20 pt Sun Sans Regular
• Level three: 18pt• Level four: 18pt
• Level five: 18 pt
2008 JavaOneSM Conference | java.sun.com/javaone | 20
Dual Text BlocksSubhead: Sun Sans Semibold, 26 pt max, 22 pt min.
Level One bullet point20 pt• Level Two: 18 pt
• Level Three:16 pt• Level Four:16 pt
Level One bullet point20 pt• Level Two:18 pt
• Level Three:16 pt• Level Four:16 pt
2008 JavaOneSM Conference | java.sun.com/javaone | 21
Learn how to architect and build 3-tier business systems using Enterprise JavaBeans™ (EJB™) technology[sample goal statement—replace with your own; this slide appears directly after your title slide]
2008 JavaOneSM Conference | java.sun.com/javaone | 22
Agenda
Agenda item one [use this color AND font to highlight agenda items]
Agenda item twoAgenda item threeAgenda item fourAgenda item fiveAnd so on
2008 JavaOneSM Conference | java.sun.com/javaone | 23
Code Sample
CodeRecommended font size for short blocks of code.Font used here is Courier New Bold at 20 points{
apply orange to code you want to discuss or highlight
}If you have room, feel free to enlarge the type for additional readability}Other Notes for CodeRecommended font size for short blocks of code.Font used here is Courier New Bold at 20 points{
2008 JavaOneSM Conference | java.sun.com/javaone | 26
Summary
Summary point oneSummary point twoSummary point threeSummary point…
2008 JavaOneSM Conference | java.sun.com/javaone | 27
For More Information
List• Cross-references to other sessions• BOFs• URLs• Related books, etc.
2008 JavaOneSM Conference | java.sun.com/javaone | 28
Primary Colors
Sun Blue
Sun Orange
Sun Green
Sun Yellow
Sun Gray
Sun Dark Blue
Sun Dark Orange
Sun Dark Green
Sun Dark Yellow
Sun Light Blue
Sun Light Orange
Sun Light Yellow
Secondary Colors
Default Color Palette
2008 JavaOneSM Conference | java.sun.com/javaone | 30
Sample Bar Chart Using Star Calc
Row 1 Row 2 Row 3 Row 41.5
22.5
33.5
44.5
55.5
66.5
77.5
88.5
99.510
Column 1Column 2Column 3
Chart Title
Source: [Please add the source of your data here]
2008 JavaOneSM Conference | java.sun.com/javaone | 31
Sample Bar Chart Using Microsoft Graph
Chart Title Chart
0123456789
10
Column 1 Column 2 Column 3 Column 4
Row 1Row 2Row 3
Source: [Please add the source of your data here]
2008 JavaOneSM Conference | java.sun.com/javaone | 32
Sample Pie Chart Using Star Calc
9.1
2.4
3.1
4.3
Row 1Row 2Row 3Row 4
Source: [Please add the source of your data here]
Chart Title
2008 JavaOneSM Conference | java.sun.com/javaone | 33
Chart Title Here
4.3
3.12.4
9.1Column 1Column 2Column 3Column 4
Sample Pie Chart Using Microsoft Graph
Source: [Please add the source of your data here]
2008 JavaOneSM Conference | java.sun.com/javaone | 34
Q2 ’03 % to Q2 ’02
Units 1K + 1%Revenue $1,000 +1%
Grossmargin 1% + . 1 0 pointsOPEX $1 +1%
Net income $1 +1%EPS $.01 +1%
Sample Data Matrix
Source: [Please add the source of your data here]
2008 JavaOneSM Conference | java.sun.com/javaone | 35
Sample Table
HeadingHeadingHeadingHeading
Body ContentBody ContentBody ContentBody Content
Body ContentBody ContentBody ContentBody Content
Top Related