Jump to content
×
×
  • Create New...

Lorem Ipsum is simply dummy text

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s

Test Test

Lorem Ipsum is simply dummy text

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s

Test Test

Lorem Ipsum is simply dummy text

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s

Test Test

Lorem Ipsum is simply dummy text

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s

Test Test

Lorem Ipsum is simply dummy text

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s

Test Test

  • Bu Alana Reklam Verebilirsiniz
    Bu Alana Reklam Verebilirsiniz

SQLite Veri Tabanı İşlemleri Yapma Tüm Kodlar | JAVA

Rate this topic


serverIR

Recommended Posts

SQLite Veri Tabanı İşlemleri Yapma Tüm Kodlar | JAVA
  • Members

Java veritabanı işlemleri ile ilgili sitede farklı veritabanlarına bağlanıp insert, update, delete ve select işlemlerini yapmıştık. Bu yazıda da SQLite veritabanı bağlantısı kullanarak basit bir veritabanı işlemi yapılışını gösterilecektir.

 

Javada SQLite veritabanına bağlanmak için öncelikle SQLite kütüphanesini indiriyoruz. Dosyayı indirme işlemi bittikten sonra aşağıdaki görsellerde olduğu gibi indirilen kütüphaneyi projeye dahil ediyoruz.

 

java-sqlite-db-1.webp

java-sqlite-db-2.webp

Java SQLite Veritabanı İşlemleri

 

SQLite kütüphanesi projeye eklendikten sonra aşağıdaki gibi main ve alt metodları oluşturuyoruz.  Adım Adım metotları yazıp yazının sonunda çalışan tüm kodları yazalım.

 

Adım 1:  Projede kullanılacak kütüphaneleri aşağıdaki gibi projenin başında tanımlıyoruz.

import java.sql.*;
import java.util.Scanner;

 

Adım 2: main metodunu yazmadan önce veritabanı oluşturma için gerekli olan vtOlustur metodunu yazıyoruz.  SQLite veritabanı yoksa oluşturulacak varsa her hangi bir düzenleme yapılmayacaktır.

public static void yeniVtOlustur(String dosyaadi) throws ClassNotFoundException {
        try{   
         Class.forName("org.sqlite.JDBC");
         con = DriverManager.getConnection("jdbc:sqlite:deneme.db");
             stmt = con.createStatement();
             String sql = "CREATE TABLE if not exists OGRENCI " +
                            "(OGRNO INT PRIMARY KEY     NOT NULL," +
                            " OGRAD  CHAR(50)    NOT NULL, " + 
                            " OGRSOYAD   CHAR(50)     NOT NULL)"; 
             stmt.executeUpdate(sql);
             System.out.println("Veritabanı Oluşturma Başarılı");
        } catch (SQLException e) {
         System.out.println(e.getMessage());
        }
    }

 

 

Adım 3: Veritabanındaki kayıtları listelemek için gerekli olan Listele metodunu oluşturuyoruz.

public static void Listele()
    {
        try{
            
            Statement stmt=con.createStatement();  
            ResultSet rs=stmt.executeQuery("select * from ogrenci"); 
            while(rs.next())  
            System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));  
 
        }catch(Exception e){ System.out.println(e);}
         
    }

 

 

Adım 4: Kayıt Ekle metodunu aşağıdaki gibi oluşturuyoruz.

