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 indir bağlantısını kullanarak indiriyoruz. Dosyayı indirme işlemi bittikten sonra aşağıdaki görsellerde olduğu gibi indirilen kütüphaneyi projeye dahil ediyoruz.



Adım 1: Projede kullanılacak kütüphaneleri aşağıdaki gibi projenin başında tanımlıyoruz.
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.
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.
Adım 7: main metodunu oluştururken class içindeki genel değişkeni metodunu hemen üstünde (class içinde) oluşturuyoruz.
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.
Javada SQLite veritabanına bağlanmak için öncelikle SQLite kütüphanesini indir bağlantısını kullanarak indiriyoruz. Dosyayı indirme işlemi bittikten sonra aşağıdaki görsellerde olduğu gibi indirilen kütüphaneyi projeye dahil ediyoruz.



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.
1 2 3 4 | import java.sql.*; import java.util.Scanner; |
2 3 4 5 6 7 8 9 10 11 12 13 14 | 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);}
}
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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);} } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | 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
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | 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);} } } |
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.