Sqlite browser burdan indirin
Sql jdbc burdan indirin
Sqlite Örneğini indir

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;//Veri tabanına Bağlantı yapmak için kullandığımız kütüphane 

public class Calistir  implements ActionListener,ListSelectionListener {
	
	JTextField[] jtext_gun;
	 JLabel[] jlb;
	 String[] lbltitle= {"Ad:","SoyAd:","Yas:"};
	 JButton jbtn_gun;
	 JPanel jpn_gun,jpnlb_gun;
	 int yas_gun=-1; 
     JFrame frm,frmgun;
	 JList jlist[],jlistyas;
	 DefaultListModel<String> dlm[];
	 DefaultListModel<Integer> dlmyas,dlmidler;
	 JTextField[] jtext;
	 String[] jbtntitle= {"Ekle","Sil","Güncelle"};
	 JButton[] jbtn;
	 JPanel jpn[],jpnyas;
	 JScrollPane scjpn[],scjpnyas;
	 public Calistir() {
       frm=new JFrame("Sqlite Bağlanti");
       frm.setSize(400, 500);
       frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frm.setLayout(new FlowLayout());
       
       jpn=new JPanel[2];
       dlm=new DefaultListModel[jpn.length];
       dlmidler=new DefaultListModel<Integer>();
       jlist=new JList[dlm.length];
       scjpn=new JScrollPane[jlist.length];
       jbtn=new JButton[3];
       jtext=new JTextField[3];
       
       for(int i=0;i<jpn.length;i++)
       {
    	   jpn[i]=new JPanel();
    	   jpn[i].setPreferredSize(new Dimension(100,400));
    	   jpn[i].setLayout(new FlowLayout());
    	   dlm[i]=new DefaultListModel<String>();
    	   jlist[i]=new JList<String>(dlm[i]);
    	   jlist[i].addListSelectionListener(this);
    	   scjpn[i]=new JScrollPane(jlist[i]);
    	   scjpn[i].setPreferredSize(new Dimension(100,300));
    	   jpn[i].add(scjpn[i]);
    	   frm.add(jpn[i]);   
       }
    	  jpnyas=new JPanel();
    	  jpnyas.setPreferredSize(new Dimension(100,400));
    	  jpnyas.setLayout(new FlowLayout());
   	      dlmyas=new DefaultListModel<Integer>();
   	      jlistyas=new JList<Integer>(dlmyas);
   	      jlistyas.addListSelectionListener(this);
   	      scjpnyas=new JScrollPane(jlistyas);
   	      scjpnyas.setPreferredSize(new Dimension(100,300));
   	      jpnyas.add(scjpnyas);
   	     
   	      for(int i=0;i<jpn.length;i++)//JButtonlar ve JTextField lar oluşturulup panele atılıyor 
   	      {   jtext[i]=new JTextField();
   	          jtext[i].setPreferredSize(new Dimension(100,20));
   	    	  jbtn[i]=new JButton(jbtntitle[i]);
   	    	  jbtn[i].setPreferredSize(new Dimension(100,20));
   	    	  jbtn[i].addActionListener(this);
   	    	  jbtn[i].setActionCommand(jbtntitle[i]);
   	    	  jpn[i].add(jtext[i]);
   	    	  jpn[i].add(jbtn[i]);
   	    	  if(i==jpn.length-1)
   	    	  { 
   	          jtext[2]=new JTextField();
   	          jtext[2].setPreferredSize(new Dimension(100,20));
   	    	  jbtn[2]=new JButton(jbtntitle[2]);
   	    	  jbtn[2].setPreferredSize(new Dimension(100,20));
   	    	  jbtn[2].addActionListener(this);
   	    	  jbtn[2].setActionCommand(jbtntitle[2]);
   	    	  jpnyas.add(jtext[2]);
   	    	  jpnyas.add(jbtn[2]);
   	    	  }
   	      }
   	   frm.add(jpnyas); 
   	   verigetir();
       frm.setVisible(true);
	}
	 public void  KGuncelle(int secilen)
		{   frm.setVisible(false);
			frmgun=new JFrame("Kişi Güncelle");
			frmgun.setSize(300, 300);
			frmgun.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
			frmgun.setLayout(new FlowLayout());
	
			jpn_gun=new JPanel();
			jpn_gun.setPreferredSize(new Dimension(100,150));
			jpn_gun.setLayout(new FlowLayout());
			
			jpnlb_gun=new JPanel();
			jpnlb_gun.setPreferredSize(new Dimension(40,150));
			jpnlb_gun.setLayout(new FlowLayout());
			
			
			jlb=new JLabel[3];
			jtext_gun=new JTextField[jlb.length];
			
			for(int i=0;i<lbltitle.length;i++)
			{
				jlb[i]=new JLabel(lbltitle[i]);
				jlb[i].setPreferredSize(new Dimension(40,20)); 
				jlb[i].setHorizontalAlignment(JLabel.RIGHT);
			    jtext_gun[i]=new JTextField();
			    jtext_gun[i].setPreferredSize(new Dimension(100,20));
			    jpnlb_gun.add(jlb[i]);
			    jpn_gun.add(jtext_gun[i]);	
			}
			for(int i=0;i<dlm.length;i++)
			     jtext_gun[i].setText(dlm[i].getElementAt(secilen));
			     jtext_gun[2].setText(dlmyas.getElementAt(secilen).toString());
			     
			
			jbtn_gun=new JButton("Kaydet");
			jbtn_gun.setPreferredSize(new Dimension(100,20));
			jbtn_gun.addActionListener(this);
			jbtn_gun.setActionCommand("Kaydet");
			jpn_gun.add(jbtn_gun);
			frmgun.add(jpnlb_gun);
			frmgun.add(jpn_gun);
			frmgun.setVisible(true);
		}
		