public static void Ekle()
    {
        Scanner scan= new Scanner(System.in,"iso-8859-9");
        System.out.print("Yeni Öğrenci No     :");
        int yenino = scan.nextInt();
        System.out.print("Yeni Öğrenci Adı    :");
        String ad=scan.next();
        System.out.print("Yeni Öğrenci Soyadı :");
        String soyad=scan.next();
        try{
            Statement stmt=con.createStatement(); 
            String sorgu=String.format("insert into ogrenci values( %d, '%s','%s')", yenino,ad,soyad);
            int ekleme = stmt.executeUpdate(sorgu);
            System.out.println("Kayıt Eklendi");
        }catch(Exception e){ System.out.println(e);}
        
            

 

 

Adım 5: SQLite Kayıt Guncelleme metodunu oluşturuyoruz.

 

public static void Guncelle()
    {
        Scanner scan= new Scanner(System.in,"iso-8859-9");
        try{
            Listele();
            System.out.print("Öğrenci Numarasını Girin:");
            int eskino=scan.nextInt();
            System.out.print("Yeni Öğrenci No     :");
            int yenino = scan.nextInt();
            System.out.print("Yeni Öğrenci Adı    :");
            String ad=scan.next();
            System.out.print("Yeni Öğrenci Soyadı :");
            String soyad=scan.next();
            
            String sorgu=String.format("update ogrenci set ogrno=%d, ograd='%s',ogrsoyad='%s' where ogrno=%d ", yenino,ad,soyad,eskino) ;
            
            Statement stmt=con.createStatement(); 
            int guncelleme = stmt.executeUpdate(sorgu);  
            System.out.println("Kayıtlar Güncellendi");
        }catch(Exception e){ System.out.println(e);}
    }

 

 

Adım 6: Kayıt silme işlemi için Sil metodunu oluşturuyoruz.

 

public static void Sil()
    {
        Scanner scan= new Scanner(System.in,"iso-8859-9");
        try{
            Listele();
            System.out.print("Öğrenci Numarasını Girin:");
            int eskino=scan.nextInt();  
            
            String sorgu=String.format("delete from ogrenci where ogrno=%d",eskino);
            Statement stmt=con.createStatement(); 
            int silindi = stmt.executeUpdate(sorgu);  
            System.out.println("Kayıtlar Silindi");
            
        }catch(Exception e){ System.out.println(e);}
    }
}

 

 

Adım 7: main metodunu oluştururken class içindeki genel değişkeni metodunu hemen üstünde (class içinde) oluşturuyoruz.

 

   static Connection con;
   static Statement stmt;
   static String vtAd ;
    
    public static void main(String[] args) {
       vtAd = "deneme.db";
        /*veritabanı yoksa oluşturur varsa bağlanır*/
        try
        {
           yeniVtOlustur(vtAd);
        }catch(Exception e){ System.out.println(e);}
       
        
        Scanner scan= new Scanner(System.in,"iso-8859-9");
        int secim;
        
        while(true)
        {
            System.out.println("*************");
            System.out.println("1.Listele");
            System.out.println("2.Ekle");
            System.out.println("3.Güncelle");
            System.out.println("4.Sil");
            System.out.println("5.Çıkış");
            System.out.print("Seçiminiz:");
            secim=scan.nextInt();
 
            System.out.println("*************");
            
            
            if(secim==1) Listele();
            if(secim==2) Ekle();
            if(secim==3) Guncelle();
            if(secim==4) Sil();
            if(secim==5) {
                try{
                    stmt.close();
                    con.close(); 
                }catch(Exception e){
                    System.out.println(e);
                }
                
                break;
            }            
        }
    }

 

 

Projede kullanılan tüm kodlar:

 

import java.sql.*;
import java.util.Scanner;
 
public class JavaOrnekleri {
    
   static Connection con;
   static Statement stmt;
   static String vtAd ;
    
