Nesne Tabanlı Programlama-2 (Bahar-2016) Hafta-14

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


public class rehber extends JFrame implements ActionListener, KeyListener, ListSelectionListener {

	JTextField jtf_arama, jtf[];
	JList<String>[] jl;
	Vector<String>[] vc;
	JRadioButton[] jrb;
	Connection conn=null;
	
	public rehber()
	{
		this.setTitle("Rehber Uygulaması");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(300,500);
		this.setLayout(new FlowLayout());
		
		JPanel jpn_main= new JPanel();
		jpn_main.setPreferredSize(new Dimension(300,500));
		
		JPanel jpn1 = new JPanel();
		jpn1.setLayout(new FlowLayout());
		jpn1.setPreferredSize(new Dimension(290,70));
		
		JLabel jlab = new JLabel("Arama Kriteri");
		jlab.setPreferredSize(new Dimension(290,30));
		jlab.setHorizontalAlignment(JLabel.CENTER);
		jpn1.add(jlab);
		
		jtf_arama = new JTextField();
		jtf_arama.setPreferredSize(new Dimension(290,30));
		jtf_arama.addKeyListener(this);
		jpn1.add(jtf_arama);
		
		JPanel jpn2 = new JPanel();
		jpn2.setLayout(new GridLayout(1,3));
		jpn2.setPreferredSize(new Dimension(290,410));
		
		
		
		String[] labels ={"İsim","Tel. No","E-Posta"};
		jl = new JList[labels.length];
		vc = new Vector[jl.length+1];
		jtf = new JTextField[jl.length];
		vc[vc.length-1] = new Vector<String>();
		
		for(int i =0;i<jl.length;i++)
		{
			JPanel jpn = new JPanel();
			
			jlab = new JLabel(labels[i]);
			jpn.add(jlab);
			
			vc[i]= new Vector<String>();
			jl[i] = new JList<String>(vc[i]);
			JScrollPane jscp = new JScrollPane(jl[i]);
			jscp.setPreferredSize(new Dimension(90,260));
			
			jpn.add(jscp);
			
			jtf[i] = new JTextField();
			jtf[i].setPreferredSize(new Dimension(90,30));
			jpn.add(jtf[i]);
			
			if(i==0)
			{
				String[] rb_lab={"Ekle","Sil","Güncelle"};
				jrb = new JRadioButton[rb_lab.length];
				
				ButtonGroup bg = new ButtonGroup();
				for(int j=0;j<jrb.length;j++)
				{
					jrb[j]= new JRadioButton(rb_lab[j]);
					jrb[j].setPreferredSize(new Dimension(90,20));
					bg.add(jrb[j]);
					jpn.add(jrb[j]);
				}
				
				jrb[0].setSelected(true);
				
			}
			
			if(i==1)
			{
				JButton jbtn = new JButton("İşlem");
				jbtn.addActionListener(this);
				jbtn.setPreferredSize(new Dimension(90,30));
				jpn.add(jbtn);
				
			}
			
			
			
			jpn2.add(jpn);
		}
		
		//jl[i].setListData(vc[i]);
		baglan();
		if(conn==null)
		{
			System.exit(0);
		}
		
		listedoldur("");
		
		jpn_main.add(jpn1);
		jpn_main.add(jpn2);
		
		this.add(jpn_main);
		this.setVisible(true);
	}
	
	
	private void baglan()
	{
		try{
			
			Class.forName("org.sqlite.JDBC");
			conn=DriverManager.getConnection("jdbc:sqlite:hafta14.db");
			
		}catch(Exception ex)
		{
			
		}
	}
	
