Sqlite dan Java

31 Dec, 2020 | Ditulis oleh : Fadlur Rohman

Rekomendasi Kelas
Membangun Toko Online Menggunakan Laravel

Laravel adalah salah framework php dengan pengguna terbanyak. Dengan banyaknya pengguna otomatis ketika kita menghadapi error akan lebih mudah menemukan solusin...

Sebelum kita menggunakan sqlite pada program java, kita harus mempunyai SQlite JDBC driver yang bisa didownload bitbucket.org/xerial/sqlite-jdbc/downloads. Setelah kita download SQLite JDBC, kita import file .jar ke folder library project.

Kalo sudah langsung kita mulai dengan membuat koneksi ke database sqlite. Buat class java dengan nama “TestSqlite” atau apa aja terserah.

1. Koneksi Database.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author fadlur
 */
public class TestSqlite {
    static Connection c = null;
    static Statement stmt = null;
    public static void main(String[] args){
        try{
            c = DriverManager.getConnection("jdbc:sqlite:test.db");
        }catch(SQLException e){
            System.err.println(e.getClass().getName() + " : " + e.getMessage());
            System.exit(0);
        }
        System.out.println("Openned database successfully");
    }
}

2. Create Table

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author fadlur
 */
public class TestSqlite {
    static Connection c = null;
    static Statement stmt = null;
    public static void main(String[] args){

        try{
            c = DriverManager.getConnection("jdbc:sqlite:test.db");
            stmt = c.createStatement();
            String sql = "create table company "+
                    "(id int primary key not null,"+
                    "name text not null,"+
                    "age int not null,"+
                    "address char(50),"+
                    "salary real)";
            stmt.executeUpdate(sql);
            stmt.close();
            c.close();
        }catch(SQLException e){
            System.err.println(e.getClass().getName() + " : " + e.getMessage());
            System.exit(0);
        }
        System.out.println("Table created successfully");
    }
}

3. Insert Data

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author fadlur
 */
public class TestSqlite {
    static Connection c = null;
    static Statement stmt = null;
    public static void main(String[] args){

        try{
            c = DriverManager.getConnection("jdbc:sqlite:test.db");
            c.setAutoCommit(false);
            stmt = c.createStatement();
            String sql = "INSERT into company values (1,'fadlur',20,'kudus',200000);";
            sql = "INSERT into company values (2,'rohman',22,'jepara',200000);";
            sql = "INSERT into company values (3,'rohim',24,'pati',200000);";
            stmt.executeUpdate(sql);
            c.commit();
            stmt.close();
            c.close();
        }catch(SQLException e){
            System.err.println(e.getClass().getName() + " : " + e.getMessage());
            System.exit(0);
        }
        System.out.println("Data berhasil disimpan");
    }
}

4. Select Data

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author fadlur
 */
public class TestSqlite {
    static Connection c = null;
    static Statement stmt = null;
    public static void main(String[] args){

        try{
            c = DriverManager.getConnection("jdbc:sqlite:test.db");
            c.setAutoCommit(false);
            stmt = c.createStatement();
            ResultSet rs = stmt.executeQuery("select * from company;");
            while (rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("name");
                int age = rs.getInt("age");
                String address = rs.getString("address");
                float salary = rs.getFloat("salary");

                System.out.println("ID = "+id);
                System.out.println("Nama = "+name);
                System.out.println("Age = "+age);
                System.out.println("Address = "+address);
                System.out.println("Salary = "+salary);
            }
            rs.close();
            stmt.close();
            c.close();
        }catch(SQLException e){
            System.err.println(e.getClass().getName() + " : " + e.getMessage());
            System.exit(0);
        }
        System.out.println("Select Data berhasil");
    }
}

5. Update Data

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author fadlur
 */
public class TestSqlite {
    static Connection c = null;
    static Statement stmt = null;
    public static void main(String[] args){

        try{
            c = DriverManager.getConnection("jdbc:sqlite:test.db");
            c.setAutoCommit(false);
            stmt = c.createStatement();
            String sql = "UPDATE company set name = 'abdurrohman' where id=3;";
            stmt.executeUpdate(sql);
            c.commit();
            ResultSet rs = stmt.executeQuery("select * from company;");
            while (rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("name");
                int age = rs.getInt("age");
                String address = rs.getString("address");
                float salary = rs.getFloat("salary");

                System.out.println("ID = "+id);
                System.out.println("Nama = "+name);
                System.out.println("Age = "+age);
                System.out.println("Address = "+address);
                System.out.println("Salary = "+salary);
            }
            rs.close();
            stmt.close();
            c.close();
        }catch(SQLException e){
            System.err.println(e.getClass().getName() + " : " + e.getMessage());
            System.exit(0);
        }
        System.out.println("Update Data berhasil");
    }
}

6. Delete Data

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author fadlur
 */
public class TestSqlite {
    static Connection c = null;
    static Statement stmt = null;
    public static void main(String[] args){

        try{
            c = DriverManager.getConnection("jdbc:sqlite:test.db");
            c.setAutoCommit(false);
            stmt = c.createStatement();
            String sql = "DELETE from company where id = 1";
            stmt.executeUpdate(sql);
            c.commit();
            ResultSet rs = stmt.executeQuery("select * from company;");
            while (rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("name");
                int age = rs.getInt("age");
                String address = rs.getString("address");
                float salary = rs.getFloat("salary");

                System.out.println("ID = "+id);
                System.out.println("Nama = "+name);
                System.out.println("Age = "+age);
                System.out.println("Address = "+address);
                System.out.println("Salary = "+salary);
            }
            rs.close();
            stmt.close();
            c.close();
        }catch(SQLException e){
            System.err.println(e.getClass().getName() + " : " + e.getMessage());
            System.exit(0);
        }
        System.out.println("Delete Data berhasil");
    }
}

Selamat mencoba semoga manfaat.




Related Post :

Operasi trigonometri sederhana menggunakan java

31 Dec, 2020 - Fadlur Rohman

Perhitungan trigonometri menggunakan java kadang membingungkan, karena...

Sqlite dan Java

31 Dec, 2020 - Fadlur Rohman

Sebelum kita menggunakan sqlite pada program java, kita harus mempunya...

Encapsulation Java

31 Dec, 2020 - Fadlur Rohman

Encapsulation adalah salah satu dari empat konsep fundamental OOP sela...

Tipe Data Variabel Dalam Bahasa Pemrograman Java

31 Dec, 2020 - Fadlur Rohman

Variabel adalah sesuatu yang digunakan untuk menyimpan nilai yang dial...

Interface Dalam Bahasa Pemrograman Java

31 Dec, 2020 - Fadlur Rohman

Seperti yang sudah kita pelajari di awal, object mendefinisikan intera...

Inheritance Dalam Bahasa Pemrograman Java

31 Dec, 2020 - Fadlur Rohman

Di dalam pemrograman beroritentasi object seperti java mengijinkan &ld...

Mau Bikin Aplikasi Web & Aplikasi Android?

Saya membuka jasa pembuatan aplikasi web dan android untuk aplikasi seperti toko online, monitoring (IoT), interface mesin absensi atau kasir

Silahkan kontak link di bawah ini untuk teknis dan harganya