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 frame1 extends javax.swing.JFrame {
/** Creates new form frame1 */
public frame1() {
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/dbsi7", "root", "");
} catch (ClassNotFoundException ex) {
Logger.getLogger(frame1.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException e) {
Logger.getLogger(frame1.class.getName()).log(Level.SEVERE, null, e);
} catch (Exception es) {
Logger.getLogger(frame1.class.getName()).log(Level.SEVERE, null, es);
}
}
public void tampilkandata() {
DefaultTableModel tabelmhs = new DefaultTableModel();
tabelmhs.addColumn("NIM");
tabelmhs.addColumn("NAMA");
try {
koneksi();
String sql = "select * from mhs";
Statement stat = conn.createStatement();
ResultSet res=stat.executeQuery(sql);
while (res.next()) {
tabelmhs.addRow(new Object[]{res.getString(1),res.getString(2)});
}
jTable1.setModel(tabelmhs);
} catch (Exception e) {
}
}