import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class LihatDatabase {
public static void main(String [] args) {
LihatData data = new LihatData();
}
}
class LihatData extends JFrame{
public LihatData() {
setTitle("Lihat Database");
String tdata[][] = new String[0][0];
try{
//Untuk Database menggunakan SQL
/*Class.forName("org.gjt.mm.mysql.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/DBpraktikum","root","");*/
//Untuk Database menggunakan Access
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:coba","","");
Statement stmt = conn.createStatement();
//Sintaks sql yang dibawa
String sql="select * from mahasiswa";
//Hasilnya ditampung disini
ResultSet rs = stmt.executeQuery(sql);
tdata = new String[500][3];
int p=0;
//Jika data berikutnya ada, maka...
while (rs.next()){
//kita dapatkan data dari kolom 1
tdata[p][0] = rs.getString(1);
tdata[p][1] = rs.getString(2);
tdata[p][2] = rs.getString(3);
p++;
}
//stmt dibebaskan dari memori
stmt.close();
//koneksi ditutup
conn.close();
}
catch (Exception exc){
JOptionPane.showMessageDialog(this,"ERROR","Hasil",JOptionPane.ERROR_MESSAGE);
}
String[] nkolom={"Nim","Nama","Alamat"};
JTable tabel = new JTable(tdata,nkolom);
JScrollPane gridtabel = new JScrollPane(tabel);
tabel.setPreferredScrollableViewportSize(new Dimension(400,300));
setLayout(new FlowLayout());
add(gridtabel);
pack();
setDefaultCloseOperation(3);
setVisible(true);
}
}
0 Response to "Membuat Menu Lihat Data dari database praktikum"
Post a Comment