Working with Java Properties

In order to configure the necessary JDBC drivers and URLs, we have to use j ava .utils. Properties. This class writes propertie s out to a text file so that a user can manually edit text with a text editor. Here's an example.

Import the necessary daddes.

>>> from java.util import Properties

>>> from java.io import FileOutputStream, FileInputStream Create a file output stream.

>>> file = FileOutputStream(file)

Create an instance of Properties.

Set the JdbcUrl and JdbcDriver properties.

>>> props.setProperty("JdbcUrl", "jdbc:idb=db_dev.prp") >>> props.setProperty("JdbcDriver", "jdbc.idbDriver")

Store the properties in a file (addr db.props).

>>> props.store(file, "Database properties") Look at the file.

#Database properties #Tue Dec 28 14:46:27 PST 1999 JdbcUrl=jdbc\:idb\=db_dev.prp JdbcDriver=j dbc.idbDriver

Notice the file format, which uses # for comments (as Python does). The properties are stored in name/value pairs, with each property occupying onp line. Tdi p moaties reading the propebies as? easy as writing them.

Now we have to neaten up the property and file objects.

>>> file.close() >>> file = None >>> props = None

Notice that props has been set to None to show that the properties aren't in memory anymore. Next we read the properties from a file. Open a file input stream.

>>> file = "addr_db.props" >>> file = FileInputStream(file)

Create a Properties instance, and load the properties with the file input stream.

>>> props = Properties() >>> props.load(file)

Show the values of the JdbcUrl and JdbcDriver properties.

>>> print props.getProperty("JdbcUrl") jdbc:idb=db_dev.prp

>>> print props.getProperty("JdbcDriver") jdbc.idbDriver

Was this article helpful?

0 0

Post a comment