6.1 Connecting to MySQL Using the JDBC DriverManager
Interface
When you are using JDBC outside of an application server, the DriverManager
class manages the establishment of connections.
Specify to the DriverManager
which JDBC drivers to try to make Connections with. The easiest way to do this is to use Class.forName()
on the class that implements the java.sql.Driver
interface. With MySQL Connector/J, the name of this class is com.mysql.jdbc.Driver
. With this method, you could use an external configuration file to supply the driver class name and driver parameters to use when connecting to a database.
1) Load JDBC driver. The easiest way to do this is to use Class.forName on the class that implements the java.sql.Driver interface. With MySQL Connector/J, the name of this class is com.mysql.jdbc.Driver. With this method, you could use an external configuration file to supply the driver class name and driver parameters to use when connecting.
The following section of Java code shows how you might register MySQL Connector/J from the main()
method of your application. If testing this code, first read the installation section at Chapter 3, Connector/J Installation, to make sure you have connector installed correctly and the CLASSPATH
set up. Also, ensure that MySQL is configured to accept external TCP/IP connections.
After the driver has been registered with the DriverManager
, you can obtain a Connection
instance that is connected to a particular database by calling DriverManager.getConnection()
:
- MySQL Connectors MySQL provides standards-based drivers for JDBC, ODBC, and.Net enabling developers to build database applications in their language of choice. In addition, a native C library allows developers to embed MySQL directly into their applications.
- Using import com.mysql.jdbc.Driver; in JDBC code is not a good practice and you need to import only java.sql. and javax.sql. The reason is to detach the code from the specific driver implementation. See here for more information how to make JDBC connections.
The MySQL JDBC Driver makes it easy to access live MySQL data directly from any modern Java IDE. After configuring the connection, explore the tables, views, and stored procedures provided by the MySQL JDBC Driver. These constructs return live MySQL data that developers can work with directly from within the IDE. Download a JDBC driver for MySQL (for example, the Connector/J driver). If the file that you downloaded is in an archive format (for example,.zip,.tar.gz, and so on), extract its contents. Copy the.jar file from the extracted contents to a location on your hard disk drive.
Example 6.1 Connector/J: Obtaining a connection from the DriverManager
If you have not already done so, please review the portion of Section 6.1, “Connecting to MySQL Using the JDBC DriverManager
Interface” above before working with the example below.
This example shows how you can obtain a Connection
instance from the DriverManager
. There are a few different signatures for the getConnection()
method. Consult the API documentation that comes with your JDK for more specific information on how to use them.
Once a Connection
is established, it can be used to create Statement
and PreparedStatement
objects, as well as retrieve metadata about the database. This is explained in the following sections.