Isrg KB
The Best Way to Gain Knowledge

How to connect NetBeans IDE to MySQL Database?

NetBeans is one of the popular Integrated Development Environment (IDE) initially released in 1996 as Xelfi, and today it is one of the widely used IDE for Java and the other programming languages. In this article, we will learn how to access MySQL Database in Netbeans through Netbeans Database connection code as well as NetBeans Databased wizard.

You can download and install MySQL or MariaDB, which works perfectly fine without any issues with NetBeans IDE. An alternative to this, you may install and use XAMPP which has inbuilt MySQL database application with a stack of other software applications such as Apache, PHP, Perl and GUI version of PHP as phpMyAdmin.

Once you’ve installed MySQL or MariaDB launch the Console and create the database as per your choice. In this article, we would be naming our database as “college”, and also we would be creating a table as “student” with columns “id, name, class, age, gender”.

Create a Database
Create a Database

Create a table
Create a table

Insert data in the student table for the demonstration
Insert data in the student table for the demonstration

Once you have created the database and the table it is ready to connect to the NetBeans IDE.

Connecting Netbeans with MySQL Database programmatically

To connect NetBeans with MySQL Database:

  1. Launch NetBeans IDE application
  2. Click on File> New Project> Select Java from categories> Java Application from Projects> click on Next button
Java new Project wizard
Java new Project wizard
  1. Enter your project name and click on Finish button in our case we have taken college as the project name
Java project name
Java project name
  1. After successful creation of the project following files will be generated that you will be able to see in the project explorer
Project explorer
Project explorer
  1. Click and open College.java file located under college>Source Packages>college and add the following NetBeans database connection code in the place of “// TODO code application logic here” (you can remove this line):

  1. Press Shift+F6 or click on Run > Run file to execute the programme
  2. After execution, you may encounter “com.mysql.jdbc.Driver” error which is caused by the missing com.mysql.jdbc.Driver

How to fix com.mysql.jdbc.DriverBUILD SUCCESSFUL and add MySQL JDBC Driver Jar file?

To fix com.mysql.jdbc.DriverBUILD SUCCESSFUL and add MySQL JDBC Driver Jar file:

  1. Right-click on Libraries and select Add Library option
Java Libraries
Java Libraries
  1. In the Add Library window select MySQL JDBC Driver and click on Add Library button

Add library

  1. After adding the MySQL JDBC Driver, run the program, and after successful execution, you will be able to see the results in the Output screen as follows:
MySQL JDBC Netbeans Output

Connecting Netbeans with MySQL Database using Wizard (Master/Detail Sample form)

To Connect Netbeans with MySQL Database using Wizard (Master/Detail Sample form):

  1. Right-click on your project and select New> Others… option
  2. In the New File wizard, select “Swing GUI forms” from the categories and “Master/Detail Sample Form” from the file types and click the Next button.
New file master detail form
New file master/detail form
  1. Give a name to your class and select the package of your choice and click on the Next button
Class and package name for master detail form
Class and package name for the master-detail form
  1. On the “Master Table” window click on the Database Connection drop-down option and select “New Database Connection…” option.
  2. On the “New connection wizard” window select “MySQL (Connection/J Driver)” from the drop-down menu and click on Next button.

New connection wizard

  1. On the next window fill out your database details like host, port, database name, database username and database password (if in case your database does not have a password leave the password field empty)
New connection details
New connection details
  1. You can test the connection by clicking on the “Test Connection” button and click on the “Finish” button.
  2. Select the columns that you want to display and click on the “Next>” button.
Select columns
Select columns
  1. On “Details Option” window click on “Finish” button.
Details options
Details options
  1. After NetBeans finishes generating the necessary files, you can generate the master data sample
Netbeans master table sample
Netbeans master table sample

Once you’ve tested out MySQL database connectivity, you can create your custom projects by modifying the necessary methods and statements. You can share your feedback and query related to our this article in the comments.