This document demonstrates how to configure MySQL on your system and set up a connection to the database from NetBeans IDE. Once connected, you can begin working with MySQL in the IDE's Database explorer by creating new tables, populating tables with data, and running SQL queries on database structures and content. This tutorial is designed for beginners with a basic understanding of database management and application development, who want to apply their knowledge in working with MySQL in NetBeans IDE.
MySQL is a popular Open Source database management system commonly used in web applications due to its speed, flexibility and reliability. MySQL employs SQL, or Structured Query Language, for accessing and processing data contained in databases.
Note: This document uses the NetBeans IDE 5.5 Release. If you are using NetBeans IDE 6.5, see Connecting to a MySQL Database.
Expected duration: 25 minutes
The following topics are covered below:
Before you begin, make sure you have the following software installed on your computer:
Note: As an alternative to downloading the IDE and JDK separately, consider downloading the Java SE Development Kit 6u1 with NetBeans IDE 5.5 Bundle.
If you already have the MySQL database set up and running on your machine, please skip ahead to Registering the Database in NetBeans IDE. By way of example, this tutorial demonstrates how to run the MySQL Community Server on Windows XP. To get the MySQL server running:

Before continuing further, it is important to understand the components found in MySQL's root directory:
Now that the MySQL database server is installed and configured, you can create a database instance which you will later connect to in NetBeans IDE. To create a new database instance:
Now that you have the database server installed and configured, and have created a new database,
you can register the MySQL server in NetBeans IDE. Begin by examining the functionality offered
by the Database explorer located in the IDE's Runtime window (Ctrl+5).
The Database explorer is represented by the Databases node
(
).
From this interface you can connect to databases, view current
connections, add database drivers, as well as create, browse or edit database
structures.
You can use the Database explorer to register MySQL in the IDE. There are two simple steps that need to be performed:
In order to allow NetBeans IDE to communicate with a database, you need to employ a Java-based driver. Generally speaking, drivers in NetBeans IDE use the JDBC (Java Database Connectivity) API to communicate with databases supporting SQL. The JDBC API is contained in the java.sql Java package. A driver therefore serves as an interface that converts JDBC calls directly or indirectly into a specific database protocol.
In this tutorial, you are using the MySQL Connector/J driver, which is a pure Java implementation of the JDBC API, and communicates directly with the MySQL server using the MySQL protocol. To add the driver to the IDE:

Note: While you just made the database driver available to the IDE, you have not yet made it available to any specific application. At this stage, you can use the IDE to access and modify the database, but cannot do so from an application yet. This is covered in the follow-up tutorial Creating a Simple Web Application in NetBeans IDE using MySQL.
You can now set up a connection to the database through the appropriate driver. Make sure your database service is running prior to continuing. You can do so by performing the following simple test:
Now, in NetBeans IDE, create a connection to MyNewDatabase:

You are now connected to MyNewDatabase in the IDE. Note that the new connection
node icon appears whole (
) when you are connected to a database. Likewise, it appears
broken (
) when there is no connection.
At later stages, when working with databases through the Database explorer, you may need to manually connect to a database. You can do so by right-clicking the broken database connection node and choosing Connect.
Now that you have connected to the database, you can begin exploring how to create tables, populate them with data, and modify data maintained in tables. This allows you to take a closer look at the functionality offered by the Database explorer, as well as NetBeans IDE's support for SQL files. As an example, you can prepare the database for use in the web application developed in the follow-up tutorial Creating a Simple Web Application in NetBeans IDE using MySQL.
The MyNewDatabase database that you are using is currently empty. In NetBeans IDE it is possible to add a database table by either using the Create Table dialog, or by inputting an SQL query and running it directly from the SQL editor. Here you can explore both methods:
| Key | Index | Null | Unique | Column name | Data type | Size |
|---|---|---|---|---|---|---|
| [checked] | [checked] | [checked] | id | SMALLINT | 0 | |
| [checked] | name | VARCHAR | 50 | |||
| [checked] | description | VARCHAR | 500 | |||
| [checked] | counselor_idfk | SMALLINT | 0 |
)
display under Tables in the Database explorer. Beneath the table node
there are the columns (fields) you created, starting with the primary key (

CREATE TABLE Counselor (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
first_name VARCHAR (50),
nick_name VARCHAR (50),
last_name VARCHAR (50),
telephone VARCHAR (25),
email VARCHAR (50),
member_since DATE DEFAULT '0000-00-00',
PRIMARY KEY (id)
);
Note: Queries formed in the SQL Editor are parsed in
Structured Query Language (SQL). SQL adheres to strict syntax rules which you
should be familiar with when working in the IDE's Editor. Upon running
a query, feedback from the SQL engine is generated in the Output window
indicating whether execution was successful or not.
) button in the task
bar at the top (or, press Ctrl+Shift+E to execute the query).
You should receive the following message in the Output window:
) now displays under Tables in the Database
explorer.
In order to work with table data, you can make use of the SQL Editor in NetBeans IDE. By running SQL queries on a database, you can add, modify and delete data maintained in database structures. To add a new record (row) to the Counselor table, do the following:
INSERT INTO Counselor
VALUES (1, 'Ricky', '"The Dragon"', 'Steamboat','334 612-5678', 'r_steamboat@ifpwafcad.com', '1996-01-01')
) button in
the task bar at the top, or press Ctrl+Shift+E to execute the query.
You should receive a message in the Output window indicating that the query was
executed successfully.
Another way to manage table data in NetBeans IDE is by running an external SQL script directly in the IDE. If you have created an SQL script elsewhere, you can simply open it in NetBeans IDE and run it in the SQL Editor.
For demonstrative purposes, download ifpwafcad.sql and save it to a location on your computer. This script creates two tables similar to what you just created above (Counselor and Subject), and immediately populates them with data. To run this script on MyNewDatabase:
) button in the SQL Editor's task bar. The script is
executed against the selected database, and any feedback is generated in
the Output window.This concludes the Connecting to a MySQL Database in NetBeans IDE tutorial. This document demonstrated how to quickly configure MySQL on your system and set up a connection to the database from NetBeans IDE. It also described how to work with MySQL in the IDE's Database explorer by creating new tables, populating tables with data, and running SQL queries.
For related and more advanced tutorials, see the following resources: