This section describes some sundry methods for connecting or for accessing configuration data from a file.
bundle.getBundle(filename)
Loads data from a properties file called Config.properties. Although not JDBC specific, it would be used when storing connection data in a configuration file.
For example, Config.properties contains the following:
Driver = com.mysql.jdbc.Driver Conn = jdbc:mysql://test.host.com/firstdb?user =guru2b&password=g00r002b
The main program then contains the following:
ResourceBundle rb = ResourceBundle.getBundle("Conn");
String conn = rb.getString("Conn");
...
Class.forName(rb.getString("Driver"));
Connection = DriverManager.getConnection(conn);
DriverManager.getConnection(connection_details)
Requests a connection with the specified details, returning a connection object. Depending on the driver you use, the connection details will be specified in slightly different ways. For the Caucho driver, the format is as follows:
jdbc:mysql-caucho://host_name[:port]/database_name
For example:
Connection connection = DriverManager.getConnection("jdbc:mysql-caucho://–
test.host.co.za/firstdb", "guru2b", "g00r002b");
The Connector/J driver uses a slightly different format, as follows:
jdbc:mysql://[host_name][:port]/database_name [?property1=value1][&property=value2]
The properties can be any of the ones listed in Table F.1, although you'll mostly just use the password and username.
|
Name |
Description |
|---|---|
|
autoReconnect |
Automatically attempts to connect again when the connection dies. Defaults tofalse. |
|
characterEncoding |
When Unicode is the character set, specifies the Unicode encoding to use. |
|
initialTimeout |
The time in seconds to wait between reconnection attempts. The default is 2. |
|
maxReconnects |
The maximum number of reconnection attempts. The default is 3. |
|
maxRows |
The maximum number of rows to return for a query or 0 (the default) for all rows. |
|
password |
The user's password (no default). |
|
useUnicode |
Specifies that Unicode be used as the character set for the connection. Defaults tofalse. |
|
user |
The user to connect as (no default). |
For example:
DriverManager.getConnection("jdbc:mysql://–
test.host.co.za/firstdb?user=guru2b&password=g00r002b");
bundle.getString(string)
See getBundle for an example of reading data from a configuration file.