Koneksi Java dg MySql

Posted: November 29, 2010 in Java
Tags: , , ,

Buat Class Koneksi dulu, untuk menghubungkan aplikasi Java dengan Mysql.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import com.mysql.jdbc.Driver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
 *
 * @author kurniawandwi
 */
public class koneksi {
    public static Connection koneksi;
    public static Connection getConnection() throws SQLException{
        if(koneksi == null){
            new Driver();
            koneksi = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_meubel","root","");
        }
        return koneksi;
    }

    public static void main(String args[]){
        try{
            getConnection();
            System.out.println("Koneksi Sukses");
        }
        catch(SQLException ex){
            System.err.println("Koneksi Gagal");
        }
    }
}

buat class untuk Mencoba Menampilkan data dari database Mysql, contoh :

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
 *
 * @author kurniawandwi
 */
public class select_database {
    public static void main(String args[]) throws SQLException{
        Statement string = koneksi.getConnection().createStatement();

        //contoh SQL Query
        //string.executeUpdate("INSERT INTO bukutamu VALUES(null,'joko','gresik','kurniawandwi.h@gmail.com','cek bro','25 Juni 2010');");
        //string.executeUpdate("UPDATE bukutamu SET nm_bt = 'Kurniawan Dwi' WHERE id_bukutamu = '65'");
        //string.executeUpdate("DELETE FROM bukutamu WHERE id_bukutamu = '64'");

        ResultSet result = string.executeQuery("SELECT * FROM bukutamu");

        String id_bukutamu = null, nm_bt = null,almt_bt = null,email_bt = null,komentar_bt = null,tgl_bt = null;
        int index = 1;
        while(result.next()){
            id_bukutamu = result.getString("id_bukutamu");
            nm_bt = result.getString("nm_bt");
            almt_bt = result.getString("almt_bt");
            email_bt = result.getString("email_bt");
            komentar_bt = result.getString("komentar_bt");
            tgl_bt = result.getString("tgl_bt");

            System.out.println(" Id BukuTamu : "+id_bukutamu+", Nama : "+nm_bt+", Tanggal : "+tgl_bt);
            index++;
        }
    }
}

Hasil Running :

Comments
  1. […] This post was mentioned on Twitter by Alltop Java, kurniawan dwi. kurniawan dwi said: Koneksi Java dg MySql: http://wp.me/pKm1l-6c […]

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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