Java MySQL JDBC Tutorial using NetBeans (Part 1)

Are you asking ‘Could I connect Java application/applet to database like MySQL?’ and the answer is definitely Yes.

You can use JDBC (Java Database Connectivity) to connect your Java application/applet with database. So Let’s start.

First, Create new Project named anything you want, for example Javasql by click File->New Project.


then you’ll be on this frame


then click next,  then give Project Name and set Project Localtion


then finish.

Second, you must have JDBC MySQL Driver before you can start to connect your Java program to database. But since we use Netbeans , this has been done. Just simply add the library to your project library. Right click in Library on the Project you use. addLibrary

Then choose MySQL JDBC Driver


Then Click Add Libary.

So now we can connect to MySQL database. Here is the example how you can connect to MySQL.

Here, we use interface Connection.

package javasql;
import com.mysql.jdbc.Driver;
import java.sql.*;
 * @author Ferdiansyah Dolot
public class Connect {
    public Connect() throws SQLException{

    private Connection koneksi;  

     public  Connection makeConnection() throws SQLException {
        if (koneksi == null) {
             new Driver();
            // buat koneksi
             koneksi = DriverManager.getConnection(
         return koneksi;

     public static void main(String args[]) {
         try {
             Connect c = new Connect();
             System.out.println("Connection established");
         catch (SQLException e) {
             System.err.println("Connection Failure");


In example above we create connection to MySQL from creating object of Driver and get connection from DriverManager (get Connection will return Connection Object type), with parameter the url, username, and password. The url above means the database is located in localhost and the name is databasename.You can also add port for MySQL so the url looks : “jdbc:mysql://localhost:3306/databasename”. (See port 3306).
So now we can make a connection to MySQL database. Now, how can we do SQL statement like update, delete, insert, etc?

We will discuss this in Java MySQL JDBC Tutorial using NetBeans (Part 2).

35 thoughts on “Java MySQL JDBC Tutorial using NetBeans (Part 1)

  1. Wah, bung ferdi, keren dah blognya, :D
    sana ada pertanyaan nih,
    berhubung saya pernahnya pake C# buat aplikasi, saya cuma dengar dari orang lain klo java tuh koneksi ke databasenya repot, coba bung ferdi kasi pembahasannya sedikit,

    thx ya :D ,

    1. oo paling kalo koneksi ke database dari java memerlukan jdbc driver . . .kalo di netbeans tinggal di add aja itu :D

      dicoba dulu aja bung baru bilang repot . . XD
      ~bung sely kan jagooo . . XD
      ~cewenya banyak lagi . .XD

  2. Thanks for this tutorial, a lot of tutorials on the web are really lengthly and confusing. I was looking for how to connect MySQL to Java and without this tutorial, I might still be searching. Keep up the good work. :D

  3. this is perfect.. for those who are having problems such as:
    java.sql.sqlexception no suitable driver not found
    java.lang.classnotfoundexception com.mysql.jdbc.Driver.

    this might work on your application too.

    Thanks for this post.

    You are awesome..

  4. Hi everyone !!!
    Plz i don’t know if my post is in accordance with your policy…for any problem please i present all my desolations.

    I’m a bsc student in CIT . I have a project i which i want to use java applet and jdbc in a sort of host/client :

    My applet is working properly in the host but when i deploy it on apache webserver i lose the jdbc connection.
    1-Is it possible to use applet with jdbc ?
    2-If yes how to do it ?
    3-Is there any web server in which to deploy applet and that support jdbc connection ?

    my system : fedora 8 , netbeans 6.5 , xampp, mysql .

      1. Hi!
        thanks for this tutorial ,i found it so helpful…
        Is it possible to deploy an applet on a web server ?
        i try it with xampp but it’s working given that i can access the applet from a client side through the LAN.
        But the problem is that i receive the Gui but the JDBC connection is lost ie i can retrieve data from the web server database …

        Once thanks for your help…

  5. Thank you for such a nice article. I am working on an applet for university project. And this article helped me connect it to database. I’m going to read part2 now for data modification into database.

    Keep up the nice work :)

    – Abbas From Pakistan

  6. Hi,
    i’m doing a project which using the neabeans and mysql to create a java program. i’m new with both software that why i have search some of the tutorial available online to get better understanding, That how i come to your website, the tutorial that provide by u is very easy to understanding but the problem when i execute the code it come out a branch of error which is:

    Exception in thread “main” java.lang.NoClassDefFoundError: javasql/Main
    Caused by: java.lang.ClassNotFoundException: javasql.Main
    at Method)
    at java.lang.ClassLoader.loadClass(
    at sun.misc.Launcher$AppClassLoader.loadClass(
    at java.lang.ClassLoader.loadClass(
    at java.lang.ClassLoader.loadClassInternal(
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

    I have no idea what it go wrong, your fee back may important to me. thank

  7. Thank you for your helpful tutorial. I’m using NetBeans 7.1.1 so there are a few changes since you captured your pictures. When I added the library for MySQL’s JDBC driver, the Add Library dialog box had two buttons: Create… and Import… I used Import and that worked for me.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s