Welcome to Abdul Malik Ikhsan's Blog

koneksi ke mysql, java,the Object Oriented Programming

Posted in Tutorial Java by samsonasik on April 6, 2007

di bawah terdapat contoh aplikasi untuk menampilkan jurnal :
pertama kita buat database jurnal , dan tabel jurnal, lebih rinci sebagai berikut :
mysql>create datatabase jurnal;
mysql>use jurnal;
mysql>create table jurnal(tgl date, masuk int, keluar int);
mysql>insert into jurnal values(‘2007-01-01’,1000,500);
mysql>insert into jurnal values(‘2007-01-02’,2000,1000);
===============================
nah, sekarang, kita buat programmnya, pertama kita buat base class, baru setelah itu derived class-nya, berikut base class-nya :

/* Simpan di jurnal.java
**This software is Copyleft by Abdul Malik Ikhsan
**Free to modify, if you modify, you may delete this lisence
**contact : samsonasik@gmail.com
**https://samsonasik.wordpress.com
*/

class jurnal
{
int masuk,keluar;
void setmasuk(int x)
{
this.masuk=x;
}
void setkeluar(int y)
{
this.keluar=y;
}
int getmasuk()
{
return masuk;
}
int getkeluar()
{
return keluar;
}
}
//*********************************
sedang untuk derived class untuk menampilkannya, programmnya sebagai berikut
/* Simpan di tampiljurnalmysqll.java
**This software is Copyleft by Abdul Malik Ikhsan
**Free to modify, if you modify, you may delete this lisence
**contact : samsonasik@gmail.com
**https://samsonasik.wordpress.com
*/

import java.sql.*;
class tampiljurnalmysql extends jurnal
{
public static void main(String args[]){

try{
Class.forName(“com.mysql.jdbc.Driver”).newInstance();
} catch (Exception e) {
System.out.println(“tidak dapat loading driver”);
System.exit(1); }

try{
String koneksi=”jdbc:mysql://localhost/jurnal”;
String user=”root”;
String pass=””;
Connection con = DriverManager.getConnection(koneksi,user,pass);
Statement st = con.createStatement();
String sql=”select * from jurnal order by tgl”;

ResultSet rs=st.executeQuery(sql);

jurnal x=new jurnal();
System.out.println(“=======Jurnal Harian======”);
System.out.println(“no   tgl           masuk        keluar     saldo”);
int no=0;
int saldosaturecord=0;
int saldosemuarecord=0;
while (rs.next())
{
no++;

Date tgl=rs.getDate(“tgl”);

int masuk=rs.getInt(“masuk”);
x.setmasuk(masuk);
int keluar=rs.getInt(“keluar”);
x.setkeluar(keluar);
saldosaturecord=masuk-keluar;
//loop saldo dengan menambahkan saldo next record
//dengan record sebelumnya
saldosemuarecord=saldosemuarecord+saldosaturecord;

System.out.println(no+”   “+tgl+”      “+x.getmasuk()+”     “+x.getkeluar()+”     “+saldosemuarecord+””);
}
System.out.println(“==============================”);
System.out.println(“saldo total   “+saldosemuarecord);
}
catch(SQLException ee){
System.out.println(“ada kesalahan pada koneksi”);
}

}}

Advertisements

One Response

Subscribe to comments with RSS.

  1. insansains said, on May 8, 2008 at 4:34 am

    ^_^ Thanks banget atas ulasannya. Barusan nyobain n sukses!! Salam kenal!


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: