Search code examples
javaintersystems-cache

Java Bind connected to the Caché code has been deprecated, how to replace?


Currently, I have used the following code (JDBC way) to connect to InterSystem Caché:

import java.sql.*;

import com.intersys.jdbc.CacheDataSource;

/**
 * @author prd
*/
public class NewTinyBind {

public static void main(String[] args) {
    Connection dbconnection = null;
    Statement stmt = null;
    try {
        String url = "jdbc:Cache://localhost:1972/SIMPLE";
        String username = "_SYSTEM";
        String password = "SYS";
        CacheDataSource cds = new CacheDataSource();
        cds.setURL(url);
        dbconnection = cds.getConnection(username, password);
        stmt = dbconnection.createStatement();
        java.sql.ResultSet rs = stmt.executeQuery(" SELECT * FROM TEST ");
        int cols = rs.getMetaData().getColumnCount();
        while (rs.next()) {
            for (int i = 1; i < cols + 1; i++) {
                String colname = rs.getMetaData().getColumnName(i);
                Object value = rs.getObject(i);
                System.out.println("colname:" + colname + ",value:" + value);
            }
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            stmt.close();
            dbconnection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

}

I would like to use this way (Java Bind) to connect to InterSystem Caché, but I am in my environment (jdk 1.8), the following code is prompted out of date (@ java.lang.Deprecated), but the document does not prompt how to replace these outdated The code that someone knows? Thank you.

code by https://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=BLJV_using#BLJV_using_objects_sample

import java.io.*;
import java.util.*;
import com.intersys.objects.*;

public class SampleApplication {
  public static void main(String[] args){
      Database dbconnection = null;
      String url="jdbc:Cache://localhost:1972/SAMPLES";
      String username="_SYSTEM";
      String password="sys";
      ObjectServerInfo info = null;
      Sample.Person person = null;

      try {
          // Connect to Cache on the local machine, in the SAMPLES namespace
          dbconnection = CacheDatabase.getDatabase (url, username, password);

          // Open an instance of Sample.Person,
          // whose ID is read in from the console
          InputStreamReader isr = new InputStreamReader(System.in);
          BufferedReader br = new BufferedReader(isr);
          System.out.print("Enter ID of Person object to be opened:");
          String strID = br.readLine();

          // Use the entered strID as an Id and use that Id to
          // to open a Person object with the _open() method inherited
          // from the Persistent class. Since the _open() method returns
         // an instance of Persistent, narrow it to a Person by casting.
          person = (Sample.Person)Sample.Person._open(dbconnection, new Id(strID));

          // Fetch some properties of this object
          System.out.println("Name: " + person.getName());
          System.out.println("City: " + person.getHome().getCity());

          // Modify some properties
          person.getHome().setCity("Ulan Bator");

          // Save the object to the database
          person._save();

          // Report the new residence of this person */
          System.out.println( "New City: " + person.getHome().getCity());

          /* de-assign the person object */
          dbconnection.closeObject(person.getOref());
          person = null;

          // Close the connection
          dbconnection.close();

    } catch (Exception ex) {
      System.out.println("Caught exception: "
        + ex.getClass().getName()
        + ": " + ex.getMessage());
    }
  }
}

Caché jdk 1.8 API - Java Bind


Solution

  • The same question on InterSystems Developer Community portal. Documentation says:

    Important:

    The Caché Java Binding is Deprecated
    Java Persistence Architecture (JPA) is the recommended persistence technology for complex object hierarchies in Java projects. Caché and Ensemble currently support JPA 1.0 and 2.0 via the Hibernate implementations of the JPA specifications. See “Using the Caché Hibernate Dialect” in Using Caché with JDBC.

    Extreme Event Persistence (XEP) is the recommended persistence technology for high-performance simple to medium complexity object hierarchies in Java projects. See “Using eXTreme Event Persistence” in Using Java with Caché eXTreme.