This document demonstrates how to set up a connection to a MySQL database from NetBeans IDE. Once connected, you can begin working with MySQL in the IDE's Database Explorer by creating new databases and 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, who want to apply their knowledge to working with MySQL in NetBeans IDE.
MySQL is a popular Open Source relational database management system (RDBMS) 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 6.0 Release. If you are using NetBeans IDE 6.1, see Connecting to a MySQL Database. If you are using NetBeans IDE 6.5, see Connecting to a MySQL Database.
Contents

To follow this tutorial, you need the following software and resources.
Notes:
Connections to databases are managed using database drivers, which enable applications written in different programming languages to interact with the database management system. NetBeans IDE 6.0 comes bundled with 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.
Begin by examining the functionality offered by the Database Explorer located in the IDE's Services window (Ctrl-5). The Database Explorer is represented by the Databases node (
). From this interface you can connect to database servers, view current connections, add database drivers, as well as create, browse or edit database structures.
Now that you have the MySQL database server installed and configured, you can connect to it from the NetBeans IDE using the Database Explorer. Note that you are connecting to the database server. In the next step, you will create a database instance through this connection. Do the following:

You are now connected to the MySQL RDBMS 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.
A common way of interacting with databases is through an SQL editor. NetBeans IDE has a built-in SQL Editor for this purpose. The SQL Editor is generally accessible via the Execute Command option from the right-click menu of the connection node (or of the connection node's child nodes). Now that you are connected to the MySQL server, you can create a new database instance using the SQL Editor. For purposes of this tutorial, create an instance called MyNewDatabase:
create database MyNewDatabase;
Then, right-click anywhere within the SQL Editor and choose Run Statement. The SQL query executes against the database. In the Output window (Ctrl-4) you will see output similar to the following, indicating that the query executed successfully:
You can now connect to the MyNewDatabase database instance that you just created from within the IDE. To do so, follow the same procedure used above when connecting to the MySQL RDBMS:
Click OK, then click OK again to exit the dialog. A new Connection node displays in the Database Explorer under the Databases node, just as it did when you connected to the MySQL server.Note: While you can now access MyNewDatabase through your connection in 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 within the context of an application yet. This is covered in the follow-up tutorial Creating a Simple Web Application using a MySQL Database.
Now that you have connected to MyNewDatabase, 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.
MyNewDatabase is currently empty. In the 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:
CREATE TABLE Counselor (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
firstName VARCHAR (50),
nickName VARCHAR (50),
lastName VARCHAR (50),
telephone VARCHAR (25),
email VARCHAR (50),
memberSince 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 (Ctrl-Shift-E), or right-click within the SQL Editor and choose Run Statement. The IDE generates the Counselor table in the database, and you receive a message similar to the following in the Output window:
To verify changes, right-click the Tables node in the Database Explorer and choose Refresh. The Refresh option updates the Database Explorer's UI component to the current status of the specified database. Note that the new Counselor table node (
) now displays under Tables in the Database explorer. If you expand the table node you can see the columns (fields) you created, starting with the primary key (

) immediately display 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')
To execute the query, right-click within the SQL Editor and choose Run Statement. In the Output window, you can see a message indicating that the query was successfully executed. To verify that the new record has been added to the Counselor table, in the Database Explorer, right-click the Counselor table node and choose View Data. A new SQL Editor pane opens in the main window. When you choose View Data, a query to select all the data from the table is automatically generated in the upper region of the SQL Editor. The results of the statement are displayed in a table view in the lower region. In this example, the Counselor table displays. Note that a new row has been added with the data you just supplied from the SQL query:
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.
Because the script overwrites these tables if they already exist, delete the Counselor and Subject tables now so it becomes obvious that new tables are being created when the script is run. To delete tables:
To run the SQL script on MyNewDatabase:
Click the Run SQL (
) 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. To verify changes, right-click the MyNewDatabase connection node in the Runtime window and choose Refresh. The Refresh option updates the Database Explorer's UI component to the current status of the specified database. Note that the two new tables from the SQL script now display as a table nodes under MyNewDatabase in the Database Explorer. Choose View Data from the right-click menu of a selected table node to see the data contained in the new tables. In this manner, you can compare the tabular data with the data contained in the SQL script to see that they match.This concludes the Connecting to a MySQL Database tutorial. This document demonstrated how to configure MySQL on your computer and set up a connection to the database server from NetBeans IDE. It also described how to work with MySQL in the IDE's Database Explorer by creating new database instances and tables, populating tables with data, and running SQL queries.
For related and more advanced tutorials, see the following resources:
You are viewing a mobilized version of this site...
View original page here