Using JDBC with Java Applications and (Cox webspace) Applets 70
Using JDBC with Java Applications and Applets 70 connectToDB() method in our Hello object, you see that the connection from Java to the database is performed in a single line of code: connection = DriverManager.getConnection( “jdbc:mysql://localhost/accounts?user=&password=”); As you can see, the DriverManager is the catalyst used to create the connection to the database. This is consistent with its job of managing all JDBC drivers. When the getConnection() method is called, the DriverManager needs to decide what JDBC driver to use to connect to the database. Figure 5.1 shows how the DriverManager determines the proper JDBC driver to use with a given connection request. DriverManager Connector/J Oracle SQLServer MySQL Oracle Application SQLServer Figure 5.1 Determining the proper driver. Let s begin our discussion of obtaining a connection to the database by examining the API for the DriverManager. DriverManager API DriverManager is a static class that exposes methods for handling connections to a database as well as administrative methods for JDBC drivers. The following methods are those we might be interested in using: Connection getConnection(String URL) The DriverManager uses a reg istered driver in an attempt to build a connection to a specified database. Connection getConnection(String URL, Properties props) The DriverManager uses a registered driver in an attempt to build a connection to the specified database using the properties provided in the Properties object. Connection getConnection(String URL, String username, String password) The DriverManager uses a registered driver in an attempt to build a connection to the specified database using the provided username and password.
Note: If you are looking for cheapest and affordable webspace to host and run your servlet application check Astra j2ee hosting services