import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
public class matapelajaran extends javax.swing.JFrame {
/** Creates new form matapelajaran */
public matapelajaran() {
initComponents();
tampilkandata();
}
public Connection conn;
public void koneksi() throws SQLException {
try {
conn=null;
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/akademik", "root", "");
} catch (ClassNotFoundException ex) {
Logger.getLogger(matapelajaran.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException e) {
Logger.getLogger(matapelajaran.class.getName()).log(Level.SEVERE, null, e);
} catch (Exception es) {
Logger.getLogger(matapelajaran.class.getName()).log(Level.SEVERE, null, es);
}
}
public void tampilkandata() {
DefaultTableModel tabelmapel = new DefaultTableModel();
tabelmapel.addColumn("KODE MAPEL");
tabelmapel.addColumn("NAMA MAPEL");
try {
koneksi();
String sql = "select * from barang";
Statement stat = conn.createStatement();
ResultSet res=stat.executeQuery(sql);
while (res.next()) {
tabelmapel.addRow(new Object[]{res.getString(1),res.getString(2)});
}
jTable1.setModel(tabelmapel);
} catch (Exception e) {
}
}