자바

우리는 SQLite JDBC 드라이버의 수정된 버전을 사용할 수 있다.


테스트 단계는 다음과 같다:


1. 컴퓨터에서 LiteSync를 컴파일하거나 사전 컴파일된 이진 파일을 사용하십시오. 테스트 목적으로 이메일을 통해 요청할 수 있다.

2. 수정된 드라이버 다운로드:

mkdir litesync-jdbc

cd litesync-jdbc

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

tar zxvf litesync-jdbc.tar.gz


사용

2개의 터미널에서 테스트를 실행할 수 있다:


./run PrimaryNode

./run SecondaryNode



예제 코드
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);
      }
    }
  }
}