Database Access with Jython, Hibernate and SQLAlchemy

treeexcellentΛογισμικό & κατασκευή λογ/κού

13 Δεκ 2013 (πριν από 3 χρόνια και 9 μήνες)

136 εμφανίσεις

Database Access with Jython, Hibernate and
SQLAlchemy
Frank Wierzbicki, Jython Project Lead
http://www.jython.org
2008 JavaOne
SM
Conference | java.sun.com/javaone |
2
Why Jython?
Jython brings the Python language to the JVM™ technology
Python is a very mature language which was designed to be
easy to pick up but powerful to use
Jython has full and nearly seamless integration into any Java™
platform and code
Jython can access many of the libraries and frameworks
written in Python
2008 JavaOne
SM
Conference | java.sun.com/javaone |
3
Basic Jython Demo
2008 JavaOne
SM
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 package
zxJDBC is a thin wrapper around JDBC API, Java platform's
standard database interface
Python programmers get access to any database with a JDBC
API driver
Java platform programmers get access to any Python
frameworks that are built on DB-API
2008 JavaOne
SM
Conference | java.sun.com/javaone |
5
Basic Connection
from com.ziclix.python.sql import zxJDBC
try:

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 JavaOne
SM
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 zxJDBC
dbInfo = {'user':'username', 'password':'pass',

'mydb':'test', 'server':'localhost',

'port':3306}
con = zxJDBC.connectx("org.gjt.mm.mysql.MysqlDataSource",
**dbInfo)
#Or you can connect via JNDI
factory = "com.sun.jndi.fscontext.RefFSContextFactory"
db = zxJDBC.lookup('context/myDataSource'),

INITIAL_CONTEXT_FACTORY=factory)
2008 JavaOne
SM
Conference | java.sun.com/javaone |
7
Using Swing Tables From Jython
from javax.swing import JTable
from 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, 300
frame.visible = 1
2008 JavaOne
SM
Conference | java.sun.com/javaone |
8
Basic zxJDBC Demo
DB-API and JDBC API taste great together
2008 JavaOne
SM
Conference | java.sun.com/javaone |
9
Object Relational Mappings (ORMs)
Handling raw SQL in object oriented programs can feel
tedious for many programming tasks
Object Relational Mappers help make Table based data feel
more like objects from an OO style program
Two styles of ORM in common use: Active Record and Mapper
Handling raw SQL in object oriented programs can feel
tedious for many programming tasks
Object Relational Mappers help make Sun Access Table
Editor™ (Table) software based data feel more like objects
from an OO style program
2008 JavaOne
SM
Conference | java.sun.com/javaone |
10
Active Record
Active Record style ORMs tend to be simpler to use
Generally there is a one to one mapping between tables and
objects
Very easy to reason about the relationships between objects
and tables
Django uses an active record pattern
2008 JavaOne
SM
Conference | java.sun.com/javaone |
11
Django Demo
2008 JavaOne
SM
Conference | java.sun.com/javaone |
12
Data Mapper
Data Mapper style ORMs are more “DBA friendly – DBAs don't
like 1:1 object:table
The mapping between objects and tables needs to be more
explicit
Data 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 JavaOne
SM
Conference | java.sun.com/javaone |
13
Hibernate
As if Hibernate needs an introduction here :)
Hibernate is a native Java platform ORM based on Data
Mapper
Very mature, very stable technology
Has a very good reputation
Has a huge install base
2008 JavaOne
SM
Conference | java.sun.com/javaone |
14
Hibernate From Jython Demo
2008 JavaOne
SM
Conference | java.sun.com/javaone |
15
SQLAlchemy
Data Mapper style ORMs are more “DBA friendly – DBAs don't
like 1:1 object:table
The mapping between objects and tables needs to be more
explicit
Data 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 JavaOne
SM
Conference | java.sun.com/javaone |
16
SQLAlchemy Demo
2008 JavaOne
SM
Conference | java.sun.com/javaone |
17
For More Information
http://www.python.org
http://wiki.python.org/jython
2008 JavaOne
SM
Conference | java.sun.com/javaone |
18
Frank Wierzbicki
http://www.jython.org
2008 JavaOne
SM
Conference | java.sun.com/javaone |
19
Title: Sun Sans Semibold; 34pt Max,
30 pt Min; Two Lines Max
Level One bullet point
Sun 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 JavaOne
SM
Conference | java.sun.com/javaone |
20
Dual Text Blocks
Subhead: Sun Sans Semibold, 26 pt max, 22 pt min.
Level One bullet point
20 pt

