nampilin data dari mysql


Compiler : java

import java.sql.*;
public class menampilkanData
{
public static void main(String[] args)
{
String url=”jdbc:odbc:tokomajalah”; //settingan odbc
String username=”root”; //username dtbase
String password=” “; //password username
Connection kon;
Statement stat;
ResultSet rs;
String queri;
String nm_majalah;
int harga,no_penerbit,jml;

//excepsi buat driver
try
{
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);

}
catch (java.lang.ClassNotFoundException e)
{
System.err.println(“pesan salahnya =”+e.getMessage());

}

try
{
kon=DriverManager.getConnection(url,username,password);
stat=kon.createStatement();

//query untuk menapilkan data
queri=”select namaMjlh,PenId,harga from majalah”;
rs=stat.executeQuery(queri);

System.out.println(“======================================”);
System.out.println(” Daftar majalah “);
System.out.println(“======================================”);

jml=0;
while(rs.next())
{
nm_majalah=rs.getString(“namaMjlh”);
no_penerbit=rs.getInt(“penId”);
harga=rs.getInt(“harga”);
System.out.println(“nama Majalah :”+nm_majalah);
System.out.println(“ID penerbit :”+no_penerbit);
System.out.println(“Harga majalah : Rp”+harga);

jml++;

}

System.out.println(“=====================================”);
System.out.println(“jumlah majalah=”+jml+” buah”);

//menutup koneksi
stat.close();
kon.close();
}
catch (SQLException ec)
{
System.err.println(“errornya ialah”+ec.getMessage());
}

}
}

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: