This article will teach you how to connect Java application with MySQL database using NetBeans. Connecting your Java application in NetBeans with MySQL is not a difficult task.
Requirements
- NetBeans IDE 8.2.
- MySQL JDBC driver.
Downloading MySQL JDBC driver
- To download the latest version of the JDBC driver, visit this link. From Select Operating System drop-down, select Platform Independent and download the zip archive.
- Extract and open the downloaded file. There will be a file named mysql-connector-java-8.0.12.jar [version number may be different].
Setting-up database
- Start xampp.
- Turn on Apache and Mysql.
- Create a MySQL database.
Configuring the NetBeans project
- Open NetBeans and create a new project by clicking File->New Project.
- From the Choose Project window, select Java->Java Application and click next.
- Choose a project name and click Finish.
- Expand the project folder and Right-click on Libraries.
- Select Add JAR/Folder.
- Select mysql-connector-java-8.0.12.jar and click open. Now you will see the jar file listed under Libraries.
- Go to Window->Services.
- Right-click on Databases and select New Connection.
- Click Add and select mysql-connector-java-8.0.12.jar file. From Driver drop-down, select MySQL [Connector/J driver]. Click Next.
- In the Customize Connection window, replace the Database name with the one you have created for the application. If you have not created a database, leave it as it is.
- Click Test Connection.
If you get a Connection Succeeded message, you have connected your application with the MySQL database.
Subscribe
Join the newsletter to get the latest updates.
Great! Check your inbox and click the link
Please enter a valid email address!
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.cj.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.
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 4, 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.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
// Notice, do not import com.mysql.cj.jdbc.*
// or you will have problems!
public class LoadDriver {
public static void main[String[] args] {
try {
// The newInstance[] call is a work around for some
// broken Java implementations
Class.forName["com.mysql.cj.jdbc.Driver"].newInstance[];
} catch [Exception ex] {
// handle the error
}
}
}
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[]
:
Example 7.1 Connector/J: Obtaining a connection from the DriverManager
If you have not already done so, please review the portion of
Section 7.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.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
Connection conn = null;
...
try {
conn =
DriverManager.getConnection["jdbc:mysql://localhost/test?" +
"user=minty&password=greatsqldb"];
// Do something with the Connection
...
} catch [SQLException ex] {
// handle any errors
System.out.println["SQLException: " + ex.getMessage[]];
System.out.println["SQLState: " + ex.getSQLState[]];
System.out.println["VendorError: " + ex.getErrorCode[]];
}
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.
For Connector/J 8.0.24 and later: When the user for the connection is unspecified, Connector/J's implementations of the authentication plugins use by default the name of the OS user who runs the application for authentication with the MySQL server [except when the Kerberos authentication plugin is being used; see Section 6.12.2, “Connecting Using Kerberos” for details].
Note
A user name is considered unspecified only when the following conditions are all met:
The method
DriverManager.getConnection[String url, String user, String password]
is not used.The connection property
user
is not used in, for example, the connection URL,or elsewhere.The user is not mentioned in the authority of the connection URL, as in
jdbc:mysql://localhost:3306/test
, orjdbc:mysql://@localhost:3306/test
.
Notice if [1] or [2] is not true and an empty string is passed, the user name is an empty string then, and is not considered unspecified.