Welcome to Abdul Malik Ikhsan's Blog

java koneksi postgresql

Posted in Tutorial Java by samsonasik on April 27, 2007

pertama, kita buat dulu database di postgresql, misalnya namanya “coba”, isinya ada tabel dengan nama barang, field-fieldnya kode_barang,nama_barang,dan harga, setelah itu download driver postgresql di http://jdbc.postgresql.org/download.html , extract di folder tempat file *.java kita,,
berikut syntaxnya :
/*
**this software is opensource
**created by Abdul Malik Ikhsan
**if you modify, you may delete this lisence
*/

import java.sql.*;
class connect
{
 public static void main(String args[]){
  try{
   Class.forName(“org.postgresql.Driver”);
  }catch(Exception e){
   System.out.print(“driver tidak ketemu”);
  }

  try{
   String koneksi=”jdbc:postgresql://localhost/coba”;
   String user=”samsonasik”; //superuser postgresql
   String pass=”123456″; //password superuser
   Connection con=DriverManager.getConnection(koneksi,user,pass);
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(“select * from barang”);

int no=0;
while (rs.next())
{
 no++;
 String kb=rs.getString(“kode_barang”);
 String nb=rs.getString(“nama_barang”);
 int hb=rs.getInt(“harga”);

 System.out.println(no+”.   “+kb+”        “+nb+”      “+hb);
}

 }catch(Exception e){
   System.out.print(“koneksi salah”);
  } }
}

9 Responses

Subscribe to comments with RSS.

  1. slamet said, on April 19, 2008 at 1:14 pm

    itu drivernya ditaruh dijava mana?
    kok masih tetep ndak connet drivernya?

    tak tunggu ya jawabanya 🙂 emailku / ymku or4ng_hilang@yahoo.com

  2. samsonasik said, on April 19, 2008 at 6:34 pm

    coba taruh di folder yang sama tempat aplikasi yang kita buat, atau set classpath-nya ke alamat driver kita :).

  3. Roy Jhon said, on April 19, 2008 at 7:24 pm

    Mas, minta program java koneksi postgresql dong, view, delete, input, add.

    🙂

  4. HORAS said, on April 19, 2008 at 7:25 pm

    Tess

  5. Nuchis said, on June 23, 2008 at 12:42 am

    mas minta program java koneksi postgresql yach, view, delete, input,insert.

    tlng kirimin d email aq yach mas, kl bs secepatx aq tunggu :

    (nucha_pucha@yahoo.com)

    Makasi…

  6. Muh Muslih said, on March 8, 2009 at 1:50 pm

    mas ane juga minta java koneksi postgresql yah, view, input, delete, insert
    moeh_moeslieh@yahoo.com

  7. tundra said, on May 6, 2009 at 4:28 am

    Saya mw bertanya bgm mengambil nama tabel yg ada di DBMS PostgreSQL.
    saya lihat dibuku dengan contoh yg digunakan utk DBMS MySQL, di MySQL Berjalan tp di PostgreSQL tidak bisa..jd nama tabelnya tidak ada kosong.

    import java.sql.*;

    public class StrukturTabel {
    public static void getRsMetadata( Connection con, String query){
    try{
    Statement select = con.createStatement ();
    ResultSet result = select.executeQuery (query);
    ResultSetMetaData meta = result.getMetaData( );

    System.out.println( “Nama Tabel= “+meta.getTableName (1));
    System.out.println( “Kolom “+”\t Tipe “+”\t Ukuran”+”\t isNullable”) ;
    int columns=meta. getColumnCount( );
    for (int i=1; i<=columns;i+ +){
    System.out.print( meta.getColumnLa bel(i)+ “\t ” +
    meta.getColumnTypeN ame(i)+ ” \t” +
    meta.getColumnDispl aySize(i) );

    if (meta.isNullable( i)==ResultSetMet aData.columnNull able)
    System.out.println( “\tTrue”) ;
    else
    System.out.println( “\tFalse” );

    }

    System.out.println( “Jumlah Kolom = “+columns);
    System.out.println( );
    select.close( );

    }catch(Exception ex){
    System.err.println( “Exception: “+ex.getMessage( ));
    }
    }

    public static void main(String[ ] args) throws Exception {
    try{
    Class.forName( “org.postgresql. Driver”);
    }catch(Exception ex){
    System.err.println( “Exception: “+ex.getMessage( ));
    }
    Connection con = DriverManager. getConnection( “jdbc:postgresql: / /localhost/ tokomajalah? user=java& password= latjava”);
    try{
    getRsMetadata( con, “SELECT *FROM majalah”);
    getRsMetadata( con, “SELECT *FROM penerbit”);

    con.close();

    }catch(Exception ex){
    System.err.println( “Exception: “+ex.getMessage( ));
    }
    }
    }

    Output:

    Nama Tabel=
    Kolom Tipe Ukuran isNullable
    mjh_id int4 11 True
    nama_mjh varchar 32 True
    pend_id int4 11 True
    harga numeric 131089 True
    terjual int4 11 True
    total int4 11 True
    Jumlah Kolom = 6

    Nama Tabel=
    Kolom Tipe Ukuran isNullable
    pen_id int4 11 True
    nama_pen varchar 40 True
    jalan varchar 30 True
    kota varchar 20 True
    provinsi varchar 20 True
    kode_pos bpchar 5 True
    Jumlah Kolom = 6

    Mohon pencerahannya ^_^

  8. mulkan said, on May 25, 2010 at 2:40 am

    Java untuk pengolahan citra

  9. rainzch said, on November 16, 2011 at 4:30 pm

    skalian juga gan, ane juga minta java koneksi postgresql yah, view, input, delete, insert
    ahmadrizkyka@yahoo.co.id
    😀


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

%d bloggers like this: