Difference between using SQLiteConnection and DriverManager Java

6

I'm connecting to the Sqlite database with the following code:

 import org.sqlite.SQLiteConnection;


 public SQLiteConnection conn() throws SQLException{
    String path = System.getProperty("user.dir");
  try {
   return new SQLiteConnection(path, "DadosPastas.s3db");
 } catch (Exception e) {
    return null;
 }
}

When I debug the IDE, the result is that the connection has been established successfully.

But I looked at several examples as follows:

import java.sql.*;

public class SQLiteJDBC {
public static void main( String args[] ) {
Connection c = null;

try {
 Class.forName("org.sqlite.JDBC");
 c = DriverManager.getConnection("jdbc:sqlite:test.db");
} catch ( Exception e ) {
 System.err.println( e.getClass().getName() + ": " + e.getMessage() );
 System.exit(0);
}
 System.out.println("Opened database successfully");
 }
}

My question is what is the difference in technical and performance aspects between using SQLiteConnection directly or using the DriverManager.

Link for the Sqlite library.

    
asked by anonymous 26.06.2017 / 18:43

1 answer

7

I do not know what you mean by "technicalities," but in terms of performance, it's notorious that JDBC is faster. I did an algorithm to test:

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.sqlite.SQLiteConnection;

public class SQLiteConnTest {


    public static void main(String[] args) {

        long startTime, endTime;
        double duration;

        File path = new File(System.getProperty("user.dir"));
        System.out.println("Conexao com SQLiteConnection...");

        startTime = System.nanoTime();
        sqliteConnection(path);
        endTime = System.nanoTime();

        duration = (endTime - startTime) / 1000000d;

        System.out.println("Tempo(ms): " + String.format("%.3f", duration));

        System.out.println("Conexao com JDBC...");

        startTime = System.nanoTime();
        jdbcConnection(path);
        endTime = System.nanoTime();

        duration = (endTime - startTime) / 1000000d;

        System.out.println("Tempo(ms): " + String.format("%.3f", duration));

    }

    public static void sqliteConnection(File path) {

        try {
            SQLiteConnection conn = new SQLiteConnection(path.getAbsolutePath(), "DadosPastas.s3db");
            System.out.println("SQLiteConnection has opened");
            conn.close();
            System.out.println("SQLiteConnection has closed");

        } catch (SQLException e) {

            e.printStackTrace();
        }
    }

    public static void jdbcConnection(File path) {

        try {

            Class.forName("org.sqlite.JDBC");
            Connection c = DriverManager.getConnection("jdbc:sqlite:" + path + "/test.db");
            System.out.println("JDBC Connection has opened");
            c.close();
            System.out.println("JDBC Connection has closed");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

In the test, I calculate the time in which both forms take to open and close the connection next.

In the first test, I ran without any of the benches being created, to see if having to create it influences time. The output was:

Conexao com SQLiteConnection...
SQLiteConnection has opened
SQLiteConnection has closed
Tempo(ms): 227,829
Conexao com JDBC...
JDBC Connection has opened
JDBC Connection has closed
Tempo(ms): 3,559

Once with the created banks, I ran again:

Conexao com SQLiteConnection...
SQLiteConnection has opened
SQLiteConnection has closed
Tempo(ms): 249,077
Conexao com JDBC...
JDBC Connection has opened
JDBC Connection has closed
Tempo(ms): 3,039

It is noticed that the time difference between the two is great, with JDBC being much more performative than the SQLiteConnection class. This is because jdbc is native, which will make it almost always be faster.

I do not know where you found this way to connect with this class, but even the driver documentation itself suggests use DriverManager , and do not say anything about this way you tested it.

    
28.06.2017 / 20:48