Java MySQL JDBC Tutorial using NetBeans (Part 1)

June 14, 2009 at 3:05 pm | In Java, Programming | 12 Comments
Tags: , ,

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.

newProject

newProject

then you’ll be on this frame

javaapps

javaapps

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

nameProject

nameProject

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

addLib

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{
        makeConnection();
    } 

    private Connection koneksi;  

     public  Connection makeConnection() throws SQLException {
        if (koneksi == null) {
             new Driver();
            // buat koneksi
             koneksi = DriverManager.getConnection(
                       "jdbc:mysql://localhost/databasename",
                       "username",
                       "password");
         }
         return koneksi;
     }  

     public static void main(String args[]) {
         try {
             Connect c = new Connect();
             System.out.println("Connection established");
         }
         catch (SQLException e) {
             e.printStackTrace();
             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) (I’ll write it soon).  ^_^.

Thanks for reading.

12 Comments »

RSS feed for comments on this post. TrackBack URI

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

    • ^_^ thx
      ok bos, jangan lupa oleh2 ya :)

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

    • 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. Hey its really Helpful to me
    Thanks alot

    • welcome :)

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

    • you’re welcome . . .
      thanks for reading . .:)

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

    • Thanks.
      Glad I could help. :)

  6. 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.
    Questions:
    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 .


Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.