Matur Nuwun Sampun Mampir Mugi-Mugi Saget Dados Ilmu Ingkang Manfaat Fiddunya Wal Akherat

Senin, 04 Agustus 2008

Penggunaan package, koneksi ke mysql

Berikut ini contoh penggunaan package dan koneksi ke MySQL. Langkah Pertama, kita buat database bernama jurnal, dengan tabel bernama jurnal, yang field-fieldnya tgl, masuk dan keluar, isi tabel tersebut.

Langkah selanjutnya, kita buat file untuk memanggil koneksi,

Berikut ini adalah listing programnya :

/**
*simpan file ini pada folder open
*simpan dengan nama connect.java
*/

package open;
import java.sql.*;

public class connect
{
private String driver=”com.mysql.jdbc.Driver”;
private String koneksi=”jdbc:mysql://localhost/jurnal”;
private String user=”root”;
private String pass=””;

protected Connection con;
protected Statement st;
protected ResultSet rs;

public connect(){}

public void buatkoneksi()throws Exception{
try{
Class.forName(driver);
con=DriverManager.getConnection(koneksi,user,pass);
}catch(Exception e){
System.out.println(”driver tidak ketemu\nada kesalahan koneksi”);
}
}

public boolean gettampiljurnal() throws Exception{
st=con.createStatement();
rs=st.executeQuery(”select * from jurnal”);
return (rs!=null);
}

public boolean getnext()throws Exception{
return rs.next();
}


public String getfield(String f)throws Exception{
return rs.getString(f);
}
}
*******************
compile file di atas
*******************
di luar folder, kita buat file untuk memanggil file koneksi di atas
/**
*simpan dengan nama jurnalmysql.java
*/

import java.sql.*;
import open.connect;
class jurnalmysql extends connect
{
public static void main(String args[])throws Exception{
connect c=new connect();
c.buatkoneksi();
if (c.gettampiljurnal())
{
System.out.println(”tgl masuk keluar saldo”);
int saldo=0;
int saldoakhir=0;
while (c.getnext())
{
String tgl=c.getfield(”tgl”);
int masuk=Integer.parseInt(c.getfield(”masuk”));
int keluar=Integer.parseInt(c.getfield(”keluar”));
saldo=(masuk-keluar);
saldoakhir=saldoakhir+saldo;
System.out.println(””+tgl+” “+masuk+” “+keluar+” “+saldoakhir);
}
System.out.println(”==============================”);
System.out.println(”saldo “+saldoakhir);
}
}
}
***************
compile dan run file di atas
***************
Berikut hasil running-nya
sharpupload.jpg

Kategory : Pemrograman Java, Database

0 komentar:


Silakan Bekomentar.!!!


Semakin banyak berkomentar, semakin banyak backlink, semakin cinta Search Engine terhadap blog anda
:a:
:b:
:c:
:1: :2: :3: :4: :5: :6:
:7: :8: :9: :10: :11: :12:

Posting Komentar

Tinggalkan Pesan

Get cash from your website. Sign up as affiliate.Join 4Shared Now! affiliate program

EARN CASH FROM YOUR SITE

Turn your valuable site visitors into income. Work online and join our free money making partner program. We offer the most commission rate to help maximize your income stream.

Join our money making program absolutely no charge and 100% risk free.

Sign Up...

Mountain a constant stream of money

Our money making system helps you to establish a constant stream of cash, 24 hours a day, 7 days a week, 365 days a year. Allowing you more time to focus on the things you love. You'll even receive money while you sleep!

Start receiving steady affiliate revenue

This money earn program really can make you profit on the same day. Start collecting serious partner income with almost no effort at all. This is a serious revenue opportunity, the first step for you to build a steady, reliable, long-time profitable business.

A constant money generator

Imagine running of a something that never failed to provide you with income-flow. A earning money program so amazingly profitable that you never had to work for a boss ever again!

Lotta Rewards Lotta Rewards