Nesne Tabanlı Programlama-2 (Bahar-2016) Hafta-10
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import java.sql.*; import java.util.Vector; public class uygulama1 extends JFrame implements ActionListener,ListSelectionListener { JList[] jls; DefaultListModel<String>[] dlm; JTextField jtf; JComboBox jcb; JLabel jlab_sonuc; Connection conn = null; Vector<String> vurunler,vurunlerid; public uygulama1() { this.setTitle("Uygulama-1"); this.setSize(300,360); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLayout(new FlowLayout()); String[] label={"Adı","Miktar","Tutar"}; String[] butlabel={"Ekle","Sil","Düzenle"}; String[] butlabelac={"ekle","sil","duzenle"}; JPanel jpn_main = new JPanel(); jpn_main.setLayout(new GridLayout(1,3)); jpn_main.setPreferredSize(new Dimension(290,350)); jls = new JList[3]; dlm = new DefaultListModel[4]; dlm[3] = new DefaultListModel<String>(); // sepet id vurunler = new Vector<String>(); vurunlerid = new Vector<String>(); for(int i =0;i<label.length;i++) { JLabel jlab= new JLabel(label[i]); JPanel jpn = new JPanel(); jpn.add(jlab); dlm[i] = new DefaultListModel<String>(); jls[i] = new JList<String>(dlm[i]); jls[i].addListSelectionListener(this); JScrollPane jscp = new JScrollPane(jls[i]); jscp.setPreferredSize(new Dimension(90,250)); jpn.add(jscp); if(i==0){ jcb = new JComboBox<String>(vurunler); jcb.setPreferredSize(new Dimension(90,30)); jpn.add(jcb); } if(i==1){ jtf= new JTextField(); jtf.setPreferredSize(new Dimension(90,30)); jpn.add(jtf); } if(i==2){ jlab_sonuc = new JLabel("Tutar: 0"); jlab_sonuc.setPreferredSize(new Dimension(90,30)); jpn.add(jlab_sonuc); } JButton jbtn = new JButton(butlabel[i]); jbtn.setPreferredSize(new Dimension(90,30)); jbtn.setActionCommand(butlabelac[i]); jbtn.addActionListener(this); jpn.add(jbtn); jpn_main.add(jpn); } this.add(jpn_main); baglan(); if(conn==null) { JOptionPane.showMessageDialog(this, "Veritabanı Hatası!"); System.exit(0); } urungetir(); // combo verigetir(); // liste this.setVisible(true); } @Override public void actionPerformed(ActionEvent arg0) { switch (arg0.getActionCommand()) { case "ekle": ekle(); break; case "sil": sil(); break; case "duzenle": duzenle(); break; } verigetir(); } private void baglan() { conn=null; try{ Class.forName("org.sqlite.JDBC"); conn=DriverManager.getConnection("jdbc:sqlite:orgun2hafta10.db"); } catch(Exception ex) { } } private void urungetir() // urunler tablosu { vurunler.clear(); vurunlerid.clear(); String sql="select id,ad from urunler order by ad;"; try{ Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(sql); while(rs.next()) { vurunler.add(rs.getString("ad")); vurunlerid.add(rs.getString("id")); } if(vurunler.size()>0) jcb.setSelectedIndex(0); } catch(Exception ex) { } } private void verigetir() { for(int i =0;i<dlm.length;i++) dlm[i].clear(); int tutar=0; try{ String sql="select s.id,u.ad,s.miktar,(u.birimfiyat*s.miktar) as tutar from urunler u inner join sepet s on s.uid=u.id"; Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(sql); while(rs.next()) { dlm[0].addElement(rs.getString("ad")); dlm[1].addElement(rs.getString("miktar")); dlm[2].addElement(rs.getString("tutar")); dlm[3].addElement(rs.getString("id")); tutar+=rs.getInt("tutar"); } } catch(Exception ex) { } jlab_sonuc.setText("Tutar: "+tutar); } private void duzenle() { try{ if(jls[0].getSelectedIndex()>-1) { String sql="update sepet set uid=?, miktar=? where id=?"; String id = dlm[3].get(jls[0].getSelectedIndex()); String uid = vurunlerid.get(jcb.getSelectedIndex()); int miktar = Integer.parseInt(jtf.getText()); PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, uid); ps.setString(2, miktar+""); ps.setString(3, id); ps.executeUpdate(); jtf.setText(""); jcb.setSelectedIndex(0); } }catch(Exception ex) { } } private void sil() { try{ if(jls[0].getSelectedIndex()>-1) { String id = dlm[3].get(jls[0].getSelectedIndex()); String sql="delete from sepet where id=?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, id); ps.executeUpdate(); } }catch(Exception ex) { } } private void ekle() { try{ String uid =vurunlerid.get(jcb.getSelectedIndex()); // urunler id -> uid int miktar = Integer.parseInt(jtf.getText()); String sql ="insert into sepet(uid,miktar) values (?,?)"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1,uid); ps.setString(2,miktar+""); ps.executeUpdate(); jcb.setSelectedIndex(0); jtf.setText(""); }catch(Exception ex) { } } @Override public void valueChanged(ListSelectionEvent arg0) { JList jl = (JList) arg0.getSource(); for(int i=0;i<jls.length;i++) jls[i].setSelectedIndex(jl.getSelectedIndex()); } /** * @param args */ public static void main(String[] args) { new uygulama1(); } }