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:
- Load the MySQL Connector/J into your program.
- Create a new Connection object from the
DriverManager
class. Then you can use this Connection object to execute queries.
How To Add Mysql Connector Jar File In Netbeans - JavaFor More Details And Screenshots Visit - import the connector to the current p. This video shows the MySQL ODBC and JDBC driver installation tasks on Windows before you can use MySQL Connector in Cloud Data Integration, Winter 2019.
Loading MySQL Connector/J into your program
To load MySQL Connector/J into your program you follow three steps below:
First, in NetBeans IDE, from project name, right mouse click and choose properties menu item. The project properties dialog will appear.
Second, on the left hand side of the project properties dialog, from the Categories section, choose Libraries item.
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.
Connecting to MySQL database
First, you need to import three classes: SQLException, DriverManager, and Connection from the java.sql.* package.
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:
- 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.
- user: the database user that will be used to connect to MySQL.
- password: the password of the database user.
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:
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.
Let’s take a look at the properties file named db.properties:
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:
From next tutorial, we will use this MySQLJDBCUtil
class for creating a new connection to MySQL as follows:
In this tutorial, we have shown you step by step how to connect to MySQL using JDBC Connection object and use properties file to store database parameters. At the end of the tutorial, we developed a utility class that you can reuse it every time you create a connection to the database.
Mysql Jdbc Driver Code
-->The Microsoft JDBC Driver for SQL Server is a Type 4 JDBC driver that provides database connectivity through the standard JDBC application program interfaces (APIs) available on the Java platform. The driver downloads are available to all users at no extra charge. They provide access to SQL Server from any Java application, application server, or Java-enabled applet.
Download
Version 9.2 is the latest general availability (GA) version. It supports Java 8, 11, and 15. If you need to use an older Java runtime, see the Java and JDBC specification support matrix to see if there's a supported driver version you can use. We're continually improving Java connectivity support. As such we highly recommend that you work with the latest version of Microsoft JDBC driver.
Mysql Jdbc Driver Java
Download Microsoft JDBC Driver 9.2 for SQL Server (zip)
Download Microsoft JDBC Driver 9.2 for SQL Server (tar.gz)
Version information
- Release number: 9.2.1
- Released: March 02, 2021
When you download the driver, there are multiple JAR files. The name of the JAR file indicates the version of Java that it supports.
Note
If you are accessing this page from a non-English language version, and want to see the most up-to-date content, please visit the US-English version of the site. You can download different languages from the US-English version site by selecting available languages.
Available languages
This release of Microsoft JDBC Driver for SQL Server is available in the following languages:
Microsoft JDBC Driver 9.2.1 for SQL Server (zip):Chinese (Simplified) | Chinese (Traditional) | English (United States) | French | German | Italian | Japanese | Korean | Portuguese (Brazil) | Russian | Spanish
Microsoft JDBC Driver 9.2.1 for SQL Server (tar.gz):Chinese (Simplified) | Chinese (Traditional) | English (United States) | French | German | Italian | Japanese | Korean | Portuguese (Brazil) | Russian | Spanish
Release notes
For details about this release, see the release notes and system requirements.
Previous releases
Mysql Jdbc Driver
To download previous releases, see previous Microsoft JDBC Driver for SQL Server releases.
Using the JDBC driver with Maven Central
The JDBC driver can be added to a Maven project by adding it as a dependency in the POM.xml file with the following code:
Unsupported drivers
Unsupported driver versions aren't available for download here. We're continually improving the Java connectivity support. As such we highly recommend that you work with the latest version of Microsoft JDBC driver.
Next steps
Mysql Jdbc Driver Class
For more information about the Microsoft JDBC Driver for SQL Server, see Overview of the JDBC driver and the JDBC driver GitHub repository.