Downloading and Installing NetBeans IDE
8. NetBeans and MySQL
Reference: "Connecting to a MySQL Database" @ http://netbeans.org/kb/docs/ide/mysql.html.
NetBeans (JavaEE) provides direct support to MySQL server. You can use NetBeans as a GUI client to access a MySQL server, as well as an administrative tool (e.g., starting and stopping the server).
Configuring NetBeans to Support MySQL
From NetBeans "Window" menu ⇒ Select "Services". The "Services" tab shall appear on the left pane
- Right-click on the "Databases" node ⇒ "Register MySQL Server". (If you have already registered a MySQL server, you can right-click on Server node "
MySQL Server at hostname:port" ⇒ Properties, to modify its properties.)
- Select the "Basic Properties" tab, enter the hostname, port number, root user and password.
- Select the "Admin Properties" tab:
- Leave the "Path/URL to admin tool" empty.
- In "Path to start command", enter "
<MYSQL_HOME>\bin\mysqld.exe"; in the "Arguments", enter "
- In "Path to stop command", enter "
<MYSQL_HOME>\bin\mysqladmin.exe", in the "Arguments", enter "
-u root -ppassword shutdown".
- A server node "
MySQL Server at hostname:port" appears.
Database Administration - Start/Stop the Server and Create Databases
- You can start the MySQL server by right-clicking on the server node ⇒ select "start". [There seems to be a problem here. If a "connection refused: connect" error occurs, enter the password again.]
- Once the MySQL server is started and connected, you can see the list of databases by expanding the MySQL server node. You can create a new database by right-clicking on it and choose "Create Database...".
Create a new Connection
You need a connection to manipulate data. You can create multiple connections with different users and default databases.
- Right-click on the "Databases" ⇒ "New Connection..." ⇒ Select the driver "MySQL Connector/J" ⇒ Next ⇒ Enter hostname, port number, default database, a general username and password ⇒ "Test Connection" (make sure that MySQL is started) ⇒ Finish.
- A connection node "
Manipulating Data via a Connection
- Right-click on a connection node (e.g., "
jdbc:mysql://hostname:port/defaultDatabase") ⇒ Choose "Connect" (if not connected, provided that the MySQL server has been started).
- You can expand the connection node to view all the databases.
- Expand an existing database. There are three sub-nodes "Tables", "View" and "Procedures". Right-click on the "Tables" to create table or execute command. Similarly, right-click on the "View" and "Procedures".
- To view/manipulate the records in a table, right-click on the selected table ⇒ You can choose to "View Data...", "Execute Command...", etc.
- You can right-click on the connection to "connect" or "disconnect" from the server.
Create a SQL Script and Run the Script
You can create a SQL script by right-clicking on a project ⇒ New ⇒ "SQL File". You can run the script by right-clicking on the SQL script ⇒ "Run File" ⇒ Select an existing connection (or create a new connection) to run the script. You could also run a single statement (right-click on the statement ⇒ Run Statement) or a selected group of statements (highlight the statements ⇒ Right-click ⇒ Run Selection).