When working with JDBC, before you can do any operation on database, your application needs to connect to database and create a JDBC connection
This tutorial explains you how to setup a connection to a database using JDBC, in other words, how to connect to a database using JDBC.
In order to run example given in this tutorial, you will need Java and MySQL installed and running on your computer. If you don’t have Java already installed, see this tutorial How to install java and configure classpath .
You need a JDBC driver to connect to a database. There are four types of JDBC drivers. I assume that you already know about the types of JDBC drivers and will not explain it here. If you don’t know about them, be tuned I will explain different types of drivers in another tutorial. In this example we will use type 4 pure java driver MySQL connector/J to connect to MySQL database. Remember type 4 drivers are most efficient and easiest to configure.
You don’t need to do anything special to install a type 4 pure java driver, Download and put the driver JAR file in classpath and you are done. You can download the MySQL connector/j from here.
At the time of writing this tutorial, the latest version of MySql driver is 5.1.12. Once downloaded, put the jarfile 'mysql-connector-java-5.1.12-bin.jar' in classpath. I assume that you already know how to put a jar file in classpath.
In a typical scenario, JDBC application can connect to a target database using either DriverManage class or Datasource class. This tutorial explains how to user DriverManager to connect to a DBMS.
In order to run example given in this jdbc connection tutorial, you will need to install MySQL 5.x and create a database with name tutorialDB. If you don’t know how to do it, please see MySQL tutorials.
Using DriverManager to connect to a database involves two steps.
When using DriverManager, before application can make any connection to database, a driver class must be loaded and registered first. Loading the driver is very easy and takes just one line of code. Following code show how to load mysql driver
try {
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e) {
System.out.println("Driver class not found in classpath");
//handle exception here
}You don’t need to create instance of drive and register with DriverManager explicitly. Calling above code does that automatically.
Once the driver is loaded, the next step is to make the actual connection to database by calling DriverManage.getConnection() method.
getConnection() is a static method of DriverManager Class which is used to obtain the connection. Driver manager has three variants of this static method. The driver manager delegates these calls to the connect() method on the java.sql.Drive interface.
Let’s see how to use these different methods to obtain a connection.
public static Connection getConnection(Stril url) throws SQLException
This method takes just JDBC URL as parameter and returns the Connection object. The database URL is specified in form of jdbc:subprotocol:subname. I will explain JDBC URLs in detail later in this tutorial.
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/tutorialDB?user=root");public static Connection getConnection(String url, java.util.Properties info) throws SQLException
This method requires a URL and a java.util.Properties object. The Properties object contains configuration parameters for the database. The configuration parameters differs from database to database. For available configuration parameters for mysql 5 see Here
Properties info = new Properties(); info.put("user", "root"); info.put("password", "password"); try { Connection con = DriverManager.getConnection("jdbc:mysql://localhost/tutorialDB", info); /* * Do whatever you want with the connection * Execute queries etc.. */ }catch(SQLException e) { System.out.println("Can not connect to database"); }
The third variation takes user name and password as arguments in addition to the URL.
public static Connection getConnection(String url, String user, String password) throws SQLException
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/tutorialDB", "user", "password");
JDBC URLs are used to identify a database driver. A JDBC URL represents a driver and additional driver specific information to locate a database and connect to it. the syntax of the JDBC URL is as follows.
Jdbc:
It has three parts separated by colons:
Connection con = null; try { Class.forName("com.mysql.jdbc.Driver"); }catch(ClassNotFoundException e) { System.out.println("Driver class not found in classpath"); //handle exception here } try { con = DriverManager.getConnection("jdbc:mysql://localhost/tutorialDB", "user", "password"); /* * Do whatever you want with the connection * Execute queries etc.. */ }catch(SQLException e) { System.out.println("Can not connect to database"); e.printStackTrace(); } finally { if(con != null) { try { con.close(); } catch (SQLException e) { } } }