Java-2 – YO2015 – Hafta – 5
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(); } }
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(); } }
Hocam Java-2 dersinde son haftada yaptığımız uygulamayı yüklememişsiniz..
Yükleyebilir misiniz acaba?
Yüklüyorum