Java-2 – YO2015 – Hafta – 5
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 |
import java.awt.event.*; import javax.swing.*; import java.awt.*; import java.sql.*; public class ornek1 implements ActionListener { JTextField jtf; DefaultListModel<String> lm; public ornek1() { JFrame jfrm = new JFrame("ornek-1 veritabani"); jfrm.setSize(180, 200); jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jfrm.setLayout(new FlowLayout()); lm = new DefaultListModel<String>(); JList<String> jlist = new JList<String>(lm); JScrollPane jscp = new JScrollPane(jlist); jscp.setPreferredSize(new Dimension(150, 120)); jfrm.add(jscp); jtf = new JTextField(); jtf.setPreferredSize(new Dimension(150, 20)); jfrm.add(jtf); JButton jbtn = new JButton("Ekle"); jbtn.addActionListener(this); jbtn.setPreferredSize(new Dimension(150, 30)); jfrm.add(jbtn); verigetir(); jfrm.setVisible(true); } 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; } public void verigetir() { Connection conn = baglan(); String sql = "select id,ad from liste;"; try { Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(sql); lm.clear(); while (rs.next()) { lm.addElement(rs.getString("ad")); } } catch (Exception ex) { } try { conn.close(); } catch (Exception ex) { } } @Override public void actionPerformed(ActionEvent arg0) { if (jtf.getText().length() > 0) { String sql = "insert into liste(ad) values(?)"; Connection conn = baglan(); try { PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, jtf.getText()); ps.executeUpdate(); } catch (Exception ex) { } try { conn.close(); } catch (Exception ex) { } jtf.setText(""); } verigetir(); } /** * @param args */ public static void main(String[] args) { new ornek1(); } } |
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 |
import java.awt.event.*; import java.awt.*; import javax.swing.*; import java.sql.*; public class ornek2 implements ActionListener { JList<String> jl1, jl2, jl3; DefaultListModel<String> lm1, lm2, lm3; DefaultListModel<Integer> lmid; JTextField jtf1, jtf2; public ornek2() { JFrame jfrm = new JFrame("ornek-2 veritabani"); jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jfrm.setLayout(new GridLayout(1, 3)); jfrm.setSize(200, 220); JPanel jpn = new JPanel(); lm1 = new DefaultListModel<String>(); jl1 = new JList<String>(lm1); jtf1 = new JTextField(); jtf1.setPreferredSize(new Dimension(55, 20)); JScrollPane jscp = new JScrollPane(jl1); jscp.setPreferredSize(new Dimension(55, 150)); JButton jbtn1 = new JButton("Ekle"); jbtn1.addActionListener(this); jbtn1.setActionCommand("ekle"); jpn.add(jscp); jpn.add(jtf1); jpn.add(jbtn1); jfrm.add(jpn); jpn = new JPanel(); lm2 = new DefaultListModel<String>(); jl2 = new JList<String>(lm2); jtf2 = new JTextField(); jtf2.setPreferredSize(new Dimension(55, 20)); jscp = new JScrollPane(jl2); jscp.setPreferredSize(new Dimension(55, 150)); JButton jbtn2 = new JButton("Sil"); jbtn2.addActionListener(this); jpn.add(jscp); jpn.add(jtf2); jpn.add(jbtn2); jfrm.add(jpn); jpn = new JPanel(); lm3 = new DefaultListModel<String>(); jl3 = new JList<String>(lm3); jscp = new JScrollPane(jl3); jscp.setPreferredSize(new Dimension(55, 150)); jpn.add(jscp); jfrm.add(jpn); verigetir(); jfrm.setVisible(true); } private void verigetir() { Connection conn = baglan(); try { String sql = "select idnot,no,ort from notlistesi;"; Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(sql); lm1.clear(); lm2.clear(); lm3.clear(); lmid = new DefaultListModel<Integer>(); while (rs.next()) { lmid.addElement(rs.getInt("idnot")); // int, silmek icin lm1.addElement(rs.getString("no")); lm2.addElement(rs.getString("ort")); int ort = rs.getInt("ort"); lm3.addElement(harfgetir(ort)); } } catch (Exception ex) { } try { conn.close(); } catch (Exception ex) { } } public String harfgetir(int ort) { String[] harfler = { "AA", "BA", "BB", "CB", "CC", "DC", "DD", "FD" }; int[] sinir = { 90, 80, 70, 60, 50, 40, 35, 30 }; for (int i = 0; i < harfler.length; i++) { if (ort >= sinir[i]) return harfler[i]; } return "FF"; } public Connection baglan() { Connection conn = null; try { Class.forName("org.sqlite.JDBC"); conn = DriverManager.getConnection("jdbc:sqlite:ornek2.db"); } catch (Exception ex) { JOptionPane.showMessageDialog(null, "Bağlantı yapılamadı!", "Hata", 0); } return conn; } @Override public void actionPerformed(ActionEvent ae) { Connection conn = baglan(); if (ae.getActionCommand().equals("ekle")) { try { if (jtf1.getText().length() > 0 && jtf2.getText().length() > 0) { int ort = Integer.parseInt(jtf2.getText()); String sql = "insert into notlistesi(no,ort) values(?,?);"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, jtf1.getText()); ps.setInt(2, ort); ps.executeUpdate(); jtf1.setText(""); jtf2.setText(""); } } catch (Exception ex) { } } else { if (jl1.getSelectedIndex() > -1) { try { int secilen = lmid.get(jl1.getSelectedIndex()); // idnot // karsiligi String sql = "delete from notlistesi where idnot=?;"; PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, secilen); ps.executeUpdate(); } catch (Exception ex) { } } } try { conn.close(); } catch (Exception ex) { } verigetir(); } public static void main(String[] args) { new ornek2(); } } |
JAVA-2 YO2015 5. Hafta Uygulamaları
Hocam Java-2 dersinde son haftada yaptığımız uygulamayı yüklememişsiniz..
Yükleyebilir misiniz acaba?
Yüklüyorum