Java QuickStart

15 minutes
estimated time of completion.
The InterSystems JDBC driver supports three lightweight Java APIs that provide direct access to InterSystems IRIS™ databases via relational tables, objects, or multidimensional storage.
Java features available in InterSystems IRIS include:
  • Relational access through the JDBC API
  • Object access through the InterSystems XEP API
  • Access to multidimensional storage through the InterSystems Native API
  • Multi-model access to use multiple APIs together
  • Support for third-party APIs such as Hibernate
  • An underlying consistent structure, avoiding data duplication
Watch the video or try the exercise below. With Java, you can interact with InterSystems IRIS relationally (with JDBC), using objects (with XEP or Hibernate), or natively (with the Native API). For this exercise, you can use your favorite IDE that supports Java, such as Eclipse. In the steps below, you will access a set of sample stock data using each of the ways described.
Note
If you don’t have InterSystems IRIS set up yet, get a free development sandbox here.

Setup

  1. Verify you have an instance of InterSystems IRIS, and an IDE that supports Java (such as Eclipse). If you are using AWS, Azure, or GCP, verify that you have followed the steps to change the password for InterSystems IRIS.
  2. Load sample stock data into InterSystems IRIS:
    1. Clone or download this repository: https://github.com/intersystems/Samples-Stock-Data. This repository has a folder named data, which contains all files needed to load the sample stock data.
    2. In the Management Portal, navigate to System Explorer > Classes and import DemoStockCls.xml and StocksUtil.xml files into the USER namespace from the data folder.
    3. In the InterSystems IRIS Terminal, type the following command. replace repo_home with the directory of Sample-Stock-Data repo that you recently cloned: do ##class(Demo.Stock).LoadData("repo_home/data/all_stocks_1yr.csv")
  3. To get the sample Java code, download or clone the following repo into your IDE: http://github.com/intersystems/quickstarts-javaWith Eclipse:
    • Select File > Import > Git > Projects from Git. Click Next.
    • Enter URI: https://github.com/intersystems/quickstarts-java
    • Select the master branch. Click Next.
    • Choose import existing project. Click Next.
    • Click Finish.
    • Open config.txt file, located inside the Solutions package, and modify the IP and password to be the correct values for your InterSystems IRIS instance. Port and username are most likely the defaults but you can verify those as well.
  4. Save the sample code in your IDE. You should now have several classes for JDBC, XEP, Native API, and multi-model. Due to its complexity, Hibernate requires separate set up for configuration files, which will be explained in detail in Step 6.

SQL-based access with JDBC

Use the standard JDBC API for SQL-based access to relational tables.

  1. Open jdbcplaystocksTask7.java to view the JDBC code. You will notice the connection string uses these variables to connect: String dbUrl = protocol + ip + ":" + port + "/" + namespace;
  2. Run jdbcplaystocksTask7.java. This code simulates a stock trading application, allowing you to manage your personal portfolio and see how you would have done selling on a particular date.
    • Run option 1: View the top 10 stocks on 2016-08-12.
    • Run option 2: Create the Portfolio table.
    • Run option 3: Add three new portfolio items (for AMZN, GOOGL, and EQIX) using a value between the opening and closing price on 2016-08-12.
    • Run option 6: Then, see how much you would have made on your portfolio if selling on 2017-08-10.
    • Run option 7: Quit.

XEP for high performance inserts

Use XEP for high performance, real-time object insertions.

  1. Modify xepplaystocksTask6.java to view the XEP code. You will notice the connection string uses these variables to connect: xepPersister.connect(ip, port, namespace, username, password);
  2. Run xepplaystocksTask6.java. This code simulates real-time stock trades.
    • Run option 1: Make a sample trade. For example, trade two shares on 2016-08-12 for $300/share, using your own name as the trade.
    • Run option 2: Save this trade.
    • Run option 3: Generate 500 generic trades.
    • Run option 4: Retrieve the trade objects from the database again and update the names to prepend NYSE- to each stock name.
    • Run option 6: Quit.
With XEP, you can efficiently stores objects directly to InterSystems IRIS, requiring no translation to rows.

Native API to store and exercise a custom structure

Use the Native API to store to a custom structure and call methods or routines.

    1. Modify nativeplaystocksTask5.java to view the Native API code. You will notice the connection string uses these variables to connect: String dbUrl = protocol + ip + ":" + port + "/" + namespace;. Notice this code uses the standard JDBC connection.
    2. Run nativeplaystocksTask5.java. This code stores stock data in a custom data structure.
      • Notice the version of IRIS is immediately printed to the screen. This is retrieved by calling a routine within InterSystems IRIS.
      • Run option 2: Store the stock data in a custom structure.
      • Run option 3: Retrieve and view the stock data from this custom structure.
      • Run option 4: Call population methods within InterSystems IRIS to generate better information for trades (as seen in the XEP example above).
      • Run option 5: Quit. Run nativeplaystocksTask5.java. This code stores stock data in a custom data structure.
With the Native API, you can efficiently store data in your own custom data structure, allowing you to answer questions you could not answer using tables or objects. Additionally, you can call class methods and routines from within InterSystems IRIS using the Native API.

Use JDBC, XEP, and Native access side by side

Use JDBC, XEP, and Native access side by side, choosing the best model for each task.

  1. Modify multiplayTask4.java to see how all APIs work together. You will notice the connection string uses these variables to connect: xepPersister.connect(ip, port, namespace, username, password); Notice this code connects using the XEP connection. Since EventPersister inherits from IRISConnection, all ways to connect leverage the same underlying connection to InterSystems IRIS.
  2. Run multiplayTask4.java. This code stores stock company information.
    • JDBC is used to quickly retrieve all distinct stock names from the Demo.Stock table.
    • Native API is used to call population methods within InterSystems IRIS for founder and mission statement.
    • XEP is used to store these objects directly to the database, avoiding any translation back to tables.
With InterSystems IRIS, you can use one connection and use the method that is best for each task, decreasing the time it takes to develop your applications while also increasing your application performance. XEP efficiently stores objects directly to InterSystems IRIS, requiring no translation to rows. It is worth mentioning that JDBC can also be used now to retrieve the data stored using XEP, reducing data redundancy.

Use Hibernate to store objects

Hibernate is an example of a third-party API that you can use to store objects to InterSystems IRIS.

  1. Modify hibernate.cfg.xml to connect to your database using your particular port, IP, namespace (if different than the default USER), username, and password. Find the following line of code and replace your values: <property name="connection.url">jdbc:IRIS://YourIP:YourPort/User?useSSL=false</property> Notice this code uses the standard JDBC connection.
  2. Run hibernateplaystocksTask6.java. This code stores stock data in a custom data structure.
    • Run option 1: Make a new trade for AMZN on 2016-08-12 with price: 200, # of shares: 2 and your own information as a new trader.
    • Run option 4: Find all traders with your last name.
    • Run option 5: View the leaderboard.
    • Run option 6: Quit.
With Hibernate, you can connect to InterSystems IRIS using object-relational mapping. Hibernate with InterSystems IRIS works exactly the way you expect, allowing you to easily upgrade your Hibernate applications to work with InterSystems IRIS.