Java

Nous utilisons une version modifiée pilote SQLite JDBC



Tester

Télécharger le pilote modifié dans un dossier vide :


mkdir litesync-jdbc

cd litesync-jdbc

wget litesync.io/download/litesync-jdbc-for-testing.tar.gz

tar zxvf litesync-jdbc-for-testing.tar.gz


Exécuter ensuite les tests d'exemple dans 2 terminaux distincts :


./run PrimaryNode

./run SecondaryNode



Code d'exemple
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.json.*;

public class SecondaryNode
{
  public static void main(String[] args)
  {
    Connection connection = null;
    try
    {
      // create a database connection
      String uri = "file:sec.db?node=secondary&connect=tcp://127.0.0.1:1234";
      connection = DriverManager.getConnection("jdbc:sqlite:" + uri);

      // print some text
      System.out.println("secondary database open");

      Statement statement = connection.createStatement();
      // check if the db is ready
      while (true) {
        ResultSet rs = statement.executeQuery("PRAGMA sync_status");
        rs.next();
        JSONObject obj = new JSONObject(rs.getString(1));
        if (obj.getBoolean("db_is_ready")) break;
        Thread.sleep(250);
      }

      // print some text
      System.out.println("secondary database ready");

      // now we can access the db
      start_access(connection);

      // exiting the app
      connection.close();
    }
    catch(SQLException e)
    {
      // if the error message is "out of memory",
      // it probably means no database file is found
      System.err.println(e.getMessage());
    }
    catch (Exception e)
    {
      System.out.println(e);
    }
    finally
    {
      try
      {
        if(connection != null)
          connection.close();
      }
      catch(SQLException e)
      {
        // connection close failed.
        System.err.println(e);
      }
    }
  }
}


Les limitations de cette bibliothèque de test : votre base de données principale ne peut contenir qu'une seule table et le chiffrement est désactivé. Tout le reste est actif. Vous aurez accès à la bibliothèque complète une fois que vous ayez la licence.