berikut gambaran tabel jurusan di database
Berikut Coding Aplikasi Dabatase untuk ComboBox pada Java Visual
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
class ComboBoxDatabase extends JFrame
{
JLabel namajurusan = new JLabel("DATA JURUSAN :");
JComboBox cbjur = new JComboBox();
ComboBoxDatabase()
{
setTitle("Aplikasi Database ComboBox");
setLocation(0,0);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,130);
}
void komponenVisual()
{
getContentPane().setLayout(null);
getContentPane().setBackground(Color.magenta);
getContentPane().add(namajurusan);
namajurusan.setBounds(10,10,100,20);
getContentPane().add(cbjur);
cbjur.setBounds(120,10,200,20);
aksireaksi(); // panggil void aksi reaksi
setVisible(true);
}
void aksireaksi ()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection Koneksi = DriverManager.getConnection("jdbc:mysql://localhost/politeknikaceh","root","");
Statement state = Koneksi.createStatement();
String query = "SELECT * FROM jurusan";
ResultSet rs = state.executeQuery(query);
while(rs.next())
{
Object obj[] = new Object[1];
obj[0] = rs.getString(1);
cbjur.addItem(obj[0]);
}
rs.close();
state.close();
}
catch(Exception ex)
{
System.out.println(ex);
} }
public static void main (String[] args)
{
ComboBoxDatabase cbd = new ComboBoxDatabase();
cbd.komponenVisual();
}
}