Connecting To MySQL Using JDBC Driver
Connecting To MySQL Using JDBC Driver
Ad closed by
In this tutorial, you will learn how to connect to MySQL database using JDBC Connection object.
To connect to MySQL database from a Java program, you need to do the following steps:
First, in NetBeans IDE, from project name, right mouse click and choose properties menu item. The
project properties dialog will appear.
http://www.mysqltutorial.org/connecting-to-mysql-using-jdbc-driver/ 1/11
21/11/2018 Connecting to MySQL Using JDBC Driver
Second, on the left hand side of the project properties dialog, from the Categories section, choose
Libraries item.
http://www.mysqltutorial.org/connecting-to-mysql-using-jdbc-driver/ 2/11
21/11/2018 Connecting to MySQL Using JDBC Driver
Third, click on the Add JAR folder button, browse to the location where you installed MySQL Connector/J,
and choose the JAR file as screenshot below; after that click OK button.
http://www.mysqltutorial.org/connecting-to-mysql-using-jdbc-driver/ 3/11
21/11/2018 Connecting to MySQL Using JDBC Driver
http://www.mysqltutorial.org/connecting-to-mysql-using-jdbc-driver/ 4/11
21/11/2018 Connecting to MySQL Using JDBC Driver
1 import java.sql.Connection;
2 import java.sql.DriverManager;
3 import java.sql.SQLException;
Second, you call the getConnection() method of the DriverManager class to get the Connection object.
There are three parameters you need to pass to the getConnection() method:
1. url: the database URL in the form jdbc:subprotocol:subname. For MySQL, you use the
jdbc:mysql://localhost:3306/mysqljdbc i.e., you are connecting to the MySQL with server name
localhost, port 3006, and database mysqljdbc.
2. user: the database user that will be used to connect to MySQL.
3. password: the password of the database user.
http://www.mysqltutorial.org/connecting-to-mysql-using-jdbc-driver/ 5/11
21/11/2018 Connecting to MySQL Using JDBC Driver
6 String password = "secret";
7
8 // create a connection to the database
9 conn = DriverManager.getConnection(url, user, password);
10 // more processing here
11 // ... HOME BASIC ADVANCED FUNCTIONS INTERFACES TIPS TRYIT
12 } catch(SQLException e) {
13 System.out.println(e.getMessage());
14 } finally {
15 try{
16 if(conn ! null)
17 conn.close()
18 }catch(SQLException ex){
19 System.out.println(ex.getMessage())
20 }
21 }
When connecting to MySQL, anything could happens e.g., database server is not available, wrong user
name or password, etc. in such cases, JDBC throws a SQLException . Therefore, when you create a
Connection object, you should always put it inside a try catch block. Also you should always close the
database connection once you complete interacting with database by calling close() method of the
Connection object.
From Java 7, there is another nice statement called try-with-resources that allows you to simplify the
code above as follows:
1 // db parameters
2 String url = "jdbc:mysql://localhost:3306/mysqljdbc";
3 String user = "root";
4 String password = "secret";
5
6 Connection conn = null;
7
8 try(conn = DriverManager.getConnection(url, user, password);) {
9 // processing here
10 } catch(SQLException e) {
11 System.out.println(e.getMessage());
12 }
It is automatically calls the close() method of the Connection object once program finishes. As you can
see it’s cleaner and more elegant. However…
It is not secure as well as flexible when you hard coded the database parameters inside the code like
above. In case you change the database server or password; you have to change the code, compile it
again, which is not a good design.
To avoid hard coding all the database parameters in the code, you can use a Java properties file to store
them. In case of changes, you just need to change them in the properties file and you don’t have⤒to
recompile the code.
http://www.mysqltutorial.org/connecting-to-mysql-using-jdbc-driver/ 6/11
21/11/2018 Connecting to MySQL Using JDBC Driver
1 # MySQL DB parameters
2 user=root
HOME BASIC ADVANCED FUNCTIONS INTERFACES TIPS TRYIT
3 password=secret
4 url=jdbc:mysql://localhost:3306/mysqljdbc
You can rewrite the code for creating a Connection object with parameters from a properties file as
follows:
For each interaction with MySQL database, you need to create a new connection. You would have the
same piece of code for doing this in all places. Rather than doing this, you can create a new class for
handing connection creation:
1 package org.mysqltutorial;
2
3 import java.io.FileInputStream;
4 import java.io.IOException;
5 import java.sql.Connection;
6 import java.sql.DriverManager;
7 import java.sql.SQLException;
8 import java.util.Properties;
9
10 /**
11 *
12 * @author mysqltutorial.org ⤒
13 */
14 public class MySQLJDBCUtil {
http://www.mysqltutorial.org/connecting-to-mysql-using-jdbc-driver/ 7/11
21/11/2018 Connecting to MySQL Using JDBC Driver
15
16 /**
17 * Get database connection
18 *
19 * @return a Connection object
20 HOME SQLException
* @throws BASIC ADVANCED FUNCTIONS INTERFACES TIPS TRYIT
21 */
22 public static Connection getConnection() throws SQLException {
23 Connection conn = null;
24
25 try (FileInputStream f = new FileInputStream("db.properties")) {
26
27 // load the properties file
28 Properties pros = new Properties();
29 pros.load(f);
30
31 // assign db parameters
32 String url = pros.getProperty("url");
33 String user = pros.getProperty("user");
34 String password = pros.getProperty("password");
35
36 // create a connection to the database
37 conn = DriverManager.getConnection(url, user, password);
38 } catch (IOException e) {
39 System.out.println(e.getMessage());
40 }
41 return conn;
42 }
43
44 }
From next tutorial, we will use this MySQLJDBCUtil class for creating a new connection to MySQL as
follows:
1 package org.mysqltutorial;
2
3 import java.sql.Connection;
4 import java.sql.SQLException;
5
6 /**
7 *
8 * @author mysqltutorial.org
9 */
10 public class Main {
11
12 public static void main(String[] args) {
13 // create a new connection from MySQLJDBCUtil
14 try (Connection conn = MySQLJDBCUtil.getConnection()) {
15
16 // print out a message
17 System.out.println(String.format("Connected to database %s "
18 + "successfully.", conn.getCatalog()));
19 } catch (SQLException ex) {
20 System.out.println(ex.getMessage());
21 } ⤒
22 }
http://www.mysqltutorial.org/connecting-to-mysql-using-jdbc-driver/ 8/11