Chapter 10. Using Java Libraries

Jython allows you to easily use existing Java library code from within your Jython program. Frequently, the libraries are actually easier to manage from Jython than they are from Java. They may be easier because Jython tools have been written to simplify the library API, or because the Jython reflection shortcuts significantly improve library access.

This chapter showcases three common Java tools—the JDBC database access API, the servlet API, and XML parsing standards—along with strategies for getting the most out of them from within Jython. Although you can use the JDBC database access API directly in Jython, a tool called zxJDBC allows for a more Pythonic style of access. The Java servlet API can be programmed directly in Jython with the help of the PyServlet tool. A number of different Java-based XML tools can be used more easily through Jython. We hope that these examples offer you ideas on how to best use other Java libraries.

JDBC and the Python Database API

Database access is an area in which both Java and Python solutions exist. In Java, database access is managed through the JDBC database connectivity classes. JDBC classes work fine in Jython, however their interaction with Python objects and code is a little awkward, in particular because the JDBC classes are not set up to allow Python-style looping through results. The analogous Python functionality is provided by the Python Database (DB) API, which is the standard target for database connectivity ...

Get Jython Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.