Java Reference
In-Depth Information
public static Connection getConnection(String url, Properties info)
throws SQLException. The Properties object contains information
about the connection to be made and typically includes user and
password entries.
Notice that the return value of each of these methods is a java.sql.Connec-
tion object, which represents the connection to the database. I will discuss the
Connection class in the upcoming section Creating Statements . The following
DriverManagerDemo program demonstrates using the DriverManager class
to connect to the moviesDSN data source. After the connection is made, the
getCatalog() method is invoked on the Connection object, which displays the
name of the database file. Study the program and try to determine its output,
which is shown in Figure 18.1.
import java.sql.*;
public class DriverManagerDemo
{
public static void main(String [] args)
{
String url = “jdbc:odbc:” + args[0];
System.out.println(“Attempting to connect to “ + url);
try
{
System.out.println(“Loading the driver...”);
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
System.out.println(“Establishing a connection...”);
Connection connection =
DriverManager.getConnection(url);
System.out.println(“Connect to “
+ connection.getCatalog() + “ a success!”);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Figure 18.1
Output of the DriverManagerDemo program.
Search WWH ::




Custom Search