JDBC with Sqlite

                                              JDBC with Sqlite

Step1:- 
download sqlite JDBC jar with bellow URL.

Step2:-
create java application and import Jar.

select application -> Right click -> Build Path -> Configure Build Path -> Libraries -> click "add external jars"-> select "slite-jdbc-3.23.1.jar"
1.
2.

Step3 :
write Java-Database connectivity logic

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

public class SqliteJdbc {

public static void main(String[] args) throws ClassNotFoundException, SQLException {
// load the sqlite-JDBC driver using the current class loader
    Class.forName("org.sqlite.JDBC");

    Connection connection = null;
    String url="jdbc:sqlite:G:/SW/Development/database/sqlite-tools-win32-x86-3240000/sqlite-tools-win32-x86-3240000/sample.db";
    
    // create a database connection
      //connection = DriverManager.getConnection("jdbc:sqlite:sample.db");
      connection = DriverManager.getConnection(url);
      
      Statement statement = connection.createStatement();
      statement.setQueryTimeout(30);  // set timeout to 30 sec.

      statement.executeUpdate("create table person (id integer, name string)");
      statement.executeUpdate("insert into person values(1, 'Rama')");
     System.out.println("Update Query executed successfully.");
     
     System.out.println("Get Resultset : ");
     String sql = "SELECT id, name FROM person";
      ResultSet rs = statement.executeQuery(sql);
      //STEP 5: Extract data from result set
      while(rs.next()){
         //Retrieve by column name
         int id  = rs.getInt(1);
         String name = rs.getString(2);

         //Display values
         System.out.println("ID: " + id);
         System.out.println("name: " + name);
      }
      if (connection != null) {
      connection.close();
          }
}

execute above program and get Results

Comments

Popular posts from this blog

Spring Boot + Maven + RestFul + JPA + Sqlite

View & Materialized View