Also you should always close the database connection once you complete interacting with database by calling close() method of the Connection object. Therefore, when you create a Connection object, you should always put it inside a try catch block. in such cases, JDBC throws a SQLException. When connecting to MySQL, anything could happens e.g., database server is not available, wrong user name or password, etc.
String url = "jdbc:mysql://localhost:3306/mysqljdbc" Ĭonn = DriverManager.getConnection(url, user, password) Connecting to MySQL databaseįirst, you need to import three classes: SQLException, DriverManager, and Connection from the java.sql.* package. 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. Second, on the left hand side of the project properties dialog, from the Categories section, choose Libraries item. If your deployment is connected to a MySQL database (which is used by certain asset discovery connections), download the MySQL JDBC driver from the MySQL Community Downloads website. The project properties dialog will appear. To load MySQL Connector/J into your program you follow three steps below:įirst, in NetBeans IDE, from project name, right mouse click and choose properties menu item.
Perform the following steps to install the MySQL JDBC drivers on Windows or Linux: Click the following link to download the MySQL JDBC driver. Loading MySQL Connector/J into your program Before you use MySQL Connector, you must install the MySQL JDBC driver version 8.0.12 MySQL ODBC driver on the Windows or Linux machine where you installed the Secure Agent. Then you can use this Connection object to execute queries. After you unpack the ZIP or TAR archive, install the driver on your Windows, macOS, or Linux system. Create a new Connection object from the DriverManager class. If your deployment is connected to a MySQL database (which is used by certain asset discovery connections), download the MySQL JDBC driver from the MySQL Community Downloads website.
In this tutorial, you will learn how to connect to MySQL database using JDBC Connection object.