Java-2(2015) 7. Hafta Swing Uygulamaları
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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.sql.*; public class ornek1 implements ActionListener{ DefaultListModel lad,lmiktar,lfiyat,lid; JTextField jtfad,jtfmiktar,jtffiyat; JList jl1; ornek1(){ JFrame jfrm = new JFrame("Alışveriş Sepeti"); jfrm.setSize(400,320); jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jfrm.setLayout(new GridLayout(1,3)); lad= new DefaultListModel(); lmiktar= new DefaultListModel(); lfiyat= new DefaultListModel(); JPanel jpn1= new JPanel(); jl1 = new JList(lad); JScrollPane jsp1 = new JScrollPane(jl1); jsp1.setPreferredSize(new Dimension(100,200)); jpn1.add(jsp1); jtfad= new JTextField(); jtfad.setPreferredSize(new Dimension(100,20)); jpn1.add(jtfad); JButton jbtn1 = new JButton("Ekle"); jbtn1.setActionCommand("ekle"); jbtn1.addActionListener(this); jpn1.add(jbtn1); JPanel jpn2= new JPanel(); JList jl2 = new JList(lmiktar); JScrollPane jsp2 = new JScrollPane(jl2); jsp2.setPreferredSize(new Dimension(100,200)); jpn2.add(jsp2); jtfmiktar= new JTextField(); jtfmiktar.setPreferredSize(new Dimension(100,20)); jpn2.add(jtfmiktar); JButton jbtn2 = new JButton("Sil"); jbtn2.setActionCommand("sil"); jbtn2.addActionListener(this); jpn2.add(jbtn2); JPanel jpn3= new JPanel(); JList jl3 = new JList(lfiyat); JScrollPane jsp3 = new JScrollPane(jl3); jsp3.setPreferredSize(new Dimension(100,200)); jpn3.add(jsp3); jtffiyat= new JTextField(); jtffiyat.setPreferredSize(new Dimension(100,20)); jpn3.add(jtffiyat); JButton jbtn3 = new JButton("Güncelle"); jbtn3.setActionCommand("guncelle"); jbtn3.addActionListener(this); jpn3.add(jbtn3); jfrm.add(jpn1); jfrm.add(jpn2); jfrm.add(jpn3); verigetir(); jfrm.setVisible(true); } public Connection baglan() { Connection conn=null; try{ Class.forName("org.sqlite.JDBC"); conn=DriverManager.getConnection("jdbc:sqlite:hafta7.db"); } catch(Exception ex) { JOptionPane.showMessageDialog(null,"Bağlantı yapılamadı!","Hata",0); } return conn; } public void verigetir() { Connection conn=null; try{ conn = baglan(); lid = new DefaultListModel(); String sql = "select id,ad,miktar,fiyat from sepet;"; Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(sql); lad.clear(); lmiktar.clear(); lfiyat.clear(); while(rs.next()) { lid.addElement(rs.getInt("id")); lad.addElement(rs.getString("ad")); lmiktar.addElement(rs.getString("miktar")); lfiyat.addElement(rs.getString("fiyat")); } } catch(Exception ex) {} } public int alankontrol() { if(jtfad.getText().length()>0 && jtfmiktar.getText().length()>0 && jtffiyat.getText().length()>0){ int deger=-1; try{ deger=Integer.parseInt(jtffiyat.getText()); }catch(Exception ex){ JOptionPane.showMessageDialog(null,"Fiyat numerik olmalı!","Hata",0); } return deger; } return -1; } public void actionPerformed(ActionEvent ae) { Connection conn = baglan(); if(ae.getActionCommand().equals("ekle")) { try{ int fiyat=alankontrol(); if(fiyat>-1) { String sql="insert into sepet(ad,miktar,fiyat) values(?,?,?);"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1,jtfad.getText()); ps.setString(2,jtfmiktar.getText()); ps.setInt(3,fiyat); ps.executeUpdate(); jtfad.setText(""); jtfmiktar.setText(""); jtffiyat.setText(""); } } catch(Exception ex){} } else if(ae.getActionCommand().equals("sil")) { try{ int selected=jl1.getSelectedIndex(); if(selected>-1) { int index=(int)lid.get(selected); String sql="delete from sepet where id=?;"; PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1,index); ps.executeUpdate(); } } catch(Exception ex){} } else if(ae.getActionCommand().equals("guncelle")) { try{ int selected=jl1.getSelectedIndex(); int fiyat=alankontrol(); if(selected>-1 && fiyat>-1) { int index=(int)lid.get(selected); String sql="update sepet set ad=?,miktar=?, fiyat=? where id=?;"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1,jtfad.getText()); ps.setString(2,jtfmiktar.getText()); ps.setInt(3,fiyat); ps.setInt(4,index); ps.executeUpdate(); jtfad.setText(""); jtfmiktar.setText(""); jtffiyat.setText(""); } } catch(Exception ex){} } try { conn.close(); } catch(Exception ex){} verigetir(); } public static void main(String[] asdasd) { new ornek1(); } } |
Java2 2015 Hafta-7 Eklentiler
Okumaya devam et
Son Yorumlar