| Previous | Table of Contents | Next |
When the connection is no longer needed, close it. Even though the connection would be collected as garbage, it is important to free connections as soon as possible. Because most databases allow a limited number of connections, the connections are a valuable resource.
import java.sql.*;
public class ConnectionExample
{
public static void main (String args[])
{
try
{
/*
Load the class for the driver.
Make sure the driver is in your CLASSPATH.
*/
Class.forName(sun.jdbc.odbc.JdbcOdbcDriver);
} catch (Exception e)
{
System.out.println(JDBCODBC driver failed to load.);
return;
}
try
{
/*
Specify the data source you want to connect to by
passing in the URL. If a driver is found that can
connect to this data source, you will get a
Connection object.
*/
Connection con =
DriverManager.getConnection(jdbc:odbc:Inventory,
,);
// When you are done, close the connection.
con.close();
} catch (Exception e)
{
System.out.println(e);
}
}
}
Connecting with Properties
In the previous example, the driver and data source were hardcoded in the code. Although this is a reasonable technique, it misses one of the dynamic features of JDBC. JDBC allows you to determine what data source and driver to use at run time. This could be important for load balancing or for easy configuration and installation of your application in various environments. In order to select a driver and data source dynamically, use a property list loaded from a text file.
Follow these steps to load the text file:
NOTE: The name of the property file is arbitrary, as are the keys and values stored. These are all programmer defined.
In this example, a connection is made to the Inventory data source; however, the driver name and the data source information are stored in a text file called datasource.config instead of hard coding the information in the class file. The text file has the following values:
datasource.driver=sun.jdbc.odbc.JdbcOdbcDriver datasource.protocol=jdbc datasource.subprotocol=odbc datasource.name=Inventory datasource.username= datasource.password=
The example also uses the ClassLoader method getSystemResourceAsStream to return an InputStream to our datasource.config file. This method searches the entire ClassPath for our file. In most of our examples, we assume . is in your ClassPath. If it isnt, place the datasource.config file in a CLASSPATH directory.
import java.util.Properties;
import java.io.InputStream;
import java.sql.*;
public class PropertyExample
{
public static void main (String args[])
{
String dsDriver=;
String dsProtocol=;
String dsSubprotocol=;
String dsName=;
String dsUsername=;
String dsPassword=;
try
{
// Loads the configuration file
InputStream is = ClassLoader.getSystemResourceAsStream
(datasource.config);
if(is != null)
{
Properties p = new Properties();
p.load (is);
dsDriver = p.getProperty(datasource.driver);
dsProtocol = p.getProperty(datasource.protocol);
dsSubprotocol =
p.getProperty(datasource.subprotocol);
dsName = p.getProperty(datasource.name);
dsUsername = p.getProperty(datasource.username);
dsPassword = p.getProperty(datasource.password);
}
} catch (Exception e)
{
System.out.println(Unable to read property file);
return;
}
try
{
Class.forName(dsDriver);
}
catch (Exception e)
{
System.out.println(Failed to load driver.);
return;
}
try
{
String theURL = dsProtocol+:+dsSubprotocol+:+dsName;
/*
Create a connection using the values parsed from
the configuration file.
*/
Connection con = DriverManager.getConnection
(theURL,dsUsername,dsPassword);
if(con != null)
{
System.out.println(Successfully connected to +
dsName);
con.close();
}
}
catch (Exception e)
{
System.out.println(Failed to connect: + e);
return;
}
}
}
Now either open a connection by hard coding the URL components or load them from a configuration file. If you load the values from the property file, you need to realize that these values may be changed, even though the class file does not. This means you should take more precautions in preparing your query by checking the metadata of the connection to verify that the database is appropriate for your query. For example, suppose the name of the database is changed in the property file and you try to query it. If the tables you expect to have in the database arent there, your application will fail. Rather than waiting for the application to fail, you could examine the data source first to see what tables are available. We follow this procedure in the next section.
Once a connection has been established with a data source, processing of SQL can begin. However, it is often useful to query the data source for information about its structure, capabilities, and limitations. This will enable you to optimize your query and prevent access errors such as trying to retrieve data that doesnt exist. Before examining the code for this process, lets view the metadata using a tool called JDBCTest.
JDBCTest is a Java application written by Intersolv that loads and interacts with any JDBC driver. It is a useful tool for looking at your data source and testing your JDBC drivers capabilities before you actually use it. After examining the data source with JDBCTest, this section walks through an example program that demonstrates how you might do this in your own code.
| Previous | Table of Contents | Next |