	public static void main(String[] args) {
		
        new Calistir();
		
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
	    int yas=sayicevir(jtext[2].getText());
	    int secilen=jlist[0].getSelectedIndex();
		if(arg0.getActionCommand().equals("Ekle")&&jtext_kontrol(jtext)&&yas!=-1)
		{
			for(int i=0;i<dlm.length;i++)
			      dlm[i].addElement(jtext[i].getText());
			dlmyas.addElement(yas);
			
			ekle(jtext,yas);//Fonskiyonu çağrıyorum eklemesi için
			for(int i=0;i<jtext.length;i++)
				jtext[i].setText("");
		  verigetir();
		}
		else if(arg0.getActionCommand().equals("Sil")&&secilen>-1) {
			sil(dlm,secilen);
			for(int i=0;i<dlm.length;i++)
	    		     dlm[i].removeElementAt(secilen);
		           dlmyas.removeElementAt(secilen);	
		}
		else if(arg0.getActionCommand().equals("Güncelle")&&secilen>-1){
               frm.setVisible(false);// Bu formu gizle
              KGuncelle(secilen);//guncelle fromunu aç
			   }
			   else if(arg0.getActionCommand().equals("Kaydet")&&jtext_kontrol(jtext_gun))
			   {  
				   yas_gun=sayicevir(jtext_gun[2].getText());
				   if(yas_gun!=-1)
				   {  
			          frm.setVisible(true);//Eğer girilen bilgiler doğruysa frm göster
			          frmgun.dispose();//guncelle formunu yoket
			      for(int i=0;i<dlm.length;i++)
				      dlm[i].removeElementAt(secilen);
			          dlmyas.removeElementAt(secilen);   
			          dlm[0].insertElementAt(jtext_gun[0].getText(),secilen);
			          dlm[1].insertElementAt(jtext_gun[1].getText(),secilen);
			          dlmyas.insertElementAt(yas_gun,secilen);
				   guncelle(secilen);
				   verigetir(); 				
				   }
			    }
}
	 public boolean jtext_kontrol(JTextField[] jtxt)
	{
		 boolean durum=true;
		 for(int i=0;i<jtxt.length;i++)
		 {
			 if(jtxt[i].getText().length()==0)
			 {
				 durum=false;
				 break;
			 }
		 }
   return durum;  
	}
  public int sayicevir(String s)
   {  int yas=0;
     
      try {
    	  yas=Integer.parseInt(s);
      }catch(Exception ex)
      {
    	  yas=-1;
      }
   return yas;	   
   }
	@Override
	public void valueChanged(ListSelectionEvent arg0) {
	    JList secilen=(JList)arg0.getSource();
	    for(int i=0;i<jlist.length;i++)
	    	  jlist[i].setSelectedIndex(secilen.getSelectedIndex());
	    jlistyas.setSelectedIndex(secilen.getSelectedIndex());
	}
//Veri Tabanı İşlemleri
Connection baglanti=null;
public void baglan()
{
     try {
    	 Class.forName("org.sqlite.JDBC");//Sql sınıfının hızlı yüklenmesi için
    	 baglanti=DriverManager.getConnection("jdbc:sqlite:Deneme.db");
     }catch(Exception ex)
     {
    	 JOptionPane.showMessageDialog(frm, "Hata="+ex.getMessage());//Bağlantıda hata olursa mesagebox' ta gösteriyoruz
     }
}
public void verigetir()
{    baglan();
    try {
    	Statement sorgu=baglanti.createStatement();//Yeni sorgu oluşturuyoruz
    	ResultSet veri=sorgu.executeQuery("select * from Kisiler");
        for(int i=0;i<dlm.length;i++)
        	dlm[i].clear();
            dlmyas.clear();
            dlmidler.clear();
    	while(veri.next())
    	{
    		dlm[0].addElement(veri.getString("Ad"));
    		dlm[1].addElement(veri.getString("SoyAd"));
    		dlmyas.addElement(veri.getInt("Yas"));
    		dlmidler.addElement(veri.getInt("KNO"));
    	}
    	veri.close();//Okuyucuyu kapatıyoruz
    	baglanti.close();//bağlantıyı kapatıyoruz
    }catch(Exception ex)
    {
    	JOptionPane.showMessageDialog(frm, "Hata="+ex.getMessage());
    }
}
public void ekle(JTextField[] jtxt,int yas)
{    baglan();//Veri tabanına bağlanıyoruz
   try {
	   PreparedStatement sorgu=baglanti.prepareStatement("insert into Kisiler(Ad,SoyAd,Yas) values(?,?,?)");
		sorgu.setString(1, jtxt[0].getText());//Paremetre ekliyoruz tip neyse ona göre
		sorgu.setString(2, jtxt[1].getText());
		sorgu.setInt(3, yas);
		sorgu.executeUpdate();
		baglanti.close();
   }catch(Exception ex)
   {
	   JOptionPane.showMessageDialog(frm, "Hata="+ex.getMessage()); 
   }
}
public void sil(DefaultListModel<String>[] dlmsil,int secilen)
{
	  baglan();//Veri tabanına bağlanıyoruz
	   try {
		  PreparedStatement sorgu=baglanti.prepareStatement("delete from Kisiler where Ad=? and SoyAd=?");
	      sorgu.setString(1,dlmsil[0].getElementAt(secilen));//Jlist secilen eleman silinecek
	      sorgu.setString(2,dlmsil[1].getElementAt(secilen));
	      sorgu.executeUpdate();//Sorguyu çalıştırıyoruz
	      baglanti.close();
	   }catch(Exception ex)
	   {
		   JOptionPane.showMessageDialog(frm, "Hata="+ex.getMessage()); 
	   }
}
public void guncelle(int secilen)
{      baglan();
	try {
		PreparedStatement sorgu=baglanti.prepareStatement("update Kisiler set Ad=?,SoyAd=?,Yas=? where KNO=?");
		sorgu.setString(1,jtext_gun[0].getText());
		sorgu.setString(2, jtext_gun[1].getText());
		sorgu.setInt(3, yas_gun);
		sorgu.setInt(4,dlmidler.getElementAt(secilen));
		sorgu.executeUpdate();
		baglanti.close();

	}catch(Exception ex)
	{
		JOptionPane.showMessageDialog(frm, "Hata="+ex.getMessage()); 
	}
}
}

 

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Tasarim implements ActionListener {
   JList[] lbox=new JList[4];
   DefaultListModel[] dlm=new DefaultListModel[4]; 
   JTextField[] tbox=new JTextField[3];
   JTextField[] tbox1=new JTextField[3];
	JButton[] buton=new JButton[4];
	JComboBox csehir,csehir2;
	JPanel anajpn,gunjpn,jpn[]=new JPanel[6];
	JScrollPane[] scp=new JScrollPane[4];
	String[] btitle= {"Ekle","Sil","Güncelle"};
	DefaultComboBoxModel dcmsehir=new DefaultComboBoxModel();
	DefaultComboBoxModel dcmsehir2=new DefaultComboBoxModel();
	JLabel[] jlb=new JLabel[4]; 
	JLabel[] jlb2=new JLabel[4]; 
	String[] baslik= {"AD","SOYAD","YAŞ","ŞEHİR"}; 
	String[] sehirler= {"K.Maraş","Osmaniye","Hatay","Gaziantep","Malatya","Mersin"};
	JFrame frm,frm2;
   public Tasarim()
	{
	    frm=new JFrame("Baslik");
		frm.setSize(600, 600);
		frm.setLayout(new FlowLayout());
		frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
		anajpn=new JPanel();
		anajpn.setPreferredSize(new Dimension(400,500));
		anajpn.setLayout(new GridLayout(1,4));
	  for(int i=0;i<tbox.length;i++)
	  {
		  jpn[i]=new JPanel();
		  dlm[i]=new DefaultListModel();
		  lbox[i]=new JList(dlm[i]);
		  scp[i]=new JScrollPane(lbox[i]);
		  scp[i].setPreferredSize(new Dimension(100,300));
		  tbox[i]=new JTextField();
		  tbox[i].setPreferredSize(new Dimension(100,25));
		  buton[i]=new JButton(btitle[i]);
		  buton[i].setPreferredSize(new Dimension(100,25));
		  buton[i].addActionListener(this);
		  buton[i].setActionCommand(btitle[i]);
		  jlb[i]=new JLabel(baslik[i]);
		  jpn[i].add(jlb[i]);
		  jpn[i].add(scp[i]);
		  jpn[i].add(tbox[i]);
		  jpn[i].add(buton[i]);
	      anajpn.add(jpn[i]);
		if(i==tbox.length-1)
		{
			  jpn[i+1]=new JPanel();
			  dlm[i+1]=new DefaultListModel();
			  lbox[i+1]=new JList(dlm[i+1]);
			  scp[i+1]=new JScrollPane(lbox[i+1]);
			  scp[i+1].setPreferredSize(new Dimension(100,300));
			  csehir=new JComboBox(dcmsehir);
			  csehir.setPreferredSize(new Dimension(100,25));
			  jlb[i+1]=new JLabel(baslik[i+1]);
			  jpn[i+1].add(jlb[i+1]);
			  jpn[i+1].add(scp[i+1]);
			  jpn[i+1].add(csehir);
		      anajpn.add(jpn[i+1]);
			break;
		}
	     
	  }
	  csehir2=new JComboBox(dcmsehir2);
	  csehir2.setPreferredSize(new Dimension(100,25));
	  for(int i=0;i<sehirler.length;i++)
	  {
		  dcmsehir.addElement(sehirler[i]);
		  dcmsehir2.addElement(sehirler[i]);
	  } 
	     frm.add(anajpn);
	     frm.setVisible(true);
	   
	}
   private void gungoster(String[] txtler)
   {
	     frm2=new JFrame("Güncelle");
	    frm2.setSize(300, 300);
	    frm2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    frm2.setLayout(new FlowLayout());
		  //Güncelleme
		  gunjpn=new JPanel();
		  gunjpn.setPreferredSize(new Dimension(200,170));
		  gunjpn.setLayout(new FlowLayout());
		  jpn[4]=new JPanel();
		  jpn[4].setPreferredSize(new Dimension(75,150));
		  jpn[5]=new JPanel();
		  jpn[5].setPreferredSize(new Dimension(100,150));
		  for(int i=0;i<tbox1.length;i++)
		  {     tbox1[i]=new JTextField();
		        tbox1[i].setPreferredSize(new Dimension(100,25));
		        tbox1[i].setText(txtler[i]);
			    jlb2[i]=new JLabel(baslik[i]);
		        jlb2[i].setPreferredSize(new Dimension(50,25));
			    jpn[4].add(jlb2[i]);
			    jpn[5].add(tbox1[i]);
		  }
		  
		  jlb2[3]=new JLabel(baslik[3]);
		  jlb2[3].setPreferredSize(new Dimension(50,25));
		  buton[3]=new JButton("Güncelle");
		  buton[3].setPreferredSize(new Dimension(100,25));
		  buton[3].setActionCommand("gun");
		  buton[3].addActionListener(this);
		  jpn[4].add(jlb2[3]);
		   jpn[5].add(csehir2);
		   jpn[5].add(buton[3]);
		  gunjpn.add(jpn[4]);
		  gunjpn.add(jpn[5]);
	     frm2.add(gunjpn);
	     frm2.setVisible(true);
   }
   //Override
   public void actionPerformed(ActionEvent arg)
   {    int yas=sayicevir(tbox[2].getText());
	    int secilen=csehir.getSelectedIndex();
	    int secilen2=lbox[0].getSelectedIndex();
	    int secilen3=csehir2.getSelectedIndex();
	   boolean durum=dogrula(tbox);
	    if(arg.getActionCommand()=="Ekle"&&yas!=-1)
	    {
	    	dlm[0].addElement(tbox[0].getText());
	    	dlm[1].addElement(tbox[1].getText());
	    	dlm[2].addElement(tbox[2].getText());
	    	dlm[3].addElement(dcmsehir.getElementAt(secilen));
	    	for(int i=0;i<tbox.length;i++)
	    		tbox[i].setText("");
	    }
	    if(arg.getActionCommand()=="Sil"&&secilen2>-1)
	    {
	    	 for(int i=0;i<lbox.length;i++)
                 dlm[i].removeElementAt(secilen2);
	    }
	    if(arg.getActionCommand()=="Güncelle"&&secilen2>-1)
	    {   String[] txtler=new String[3]; 
	        for(int i=0;i<txtler.length;i++)
	        	  txtler[i]=(String)dlm[i].getElementAt(secilen2);
	    	 frm.setVisible(false);
	    	 gungoster(txtler);
	    	 
	    }
        if(arg.getActionCommand()=="gun"&&secilen3>-1)
        {   if(dogrula(tbox1)) {
        	 for(int i=0;i<lbox.length;i++)
                 dlm[i].removeElementAt(secilen2);
         for(int i=0;i<tbox1.length;i++)
            dlm[i].insertElementAt(tbox1[i].getText(), secilen2);
            dlm[3].insertElementAt(dcmsehir2.getElementAt(secilen3), secilen2);
        	frm2.setVisible(false);
        	frm.setVisible(true);
        }
        }
   }
   private boolean dogrula(JTextField[] txt)
   {
	    int sayac=0;
	    for(int i=0;i<txt.length;i++)
	    	if(txt[i].getText().length()>0)
	    		sayac++;
         if(txt.length==sayac)
        	 return true;
         else
        	 return false;
   }
   private int sayicevir(String yas)
   {
	   int sayi=-1;
	   try {
		   sayi=Integer.parseInt(yas);
		   return sayi;
	   }catch(Exception ex)
	   {
		   sayi=-1;
		   return sayi;
	   }
   }
	public static void main(String[] arg)
	{
		new Tasarim();
	}
}

 

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

