0

Java Sqlite Database Sınıfı Oluşturma

Merhaba arkadaşlar java sqlite dersimizde temel sql işlemlerini göstermek için hazırladığımız minik projenin kodlarını paylaşıyoruz. Database sınıfımız Singleton Pattern’i ile hazırlanmıştır. Bağlantı , Tablo oluşturma, Kayıt seçme, Silme , Güncelleme gibi temel metodlar bulunmakta .  Herkese kolay gelsin . İşte kodlar…

package com.company;


import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;

public class Database {
    private static Database ourInstance = new Database();

    public static Database getInstance() {
        return ourInstance;
    }


    private Database() {
    }
    

    public int updateKisi(Kisi k){
        int result=0;
        Connection connection=this.connect();
        PreparedStatement ps=null;
        String sql="update kisiler set adsoyad=?, telefon=?,email=?,adres=? where id=?";

        try {

            ps=connection.prepareStatement(sql);
            ps.setString(1,k.getAdsoyad());
            ps.setString(2,k.getTelefon());
            ps.setString(3,k.getEmail());
            ps.setString(4,k.getAdres());
            ps.setInt(5,k.getId());
           result=ps.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            try {
                ps.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


        return result;
    }

    public int deletefromTable(int deletedId){

        Connection connection=this.connect();
        PreparedStatement ps=null;
        String sql="delete from kisiler where id=?";
        int result=0;

        try {
            ps=connection.prepareStatement(sql);
            ps.setInt(1,deletedId);
            result=ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            try {
                ps.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

        return result;
    }

    public List<Kisi> getDatas(){

        String sql="select * from kisiler";
        List<Kisi> liste=new ArrayList<>();
        Connection connection=this.connect();
        Statement statement=null;
        ResultSet rs=null;
        try {
             statement=connection.createStatement();
             rs=statement.executeQuery(sql);
             while (rs.next()){

                 liste.add(new Kisi(rs.getInt("id"),
                                 rs.getString("adsoyad"),
                                 rs.getString("telefon"),
                                 rs.getString("email"),
                                 rs.getString("adres")));
             }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {

            try {
                rs.close();
                statement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }


        }

        return  liste;

    }

    public int insertKisilerTable(String adsoyad, String telefon, String email, String adres){
        // adsoyad,telefon,email,adres

        int result=0;
        Connection connection=this.connect();
        String insertSql="insert into kisiler(adsoyad,telefon,email,adres) values(?,?,?,?)";
        PreparedStatement ps=null;
        try {
            ps=connection.prepareStatement(insertSql);
            ps.setString(1,adsoyad);
            ps.setString(2,telefon);
            ps.setString(3,email);
            ps.setString(4,adres);

            result=ps.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {

            try {
                ps.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        return result;
    }

    public void createTable()  {

        String tableSql="create table if not exists kisiler (id integer primary key autoincrement," +
                "adsoyad text not null, telefon text not null, email text not null, adres text not null) ";

            Connection connection = this.connect();
            Statement statement=null;
            try {
                 statement = connection.createStatement();
                statement.execute(tableSql);
                System.out.println("Tablo oluştuma işlemi başarı ile tamamlandı.");

            } catch (SQLException e) {
                e.printStackTrace();
            }
            finally {
                if(statement!=null){
                    try {
                        statement.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                if(connection!=null){
                    try {
                        connection.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }


    }

    public Connection connect(){
        String dbUrl="jdbc:sqlite:rehber.db";
        Connection connection=null;
        try {
            connection=DriverManager.getConnection(dbUrl);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }
}
package com.company;

import javax.xml.crypto.Data;
import java.sql.SQLException;
import java.util.List;

public class Main {

    public static void main(String[] args) {
	// write your code here

  /*      System.out.println(
    Database.getInstance().insertKisilerTable("Akın Özkayıt",
            "509 645 45 45",
            "akozkayit@gmail.com",
            "ÇORUM")+
        " kayıt Eklendi ");*/


      //  System.out.println(Database.getInstance().deletefromTable(3)+ " kayıt silindi \n");

        int sonuc = Database.getInstance().updateKisi(new Kisi(5,"Selami ÖZYILMAZ","544 444 44 44",
               "selamiPrometus@gmail.com","ANKARA" ));


        List<Kisi> kisiler=Database.getInstance().getDatas();


        System.out.println("              REHBER                                   ");
        System.out.println(" -------------------------------------------------------");
        for (Kisi k:kisiler
             ) {
            System.out.println(k.getId()+ "   |  "+k.getAdsoyad() +"  |  "+k.getEmail()+ "  |    "+ k.getTelefon()+ "   |   " +k.getAdres() );
        }
        System.out.println("Toplam Kayıt sayısı "+kisiler.size());

        System.out.println(" \n "+sonuc+" kayıt güncellendi ");

    }
}
package com.company;

public class Kisi {
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    private int id;
    private String adsoyad;
    private String telefon;
    private String email;
    private String adres;

    public Kisi(int id, String adsoyad, String telefon, String email, String adres) {
        this.adsoyad = adsoyad;
        this.id=id;
        this.telefon = telefon;
        this.email = email;
        this.adres = adres;
    }

    public String getAdsoyad() {
        return adsoyad;
    }

    public void setAdsoyad(String adsoyad) {
        this.adsoyad = adsoyad;
    }

    public String getTelefon() {
        return telefon;
    }

    public void setTelefon(String telefon) {
        this.telefon = telefon;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAdres() {
        return adres;
    }

    public void setAdres(String adres) {
        this.adres = adres;
    }
}

admin

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir