Java-2 2016 Hafta-10
import java.awt.*; import java.awt.event.*; import java.sql.*; import javax.swing.*; public class ornek1 implements ActionListener { JList jl1,jl2; DefaultListModel dlm1,dlm2,dlm_id; JTextField jtf_isim,jtf_telno; public ornek1() { JFrame jfrm = new JFrame("Personel Bilgileri"); jfrm.setLayout(new FlowLayout()); jfrm.setSize(150,290); jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel jpn_main = new JPanel(); jpn_main.setPreferredSize(new Dimension(150,290)); jpn_main.setLayout(new GridLayout(1,2)); jfrm.add(jpn_main); dlm_id= new DefaultListModel(); JPanel jpn1 = new JPanel(); dlm1= new DefaultListModel(); jl1= new JList(dlm1); JScrollPane jscp1= new JScrollPane(jl1); jscp1.setPreferredSize(new Dimension(70,200)); jpn1.add(jscp1); jtf_isim = new JTextField(); jtf_isim.setPreferredSize(new Dimension(70,30)); JButton jbtn1= new JButton("Ekle"); jbtn1.addActionListener(this); jbtn1.setActionCommand("ekle"); jbtn1.setPreferredSize(new Dimension(70,30)); jpn1.add(jtf_isim); jpn1.add(jbtn1); JPanel jpn2 = new JPanel(); dlm2= new DefaultListModel(); jl2= new JList(dlm2); JScrollPane jscp2= new JScrollPane(jl2); jscp2.setPreferredSize(new Dimension(70,200)); jpn2.add(jscp2); jtf_telno = new JTextField(); jtf_telno.setPreferredSize(new Dimension(70,30)); JButton jbtn2= new JButton("Sil"); jbtn2.addActionListener(this); jbtn2.setPreferredSize(new Dimension(70,30)); jpn2.add(jtf_telno); jpn2.add(jbtn2); jpn_main.add(jpn1); jpn_main.add(jpn2); listedoldur(); jfrm.setVisible(true); } public void listedoldur() { try{ Connection conn = baglan(); if(conn!=null) { dlm_id.clear(); dlm1.clear(); dlm2.clear(); String sql="select id, isim, telno from personel;"; Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(sql); while(rs.next()) // kayit varsa { dlm_id.addElement(rs.getInt("id")); dlm1.addElement(rs.getString("isim")); dlm2.addElement(rs.getString("telno")); } } }catch(Exception ex) { System.out.println(ex); } } @Override public void actionPerformed(ActionEvent ae) { Connection conn = baglan(); if(ae.getActionCommand().equals("ekle")) { try{ if(jtf_isim.getText().length()>0 && jtf_telno.getText().length()>0) { String sql = "insert into personel(isim,telno) values(?,?)"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, jtf_isim.getText()); ps.setString(2, jtf_telno.getText()); ps.executeUpdate(); jtf_isim.setText(""); jtf_telno.setText(""); listedoldur(); } } catch(Exception ex) { System.out.println(ex); } } else { try{ int secilen = jl1.getSelectedIndex(); if(secilen>-1) { String sql = "delete from personel where id=?;"; PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, (int)dlm_id.get(secilen)); ps.executeUpdate(); listedoldur(); } } catch(Exception ex) { System.out.println(ex); } } } public Connection baglan() { Connection conn=null; try{ Class.forName("org.sqlite.JDBC"); conn=DriverManager.getConnection("jdbc:sqlite:ornek1.db"); } catch(Exception ex) { JOptionPane.showMessageDialog(null,"Bağlantı yapılamadı!","Hata",0); } return conn; } /** * @param args */ public static void main(String[] args) { new ornek1(); } }