Members serverIR Posted April 26, 2022 Members #1 Share Posted April 26, 2022 (edited) 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 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 April 26, 2022 by serverIR Quote ---------------------------------------------------------------------------- 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 More sharing options...