36
Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead http://www.jython.org

Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

  • Upload
    others

  • View
    52

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

Database Access with Jython, Hibernate and SQLAlchemy

Frank Wierzbicki, Jython Project Lead

http://www.jython.org

Page 2: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

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

Page 3: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

2008 JavaOneSM Conference | java.sun.com/javaone | 3

Basic Jython Demo

Page 4: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

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

Page 5: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

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

Page 6: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

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)

Page 7: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

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

Page 8: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

2008 JavaOneSM Conference | java.sun.com/javaone | 8

Basic zxJDBC DemoDB-API and JDBC API taste great together

Page 9: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

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

Page 10: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

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

Page 11: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

2008 JavaOneSM Conference | java.sun.com/javaone | 11

Django Demo

Page 12: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

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)

Page 13: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

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

Page 14: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

2008 JavaOneSM Conference | java.sun.com/javaone | 14

Hibernate From Jython Demo

Page 15: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

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)

Page 16: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

2008 JavaOneSM Conference | java.sun.com/javaone | 16

SQLAlchemy Demo

Page 17: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

2008 JavaOneSM Conference | java.sun.com/javaone | 17

For More Information

http://www.python.orghttp://wiki.python.org/jython

Page 18: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

2008 JavaOneSM Conference | java.sun.com/javaone | 18

Frank Wierzbicki

http://www.jython.org

Page 19: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

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

Page 20: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

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

Page 21: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

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]

Page 22: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

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

Page 23: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

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{

Page 24: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

2008 JavaOneSM Conference | java.sun.com/javaone | 24

Video TitleVideo Company, etc.

Page 25: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

2008 JavaOneSM Conference | java.sun.com/javaone | 25

Demo TitleDemo Company, etc.

Page 26: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

2008 JavaOneSM Conference | java.sun.com/javaone | 26

Summary

Summary point oneSummary point twoSummary point threeSummary point…

Page 27: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

2008 JavaOneSM Conference | java.sun.com/javaone | 27

For More Information

List• Cross-references to other sessions• BOFs• URLs• Related books, etc.

Page 28: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

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

Page 29: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

2008 JavaOneSM Conference | java.sun.com/javaone | 29

Sample GraphicsPrimary default color graphics

Page 30: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

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]

Page 31: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

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]

Page 32: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

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

Page 33: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

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]

Page 34: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

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]

Page 35: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

2008 JavaOneSM Conference | java.sun.com/javaone | 35

Sample Table

HeadingHeadingHeadingHeading

Body ContentBody ContentBody ContentBody Content

Body ContentBody ContentBody ContentBody Content

Page 36: Database Access with Jython, Hibernate and SQLAlchemy · Database Access with Jython, Hibernate and SQLAlchemy Frank Wierzbicki, Jython Project Lead ... As if Hibernate needs an introduction

2008 JavaOneSM Conference | java.sun.com/javaone | 36

Speaker Name, Speaker Title, 24 pt.

ID#, Misc., 16 pt.

Speaker’s logo here (optional)