Java Tutorial - Java Script :
Connections
A JDBC driver provides a connection to a database for a Java program. The connection is the means that Java uses to interact with the database. The following code fragment illustrates one way of making a connection to a supported database using JDBC:
Class.forName(“org.hsqldb.jdbcDriver”);
conn = DriverManager.getConnection(
“jdbc:hsqldb:c:/openjava/hsqldb/data/hsqltest”,
“sa”,
“”);
This example loads the driver for the HsqlDb database. The following line loads the JDBC driver into the JVM:
Class.forName(“org.hsqldb.jdbcDriver”); The DriverManager then uses the driver to get a connection. The getConnection method of the DriverManager class requires three arguments, a connection URL for the database, a username, and a password. The URL follows the same basic rules as a URL that would be used for the Internet.
jdbc:hsqldb:c:/openjava/hsqldb/data/hsqltest The jdbc: portion of this string is the protocol (exactly as http: is the protocol in a URL requesting a Web page). The hsqldb: portion of the string is the sub-protocol. The DriverManger looks for a loaded JDBC driver that understands the requested subprotocol. After the subprotocol is the subname. The subname provides information to the driver that is needed to connect a specific driver to a specific database. In this case the subname represents the absolute path to a database file on a file system. The subname information and
how it is formatted within the string is driver specific and varies from driver to driver. The subname may, for instance, include information needed to make a network connection to the database, such as machine name and port. For this reason, most programs to not code the string directly but instead read the connection strings from parameter files or some other configuration technique. Reading the string as a parameter allows connection to other database instances without recompiling the program.
