Bir önceki yazıda Combobox’a seçenek eklemeyi göstermiştim. Şimdi ise bu seçenekleri direk sql’den alması için gereken kodları göstereceğim.
//Burada baglantiAc() fonksiyonu ile statement oluşturmayı java ile sql’e bağlanma yazısında göstermiştim.
//personelSonucKumesi = st.executeQuery(“SELECT Adi FROM personeller”); ile sql’e personeller tablosundan sadece isim sütununu çekmesini söylüyoruz. Daha sonra personelComboYaz() fonksiyonunu çağırarak çektiğimiz verileri combobox’a yazdırıyoruz.Bu fonksiyon :
while(personelSonucKumesi.next()){
personelCombo.addItem(personelSonucKumesi.getString(“Adi”));
}
While döngüsüyle personelSonucKumesi’nde tüm verileri alana kadar devam ettiriyoruz.
personelSonucKumesi.getString(“Adi”) ile bu verilerden Adi sütunundaki satırları tek tek çekiyoruz.
public Statement baglantiAc() throws Exception{
Class.forName(driver).newInstance();
baglayici = DriverManager.getConnection(url, userName, password);
return baglayici.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
}
public NewJFrame(){
this.ataPencere = ata;
initComponents();
Statement st = null;
try {
st = baglantiAc(); //veritabanına bağlanılıyor
} catch (Exception ex) {
JOptionPane.showConfirmDialog(null, “Bağlantı Başarısız”, “MySQL Bağlantısı”, JOptionPane.PLAIN_MESSAGE);
}
try{
personelSonucKumesi = st.executeQuery(“SELECT Adi FROM personeller”);
personelComboYaz();
}catch(Exception ex){
}
}
public void personelComboYaz(){
try {
while(personelSonucKumesi.next()){
personelCombo.addItem(personelSonucKumesi.getString(“Adi”));
}
} catch (SQLException ex) {
Logger.getLogger(AIsEkle.class.getName()).log(Level.SEVERE, null, ex);
}
}