| Accessing MySQL on NetBeans using JDBC, Part I: Create a connection |
Introduction
This tutorial show you how to use NetBeans to connect MySQL by using MySQL Connector/J, MySQL AB’s JDBC Driver for MySQL.
I’ll divide into 2 parts:
- Part I : Create a connection
This part which you’re reading shows about how to establish a connection between NetBeans and MySQL. - Part II : Perform SQL Operations
This part show how to perform some basic operations from NetBeans to MySQL. For instance, send querys as SELECT, INSERT, UPDATE to a database.
Requirements
- MySQL Connector/J, licensed under the GPL or a commercial license
from MySQL AB. - NetBeans with JRE (Java Runtime Environment).
Step-by-Step guide
- Installation
- Install NetBeans.
- Download MySQL Connector/J, name ‘mysql-connector-java-5.0.6.zip’. (The file name may differs depends on the version if you’ve downloaded from the Official Site at here.)
- Extract the zip file to a folder, you’ll see file ‘mysql-connector-java-5.0.6-bin.jar’ which is the library file that we want. Just copy the file to the library folder, for example to “C:\Program Files\Java\jdk1.6.0_02\lib” directory.

- Add JDBC Driver to the project on NetBeans (Add a library).
Next, I create a new Java project on NetBeans named ‘TestMySQL’ and add ‘mysql-connector-java-5.0.6-bin.jar’ that I’ve just extracted from previous step to the project’s library.- Create New Project called TestSQL.

- In Projects window, right click the project name and select Properties.

- Project Properties window appears. The Categories on left side, select Libraries. And on right side in Compile tab, click Add JAR/Folder.

- New Window appears, browse to the file ‘mysql-connector-java-5.0.6-bin.jar’ and click Open.

- You’ll see the .jar file was added to the project. Click OK to finish.

Note: You should keep mysql-connector-java-5.0.6-bin.jar in the directory that you won’t delete it (ex. not in temp folder). May be in the same directory that keep common library files. If you delete the file without delete a link from the project, the project will show error about missing library.
- Create New Project called TestSQL.
- Connect to the database.
Now I’m going to write some code to connect to MySQL database. I have configured MySQL service on localhost.- I’m going to use Connection and DriverMapper Classes so I need to import libraries.
import java.sql.*;

- I’ll connect to MySQL Server on local machine, the mysql database(a default database in MySQL). In main method, add the following code.
try { Class.forName("com.mysql.jdbc.Driver"); String connectionUrl = "jdbc:mysql://localhost/mysql?" + "user=root&password=123456"; Connection con = DriverManager.getConnection(connectionUrl); } catch (SQLException e) { System.out.println("SQL Exception: "+ e.toString()); } catch (ClassNotFoundException cE) { System.out.println("Class Not Found Exception: "+ cE.toString()); }
The code explanation:
- Class.forName(”com.mysql.jdbc.Driver”); means load the MySQL driver.
- “jdbc:mysql://localhost/mysql?” + “user=root&password=123456″; is a connection string that tells to connect MySQL on localhost, select database named ‘mysql’ and user/password for MySQL server.
If you would like to connecto to other database, simply change text ‘mysql’ after ‘localhost/’ to your database name.

- Compile and run the project. If no error occurs, it means that the connection has established successfully.

- I’m going to use Connection and DriverMapper Classes so I need to import libraries.
Next part, I’ll show to how to perform some basic SQL operations to MySQL.
























