We often use associated databases when building Java Desktop applications through NetBeans. Therefore, this article will introduce how to create a MySQL database connection from NetBeans.
Before connecting, install the following software:
(1) NetBeans 6.5
(2) JDK 6 (Update 13)
(3) MySQL 5
You can install NetBeans after installing JDK, and then install MySQL. Remember the port number (usually 3306 by default) and database password when installing MySQL. Next, I will introduce in detail how to connect to the MySQL database in NetBeans. This document uses the database named cars as an example.
1. Start the MySQL command line client, enter the database Password, Run "create database cars;" to create a database named cars, and run "show databases;" to check whether the database is created successfully. As follows:
2. Start NetBeans 6.5, click "service", select "Database", right-click and create a connection. The following window is displayed, as shown in. the specific port and database password will be related to the settings when MySQL is installed. The MySQL port here adopts the default 3306, and the database password is set to 33123. click OK.
3. Click "OK" to check that the connection is successful, as shown in:
4. After the database connection is established, right-click "jdbc: mysql: // localhost: 3306/cars" and choose "Run Command" to write and execute the SQL script, the preceding SQL script is used to create a table named car in the cars database. The table contains multiple attributes, such as the car brand (make) and model (size, and contains many fields ). After the script is imported, run the script language.
As shown in:
5. Click "jdbc: mysql: // localhost: 3306/cars". You can see that a table named car has been created in the cars database, as shown in:
6. In NetBeans, click "file", select "New Project", select "java", and select "java Desktop Application", for example:
7. Click "Next" on Step 6 and select "database application", as shown in:
8. Click "Next" on top of Step 7. In "database connection", select a connection established in step 3 and select the attribute to be displayed, click "Next", as shown in the following two figures:
9. After completing the above eight steps, you can run the main project, as shown in:
You can also add, delete, and perform other operations.
In addition, you can add the Swing control to further improve the functions of this application.
- Top eight technical talents rating NetBeans 6.5
- Release of NetBeans IDE 6.7 beta
- NetBeans IDE 6.5.1 released enhanced network and Java Development
- The NetBeans cooperation system is becoming increasingly mature. Third-party support is gradually increasing.
- Added Python support for NetBeans IDE