Menampilkan data dari Database ke jTable di java

//setting koneksi database sesuai dengan user dan passwordnya

public Connection conn;
public void koneksi() throws SQLException{
try{
conn=null;
Class.forName(“com.mysql.jdbc.Driver”);
conn = DriverManager.getConnection(“jdbc:mysql://localhost:3306/mydb”,”root”,””);
}catch(ClassNotFoundException ex){
Logger.getLogger(FUtama.class.getName()).log(Level.SEVERE, null, ex);
}catch (SQLException e){
Logger.getLogger(FUtama.class.getName()).log(Level.SEVERE, null, e);
}catch (Exception es){
Logger.getLogger(FUtama.class.getName()).log(Level.SEVERE, null, es);
}
}

//    perintah try catch nya
try{
koneksi();
String sql = “select * from tableName”;
Statement stat = conn.createStatement();
ResultSet res = stat.executeQuery(sql);
while (res.next()){
tabelmapel.addRow(new Object[]{res.getString(1),res.getString(2),res.getString(3),res.getString(4),
res.getString(5), res.getString(6), res.getString(7), res.getString(8)});
}
tblTampil.setModel(tabelmapel);
} catch(Exception e){
System.out.println(“Terjadi kesalahan, “+e);
}

Advertisements

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