Data Base Console java Sql

Posted: Juli 6, 2011 in IT, Java, KBM, kuliah

Dibawah ini saya mencoba belajar java dengan menggunakan database java Mysql

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package databaseConsole;
import java.sql.*;

/**
*
* @author farus mercu
*/
public class KoneksiMysql {
private String host=”localhost”;
private String user=”root”;
private String password=”farus”;
private String db=”barang”;
public KoneksiMysql() {
Connection conn;
Statement st;
ResultSet rs;
int no=0;
try {
Class.forName(“com.mysql.jdbc.Driver”);
conn = DriverManager.getConnection(

“jdbc:mysql://”+ host +”/”+ db +”?user=” + user +
“&password=”+ password);
st = conn.createStatement();
rs = st.executeQuery(“Select * from barang”);
while (rs.next()) {
no=no+1;
System.out.print(no + “)”);
System.out.println(“kd_brg” + rs.getString(“kd_brg”));
System.out.print(“Nama :” + rs.getString(“nm_brg”));
System.out.println(“============” + “====================”);
}
conn.close();
}
catch(Exception e) {
System.out.println(“Pesan kesalahan : “+ e.getMessage());
}
}
public static void main(String[] args) {
KoneksiMysql koneksimysql = new KoneksiMysql();
}
}
selamat mencoba sobat

Komentar
  1. fanz mengatakan:

    jadi inget semester kemaren belajar java🙂

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s