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 Responses to Java MySQL JDBC Tutorial using NetBeans (Part 1)

  1. deni says:

    mantap lot! :D
    nanti gw submit ke regional yah linknya..
    kalo bisa pake bahasa indo next time, biar bantu orang2 indo :)

  2. Sely Haudy says:

    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 ,

    • Ferdiansyah Dolot says:

      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

  3. nikita says:

    Hey its really Helpful to me
    Thanks alot

  4. Pleased student says:

    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

  5. ahmmy says:

    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..

  6. soulemane says:

    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 .

  7. 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

  8. linus says:

    Thanks a lot for the tutorial, it was really helpful. :)

  9. payungjagad says:

    mantap gan…

    untuk select ma sum gimana tuccchhh bagi ceritanya dunk …

  10. ffaaddiill says:

    thank om .. saya coba dulu .. buat tugas kampus .. hahahah :D

  11. usuario555 says:

    gracias amigo..
    you explain excellent. it’s perfect.. I hope you’ll continue the tutorial


  12. risa says:

    kenapa di mysql aku masih berhasil ngisi….??? TT__TT

  13. Pingback: JavaApplet MySQL JDBC Tutorial Using Netbeans « Ferdiansyah Dolot

  14. GInoL says:

    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

  15. Mandar says:

    Thanks a lot :)

  16. aspiring to iron code says:

    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.

  17. sperker says:

    thanks gan…bisa dibaca2 juga di ebook “manage and using musql chapter 14”

  18. srikanth ganta says:

    Reblogged this on Srikanth's Blog.

  19. Felipe Franco says:

    Thanks was a great help


  20. itz says:

    habia implementado mucho codigo de conexion pero solo este funciono… grax

  21. Daniyal says:

    Thank you so much for the awesome tutorial. Took me almost 2 hours to reach this tutorial and it worked like magic!

    Thank you so much!

  22. hima says:

    Hey its really Helpful to me
    Thanks alot

  23. Zunain Ahmad says:

    package javasql;

    i am getting red line under it..
    means error

  24. thank you so much for the grate tutorial

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


Get every new post delivered to your Inbox.

%d bloggers like this: