Java-2 2016 Hafta-10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
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(); } } |