Input Data JList ke Database MySQL JAVA

Hallo, kali ini saya akan membahas mengenai cara input data JList ke database MySQL JAVA. Perhatikan gambar dibawah ini, terdapat dua JList yang sudah diisi. List yang dikiri adalah id, sedangkan list yang dikanan adalah judul buku.

Perhatikan source code dibawah ini. Saya akan menginput ke tabel buku data yang ada pada JList.

dataBase d = new dataBase();
Connection con = null;
PreparedStatement psmt = null;
ResultSet rs = null;
DefaultListModel list1 = new DefaultListModel();
DefaultListModel list2 = new DefaultListModel();

try {
con = d.getCon();

for (int i = 0; i < jList1.getModel().getSize(); i++) {

String item1 = jList1.getModel().getElementAt(i).toString();
String item2 = jList2.getModel().getElementAt(i).toString();
String SQL3 = "INSERT INTO buku VALUES(?,?)";
psmt = con.prepareStatement(SQL3);
psmt.setString(1, item1);
psmt.setString(2, item2);
psmt.executeUpdate();

}

} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}



Related Posts


Share on Google Plus

About Mr.J

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

3 comments:

  1. thanks a lots , i was looking for long times for this code.. i am thankful for you man

    BalasHapus
    Balasan
    1. You are welcome bro.

      Check also my youtube video.:)
      https://www.youtube.com/watch?v=BoN0SmQnQps

      Hapus