Nesne Tabanlı Programlama-2 (Bahar-2016) Hafta-7/1

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class veritabani extends JFrame implements ActionListener {
	
	DefaultListModel<String>[] dlm;
	JList<String>[] jlist;
	
	JTextField jtf1,jtf2;
	
	public veritabani()
	{
		this.setTitle("Telefon Rehberi");
		this.setSize(200,400);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(new FlowLayout());
		
		JPanel jpn_main= new JPanel();
		jpn_main.setLayout(new GridLayout(1,2));
		jpn_main.setPreferredSize(new Dimension(190, 390));
	
		JLabel jlab1= new JLabel("İsim");
		JLabel jlab2= new JLabel("Telefon No");
		JPanel jpn1= new JPanel();		
		JPanel jpn2= new JPanel();
		
		jpn1.add(jlab1);
		jpn2.add(jlab2);
		
		dlm= new DefaultListModel[3];
		jlist = new JList[2];
		
		dlm[2] = new DefaultListModel<String>(); // id listesi
		
		for(int i=0;i<2;i++)
		{
			dlm[i] = new DefaultListModel<String>();
			jlist[i]= new JList<String>(dlm[i]);
			
			JScrollPane jscp = new JScrollPane(jlist[i]);
			jscp.setPreferredSize(new Dimension(90, 290));
			
			if(i%2==0)
				jpn1.add(jscp);
			else
				jpn2.add(jscp);
		}
		
		jtf1 = new JTextField();
		jtf2 = new JTextField();
		JButton jbtn1 = new JButton("Ekle");
		JButton jbtn2 = new JButton("Sil");		
		jtf1.setPreferredSize(new Dimension(90, 30));
		jtf2.setPreferredSize(new Dimension(90, 30));	
		jbtn1.setPreferredSize(new Dimension(90, 30));
		jbtn2.setPreferredSize(new Dimension(90, 30));
			
		jpn1.add(jtf1);
		jpn1.add(jbtn1);
		jpn2.add(jtf2);
		jpn2.add(jbtn2);
		
		jpn_main.add(jpn1);
		jpn_main.add(jpn2);
		
		jbtn1.setActionCommand("ekle");
		jbtn1.addActionListener(this);
		jbtn2.addActionListener(this);
		this.add(jpn_main);
	
		Connection conn = baglan();
		verigetir(conn);
		
		this.setVisible(true);
		
		
	}

	public void verigetir(Connection conn)
	{
		if(conn==null)
			return;
		
		try{
			dlm[0].clear();
			dlm[1].clear();
			dlm[2].clear(); // id ler silindi
			
			String sql = "select id,isim,telno from kullanicilar order by isim";
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			
			while(rs.next())
			{
				dlm[0].addElement(rs.getString("isim"));
				dlm[1].addElement(rs.getString("telno"));
				dlm[2].addElement(rs.getString("id")); // id ler listeye yukleniyor
			}
			
						
		}
		catch(Exception ex)
		{
			JOptionPane.showMessageDialog(this, "Sorgu Hatasi!");
		}
		
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		
		Connection conn = baglan();
		if(conn==null)
			return;
		try{		
			if(arg0.getActionCommand().equals("ekle"))
			{
				String sql="insert into kullanicilar (isim,telno) values(?,?)";
					
				if(jtf1.getText().length()>0 && jtf2.getText().length()>0)
				{
					PreparedStatement ps = conn.prepareStatement(sql);
					ps.setString(1, jtf1.getText());
					ps.setString(2, jtf2.getText());
					ps.executeUpdate();
					jtf1.setText("");
					jtf2.setText("");
				}
			}
			else
			{
				int secilen = jlist[0].getSelectedIndex();
				
				if(secilen>-1)
				{
					String id = dlm[2].get(secilen);
					String sql = "delete from kullanicilar where id=?;";
	
					PreparedStatement ps = conn.prepareStatement(sql);
					ps.setString(1, id);
					int say = ps.executeUpdate();
					//JOptionPane.showMessageDialog(this, "Silinen kayit:"+say);
				}
				
			}
		}catch(Exception ex)
		{
			JOptionPane.showMessageDialog(this, "Hata: "+ex.getMessage());
		}
		
		verigetir(conn);
		
	}
	
	public Connection baglan()
	{
		Connection conn = null;
		try{
			
			Class.forName("org.sqlite.JDBC");
			conn=DriverManager.getConnection("jdbc:sqlite:orgun1h7_1.db");
			
		}catch(Exception ex)
		{
			JOptionPane.showMessageDialog(this, "Baglanti Hatasi!");
		}
		return conn;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new veritabani();

	}

}

NTP2 (Bahar-2016) Hafta-7/1
sqlite-jdbc

Share