	private void listedoldur(String deger)
	{
		try{
				for(int i=0;i<vc.length;i++)
					vc[i].clear();
				
			String arama="%"+deger+"%";
			String sql="select isim,telno,eposta,id from rehber where isim like ? or telno like ? or eposta like ?;";
			
			PreparedStatement ps =conn.prepareStatement(sql);
			ps.setString(1, arama);
			ps.setString(2, arama);
			ps.setString(3, arama);
			
			ResultSet rs = ps.executeQuery();
			
			while(rs.next())
			{
				for(int i =0;i<vc.length;i++)
					vc[i].add(rs.getString(i+1));
				
			}
			
			for(int i=0;i<jl.length;i++)
				jl[i].updateUI();
				//jl[i].setListData(vc[i]);
			
			
		}catch(Exception ex)
		{
			
		}
	}
	
	private void ekle()
	{
		try{
			if(kontrol())
			{
				
				String sql="insert into rehber(isim,telno,eposta) values(?,?,?);";
				
				PreparedStatement ps = conn.prepareStatement(sql);
				ps.setString(1, jtf[0].getText());
				ps.setString(2, jtf[1].getText());
				ps.setString(3, jtf[2].getText());
				ps.executeUpdate();
	
				for(int i =0;i<jtf.length;i++)
					jtf[i].setText("");

			}
			
			
		}
		catch(Exception ex)
		{
			
		}
		
	}
	
	private void sil()
	{
		try{
			
			int secilen = jl[0].getSelectedIndex();
			
			if(secilen>-1)
			{
				String uid=vc[vc.length-1].get(secilen);
				
				String sql="delete from rehber where id=?;";
				
				PreparedStatement ps = conn.prepareStatement(sql);
				ps.setString(1, uid);
				ps.executeUpdate();
			}
			
			
		}catch(Exception ex)
		{
			
		}
		
		
		
	}
	
	private void guncelle()
	{
		try{
			
			int secilen = jl[0].getSelectedIndex();
			if(secilen>-1 && kontrol())
			{
				String uid =vc[vc.length-1].get(secilen);
				
				String sql="update rehber set isim=?, telno=?, eposta=? where id=?;";
				PreparedStatement ps = conn.prepareStatement(sql);
				ps.setString(1, jtf[0].getText());
				ps.setString(2, jtf[1].getText());
				ps.setString(3, jtf[2].getText());
				ps.setString(4, uid);
				ps.executeUpdate();
				
				for(int i =0;i<jtf.length;i++)
					jtf[i].setText("");
			}
			
			
			
		}
		catch(Exception ex)
		{
			
		}
		
		
		
		
	}
	
	private boolean kontrol()
	{
		int[] sinirlar={0,5,4};
		for(int i =0;i<jtf.length;i++)
			if(!(jtf[i].getText().length()>sinirlar[i]))
				return false;
		
		try{
			
			Integer.parseInt(jtf[1].getText());
			
			String[] parca1= jtf[2].getText().split("@");
			
			
			
			if(parca1.length==2 && parca1[0].length()>0 && parca1[1].length()>0)
			{
				String[] parca2 = parca1[1].split("\\.");
				
				if(parca2.length>1)
				{
					for(int i=0;i<parca2.length;i++)
					{
						if(!(parca2[i].length()>0))
							return false;
						
					}
					
					return true;
					
				}
				
			}
			
			
			
		}catch(Exception ex)
		{
			
		}

		
		return false;
	}
	
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
	
		if(jrb[0].isSelected())
		{
			ekle();
		}
		if(jrb[1].isSelected())
		{
			sil();
		}
		if(jrb[2].isSelected())
		{
			guncelle();
		}
		
		listedoldur("");
		
	}
	
	@Override
	public void keyPressed(KeyEvent arg0) {
		
	}


	@Override
	public void keyReleased(KeyEvent arg0) {
		
		if(jtf_arama.getText().length()>2)
			listedoldur(jtf_arama.getText());
		else
			listedoldur("");
		
		
	}


	@Override
	public void keyTyped(KeyEvent arg0) {
		
		
	}


	@Override
	public void valueChanged(ListSelectionEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	
	

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

	}

}

NTP2 (Bahar-2016) Hafta-14

Share