    public static void main(String[] args) {
       vtAd = "deneme.db";
        /*veritabanı yoksa oluşturur varsa bağlanır*/
        try
        {
           yeniVtOlustur(vtAd);
        }catch(Exception e){ System.out.println(e);}
       
        
        Scanner scan= new Scanner(System.in,"iso-8859-9");
        int secim;
        
        while(true)
        {
            System.out.println("*************");
            System.out.println("1.Listele");
            System.out.println("2.Ekle");
            System.out.println("3.Güncelle");
            System.out.println("4.Sil");
            System.out.println("5.Çıkış");
            System.out.print("Seçiminiz:");
            secim=scan.nextInt();
 
            System.out.println("*************");
            
            
            if(secim==1) Listele();
            if(secim==2) Ekle();
            if(secim==3) Guncelle();
            if(secim==4) Sil();
            if(secim==5) {
                try{
                    stmt.close();
                    con.close(); 
                }catch(Exception e){
                    System.out.println(e);
                }
                
                break;
            }            
        }
    }
    public static void yeniVtOlustur(String dosyaadi) throws ClassNotFoundException {
        try{   
         Class.forName("org.sqlite.JDBC");
         con = DriverManager.getConnection("jdbc:sqlite:deneme.db");
             stmt = con.createStatement();
             String sql = "CREATE TABLE if not exists OGRENCI " +
                            "(OGRNO INT PRIMARY KEY     NOT NULL," +
                            " OGRAD  CHAR(50)    NOT NULL, " + 
                            " OGRSOYAD   CHAR(50)     NOT NULL)"; 
             stmt.executeUpdate(sql);
             System.out.println("Veritabanı Oluşturma Başarılı");
        } catch (SQLException e) {
         System.out.println(e.getMessage());
        }
    }
    public static void Listele()
    {
        try{
            
            Statement stmt=con.createStatement();  
            ResultSet rs=stmt.executeQuery("select * from ogrenci"); 
            while(rs.next())  
            System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));  
 
        }catch(Exception e){ System.out.println(e);}
         
    }
    
    public static void Ekle()
    {
        Scanner scan= new Scanner(System.in,"iso-8859-9");
        System.out.print("Yeni Öğrenci No     :");
        int yenino = scan.nextInt();
        System.out.print("Yeni Öğrenci Adı    :");
        String ad=scan.next();
        System.out.print("Yeni Öğrenci Soyadı :");
        String soyad=scan.next();
        try{
            Statement stmt=con.createStatement(); 
            String sorgu=String.format("insert into ogrenci values( %d, '%s','%s')", yenino,ad,soyad);
            int ekleme = stmt.executeUpdate(sorgu);
            System.out.println("Kayıt Eklendi");
        }catch(Exception e){ System.out.println(e);}
        
            
    }
    
    public static void Guncelle()
    {
        Scanner scan= new Scanner(System.in,"iso-8859-9");
        try{
            Listele();
            System.out.print("Öğrenci Numarasını Girin:");
            int eskino=scan.nextInt();
            System.out.print("Yeni Öğrenci No     :");
            int yenino = scan.nextInt();
            System.out.print("Yeni Öğrenci Adı    :");
            String ad=scan.next();
            System.out.print("Yeni Öğrenci Soyadı :");
            String soyad=scan.next();
            
            String sorgu=String.format("update ogrenci set ogrno=%d, ograd='%s',ogrsoyad='%s' where ogrno=%d ", yenino,ad,soyad,eskino) ;
            
            Statement stmt=con.createStatement(); 
            int guncelleme = stmt.executeUpdate(sorgu);  
            System.out.println("Kayıtlar Güncellendi");
        }catch(Exception e){ System.out.println(e);}
    }
    
    public static void Sil()
    {
        Scanner scan= new Scanner(System.in,"iso-8859-9");
        try{
            Listele();
            System.out.print("Öğrenci Numarasını Girin:");
            int eskino=scan.nextInt();  
            
            String sorgu=String.format("delete from ogrenci where ogrno=%d",eskino);
            Statement stmt=con.createStatement(); 
            int silindi = stmt.executeUpdate(sorgu);  
            System.out.println("Kayıtlar Silindi");
            
        }catch(Exception e){ System.out.println(e);}
    }
}

 

Yazıdaki basit örnekle Java programlama dili ile SQLite veritabanı üzerinde insert, updata, delete gibi temel veritabanı işlemlerinin yapmak hedeflenmiştir.

 

Fırsat olursa sitede Access, SQL Server ve Oracle bağlantıları ile de ilgili örnek yazıp paylaşacağım. Java kategorisi altında java ile ilgili yazılan diğer örneklere ulaşabilirsiniz.

Edited by serverIR

----------------------------------------------------------------------------

Link to comment
https://weblep.com/topic/205-sqlite-veri-taban%C4%B1-i%CC%87%C5%9Flemleri-yapma-t%C3%BCm-kodlar-java/
Share on other sites


  • serverIR changed the title to SQLite Veri Tabanı İşlemleri Yapma Tüm Kodlar | JAVA
Konu Altı Reklam 1
Konu Altı Reklam 2
  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Posted Images

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...