Java-2 2016 Hafta-11
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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
import java.awt.*; import java.sql.*; import java.awt.event.*; import javax.swing.*; public class market implements ActionListener { JList[] jlist; DefaultListModel[] dlm; JComboBox jcb; JTextField jtf; JLabel jlab; public market() { JFrame jfrm = new JFrame("Market"); jfrm.setSize(250,275); jfrm.setLayout(new FlowLayout()); jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel jpn_main = new JPanel(); jpn_main.setLayout(new GridLayout(1,3)); jpn_main.setPreferredSize(new Dimension(250,275)); JPanel[] jpn = new JPanel[3]; jlist = new JList[3]; dlm= new DefaultListModel[5]; // 3 tanesi jlist , +2 tanesi id ler icin (urunlerin id bilgisi, sepetin id bilgisi) for(int i =0;i<jlist.length;i++) { jpn[i]= new JPanel(); dlm[i]= new DefaultListModel(); jlist[i]=new JList(dlm[i]); JScrollPane jscp = new JScrollPane(jlist[i]); jscp.setPreferredSize(new Dimension(80,200)); jpn[i].add(jscp); jpn_main.add(jpn[i]); } jcb = new JComboBox(); jcb.setPreferredSize(new Dimension(80,25)); jtf= new JTextField(); jtf.setPreferredSize(new Dimension(80,25)); jlab = new JLabel("Tutar: 0"); jlab.setPreferredSize(new Dimension(80,25)); jpn[0].add(jcb); jpn[1].add(jtf); jpn[2].add(jlab); JButton jbtn_ekle = new JButton("Ekle"); jbtn_ekle.addActionListener(this); jbtn_ekle.setActionCommand("ekle"); jbtn_ekle.setPreferredSize(new Dimension(80,25)); JButton jbtn_sil = new JButton("Sil"); jbtn_sil.addActionListener(this); jbtn_sil.setPreferredSize(new Dimension(80,25)); jpn[0].add(jbtn_ekle); jpn[1].add(jbtn_sil); jfrm.add(jpn_main); combodoldur(); listedoldur(); jfrm.setVisible(true); } @Override public void actionPerformed(ActionEvent ae) { if(ae.getActionCommand().equals("ekle")) { // ekle Connection conn =baglan(); try { String sql = "insert into sepet(urunid,miktar) values(?,?);"; int secilen = jcb.getSelectedIndex(); int miktar = Integer.parseInt(jtf.getText()); if(secilen>0) { int urunid = (int) dlm[3].get(secilen-1); // urunler icin id listesi; PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1,urunid); ps.setInt(2,miktar); ps.executeUpdate(); jcb.setSelectedIndex(0); jtf.setText(""); listedoldur(); } } catch(Exception ex) { System.out.println(ex); } } else { Connection conn =baglan(); try { String sql = "delete from sepet where id=?;"; int secilen = jlist[0].getSelectedIndex(); if(secilen>-1) { int secilen_id = (int) dlm[4].get(secilen); // urunler icin id listesi; PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1,secilen_id); ps.executeUpdate(); listedoldur(); } } catch(Exception ex) { System.out.println(ex); } } } public void combodoldur() { try{ Connection conn = baglan(); if(conn!=null) { String sql = "select id,ad,fiyat from urunler"; Statement st = conn.createStatement(); ResultSet rs =st.executeQuery(sql); dlm[3] = new DefaultListModel(); // urunler tablosunun id bilgisini tutacak olan degisken jcb.addItem("Seçiniz"); while(rs.next()) { dlm[3].addElement(rs.getInt("id")); jcb.addItem(rs.getString("ad")); } } } catch(Exception ex) { System.out.println(ex); } } public void listedoldur() { try{ Connection conn = baglan(); if(conn!=null) { String sql = "select u.ad,u.fiyat*s.miktar as tutar,s.id," + "s.miktar from urunler u inner join sepet s on " + "s.urunid=u.id"; Statement st = conn.createStatement(); ResultSet rs =st.executeQuery(sql); dlm[0].clear(); dlm[1].clear(); dlm[2].clear(); dlm[4] = new DefaultListModel(); // sepet id bilgisi tutan degisken silme isleminde kullanilacak int tutar=0; while(rs.next()) { dlm[0].addElement(rs.getString("ad")); dlm[1].addElement(rs.getInt("miktar")); dlm[2].addElement(rs.getInt("tutar")); dlm[4].addElement(rs.getInt("id")); // s.id bilgisi tutar+=(int)rs.getInt("tutar"); } jlab.setText("Tutar: "+tutar); } } catch(Exception ex) { System.out.println(ex); } } public Connection baglan() { Connection conn=null; try{ Class.forName("org.sqlite.JDBC"); conn=DriverManager.getConnection("jdbc:sqlite:market.db"); } catch(Exception ex) { JOptionPane.showMessageDialog(null,"Bağlantı yapılamadı!","Hata",0); } return conn; } /** * @param args */ public static void main(String[] args) { new market(); } } |
Java-2 2016 Hafta-11 Uygulamaları
Okumaya devam et