class buton1 implements ActionListener{
	
	@Override
	public void actionPerformed(ActionEvent arg)
	{
		Calistir.jlb.setText("buton1");	
	}
}
class buton2  implements ActionListener{
   
	@Override
	public void actionPerformed(ActionEvent arg)
	{
		Calistir.jlb.setText("buton2");
	}
}
public class Calistir implements ActionListener {
  static JLabel jlb=new JLabel("----");
  JButton[] jbtn=new JButton[2];
  
	public Calistir()
	{
		JFrame frm=new JFrame("Form");
		frm.setSize(300,300);
		frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frm.setLayout(new FlowLayout());
		
		jlb.setPreferredSize(new Dimension(100,25));
	    JPanel jpn=new JPanel();
	   jpn.setPreferredSize(new Dimension(100,100));
	  
	   for(int i=0;i<jbtn.length;i++)
	   {
		   jbtn[i]=new JButton("Buton"+(i+1));
		   jbtn[i].setPreferredSize(new Dimension(100,25)); 
	       jpn.add(jbtn[i]);
	   }
	  // jbtn[0].addActionListener(new buton1());// buton1 sınıfındaki actionPerformed fonksiyonun cağır
	   //jbtn[1].addActionListener(new buton2());// buton2 sınıfındaki actionPerformed fonksiyonun cağır
	   jbtn[0].addActionListener(this);//Bu sınıfta ki actionPerformed fonksiyonun cağır
	   jbtn[1].addActionListener(this);//Bu sınıfta ki actionPerformed fonksiyonun cağır
	   jbtn[0].setActionCommand("btn1");
	   jbtn[1].setActionCommand("btn2");
	   jpn.add(jlb);
	   frm.add(jpn);
	   frm.setVisible(true);
	}
	@Override
	public void actionPerformed(ActionEvent arg)
	{
		switch(arg.getActionCommand())
		{
		case "btn1":jlb.setText("buton1");break;
		case "btn2":jlb.setText("buton2");break;
		}
		
	}
	public static void main(String[] arg)
	{
		new Calistir();
	}
}