Level Two: 18 pt

Level Three:16 pt

Level Four:16 pt
Level One bullet point
20 pt

Level Two:18 pt

Level Three:16 pt

Level Four:16 pt
2008 JavaOne
SM
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 JavaOne
SM
Conference | java.sun.com/javaone |
22
Agenda
Agenda item one

[use this color AND font to highlight agenda items]
Agenda item two
Agenda item three
Agenda item four
Agenda item five
And so on
2008 JavaOne
SM
Conference | java.sun.com/javaone |
23
Code Sample
Code
Recommended 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 Code
Recommended font size for short blocks of code.
Font used here is Courier New
Bold
at 20 points
{
2008 JavaOne
SM
Conference | java.sun.com/javaone |
24
Video Title
Video Company, etc.
2008 JavaOne
SM
Conference | java.sun.com/javaone |
25
Demo Title
Demo Company, etc.
2008 JavaOne
SM
Conference | java.sun.com/javaone |
26
Summary
Summary point one
Summary point two
Summary point three
Summary point…
2008 JavaOne
SM
Conference | java.sun.com/javaone |
27
For More Information
List

Cross-references to other sessions

BOFs

URLs

Related books, etc.
2008 JavaOne
SM
Conference | java.sun.com/javaone |
28
Primary Colors
Sun Blue
Sun Orange
Sun Green
Sun Yellow
Sun Gray
SunDark Blue
SunDark Orange
SunDark Green
SunDark Yellow
SunLight Blue
SunLight Orange
SunLight Yellow
Secondary Colors
Default Color Palette
2008 JavaOne
SM
Conference | java.sun.com/javaone |
29
Sample Graphics
Primary default color graphics
2008 JavaOne
SM
Conference | java.sun.com/javaone |
30
Sample Bar Chart Using Star Calc
Row 1
Row 2
Row 3
Row 4
1.5
2
2.5
3
3.5
4
4.5
5
5.5
6
6.5
7
7.5
8
8.5
9
9.5
10
Column 1
Column 2
Column 3
Chart Title
Source: [Please add the source of your data here]
2008 JavaOne
SM
Conference | java.sun.com/javaone |
31
Sample Bar Chart Using Microsoft Graph
Chart Title Chart
0
1
2
3
4
5
6
7
8
9
10
Column 1
Column 2
Column 3
Column 4
Row 1
Row 2
Row 3
Source: [Please add the source of your data here]
2008 JavaOne
SM
Conference | java.sun.com/javaone |
32
Sample Pie Chart Using Star Calc
9.1
2.4
3.1
4.3
Row 1
Row 2
Row 3
Row 4
Source: [Please add the source of your data here]
Chart Title
2008 JavaOne
SM
Conference | java.sun.com/javaone |
33
Chart Title Here
4.3
3.1
2.4
9.1
Column 1
Column 2
Column 3
Column 4
Sample Pie Chart Using Microsoft Graph
Source: [Please add the source of your data here]
2008 JavaOne
SM
Conference | java.sun.com/javaone |
34
Q2 ’03
% to Q2 ’02
Units
1K
+ 1%
Revenue
$1,000
+1%
Gross margin
1%
+. 1 0 points
OPEX
$1
+1%
Net income
$1
+1%
EPS
$.01
+1%
Sample Data Matrix
Source: [Please add the source of your data here]
2008 JavaOne
SM
Conference | java.sun.com/javaone |
35
Sample Table
Heading
Heading
Heading
Heading
Body Content
Body Content
Body Content
Body Content
Body Content
Body Content
Body Content
Body Content
2008 JavaOne
SM
Conference | java.sun.com/javaone |
36
Speaker Name, Speaker Title, 24 pt.
ID#, Misc., 16 pt.
Speaker’s logo here
(optional)