December 8th, 2007 at 4:26 am
hi
i’m doiong a basketball scoreboard with it control panel on javabeans so any idea about how to connect a java application to mysql! not connecting netbeans to mysql . i’m facing some difficulties in solving this problem so would you please provide me help or send me a useful tutorial about the subject ..
thank you for your time
December 8th, 2007 at 4:37 am
no idea dear!!!!
December 20th, 2007 at 5:43 pm
Hi,
You above mentioned article is very informative. Especially for learners it is very helpful. Keep up and I am waiting for 2nd part.
December 31st, 2007 at 7:25 pm
The one who does not thank people, he does not thank the GOD
January 5th, 2008 at 2:42 am
hey all thank for your help!!!! i did the link according 2 this article and itis working..
i have another question: Does anyone know how can i connect 2 different GUIs, i want 2 link a control panel with it corresponding basketball scoreboard, i want the data to change on scoreboard each time i change it from the soreboard..
Thank you
January 5th, 2008 at 6:51 pm
sorry i want the data to change on the scoreboard each time i change it from the control panel.. :):):)
January 7th, 2008 at 3:27 pm
I’m not sure that I understand your question correctly. If you have data displayed on more than 1 output, I suggest using Timer to periodically update your data on each output.
January 7th, 2008 at 5:42 pm
Thank u Linglom!! the thing is i want to update the scoreboard each time i have an action performed on the control panel. I’m connecting the control to a database implemented on MySQL and i want the scoreboard to retieve data from the database each time i modify them from the control. this project is what we call an MVC model( Model View Controller)any IDEAs??????
Thank u again for ur help..
February 14th, 2008 at 12:50 am
Thanks for the example. For some reason I received the message: “SQL Exception: java.sql.SQLException: Access denied for user…”. I then reformatted the getConnection to be in the form of 3 arguments like this:
String connectionUrl = “jdbc:mysql://localhost/”;
Connection con = DriverManager.getConnection(connectionUrl, “User”, “Password”);
where “User” and “Password” were registered in the MySQL server, and it connected fine. Also, I don’t have a db yet, so I left out the db name.
Thanks again for taking the time to post your example.
February 15th, 2008 at 10:56 am
To Soleil Farah,
Sorry for late answer. You can implement Timer class to refresh your data on scoreboard on every x seconds.
// create a timer, xxxx is time in millisecond
Timer timer = new Timer(xxxx, new ActionListener( {
public void actionPerformed(ActionEvent evt) {
//refresh your scoreboard
}
}
// start the timer
timer.start();
To Alan,
Thank you for sharing.
February 18th, 2008 at 6:11 am
hi, that was great. thanks for the steps
iam working on it
February 27th, 2008 at 4:22 am
hi, thx that’s what i need
March 14th, 2008 at 7:24 pm
Thanks a lot for this great blog. Simplified access to Mysql.
March 23rd, 2008 at 9:42 pm
I went to Driver NetBeans for MS Access
June 4th, 2008 at 8:16 am
How can we hide the database username and password in the source code.
June 5th, 2008 at 8:54 am
If you’are using SQL Authentication method as in my example, you have to store username and password somewhere in the program maybe in code or external configuration file.
Another way, you can use Windows Authentication method which authenticate by using the current user’s credential (user that execute the application) so no need to specify username and password in application.
June 19th, 2008 at 3:37 am
Hello, I’m a completely new to Java and netbeans, I downloaded the MySQL Connector J 5.1.6 and added the address of the file in my CLASSPATH `enviroment variables`(I’m using Windows XP) , but Netbeans wasn’t able to load the drivers to connect to MySQL.
Any idea why?
I have followed your example and it works very well. So just would like to know why this is not working with the CLASSPATH method out of curiosity!
Thank you
Arnaud
June 20th, 2008 at 10:50 pm
Quote from NetBeans 5.5 Doc,
“You have to set an explicit classpath in your build scripts because the IDE ignores your environment’s CLASSPATH variable whenever it runs Ant”
Reference: http://www.netbeans.org/kb/55/using-netbeans/project_setup.html
on Managing the Classpath in Free-form Projects -> Specifying the Classpath for Project Sources
June 23rd, 2008 at 7:55 pm
Thank you so much linglom
June 23rd, 2008 at 8:01 pm
Thanks for info linglom
July 20th, 2008 at 1:37 pm
I had done it already using jdk directly but could not accomplish this without this tutorial. Thanks a lot.
July 29th, 2008 at 8:40 pm
Thanks a lot PRO for this cool, tutorial.
I have wasted a full day to know how to connect to an MySQL database using Linux UBUNTU, but I didn’t know how. But by reading your tutorial it made things very simple.
Thanks.
August 15th, 2008 at 4:42 pm
Hello people, just to inform you all, what is in this tutoriel are the basics…because to use a database in java you must includes the jdbc driver for the corresponding database server in the application classpath.
I’m facing a problem which can’t let me remotely connect to MySQL i-e:I have an application installed on different employee computers and the database installed on a server. So i have to enable remote connection for all IP addresses to my mysql instance…any idea please?
August 20th, 2008 at 10:37 pm
how would connect a MS ACCESS database using the Desktop Application route in Netbeans.
September 28th, 2008 at 10:24 pm
hi ,it is really useful ,,thanks a lot,,
October 27th, 2008 at 4:40 pm
Thanks a lot mate,
A super tutorial
I have waisted my time using the other tut (http://www.netbeans.org/kb/55/mysql.html) it works locally, but didn’t work when I tried to publish it.
cheers, sam
October 29th, 2008 at 2:03 am
Same as Yasir’s comment.Have done it through JDK but creat problem, when I run it through Netbeans. Now resolved.
Thanks
DK