Easysoft Blog

Thu, 06 September 2018

Salesforce SOQL from Java

The Salesforce ODBC Driver extends the number of applications that you can use Salesforce SOQL from. In this blog, we describe how to run SOQL statements from Java by using the Salesforce ODBC driver under our JDBC-ODBC Bridge, Java to ODBC connector.

To get started:

Before you can use the Salesforce.com ODBC Driver to connect your application to Salesforce.com, you need to configure an ODBC data source. An ODBC data source stores the connection details for the target database (e.g. Salesforce.com) and the ODBC driver that is required to connect to it (e.g. the Salesforce.com ODBC driver).

  1. Start ODBC Administrator. To do this, in the Windows Run dialog box, type:
    %windir%\syswow64\odbcad32.exe
  2. In the ODBC Administrator, choose the System DSN tab, and then choose Add.
  3. In the Create New Data Source dialog box, choose Easysoft Salesforce SOQL ODBC Driver, and then choose Finish.
  4. Complete the Easysoft Salesforce SOQL ODBC Driver DSN Setup dialog box:
    Setting Value
    DSN Salesforce.com
    User Name The name of your Salesforce.com user. For example, myuser@mydomain.com.
    Password The password for your Salesforce.com user.
    Token The security token for your Salesforce.com user, if required.

    To find out whether you need to supply a security token, choose the Test button. If the connection attempt fails with an error which contains LOGIN_MUST_USE_SECURITY_TOKEN, you need to supply one.

    Salesforce.com emails the security token to the email address associated with your Salesforce.com user account. If you have not received a security token, you can regenerate it. Salesforce.com will then email the new security token to you. To regenerate your security token, log in to Salesforce.com and then choose Setup from the user menu. Search for "security token" in the Quick Find box. Click Reset Security Token in the Reset Security Token page. When you receive the token in your email client, copy it and then paste it into the Token field.

  5. Use the Test button to verify that you can successfully connect to Salesforce.com.
  6. Download the Easysoft JDBC-ODBC Bridge. (Registration required.)
  7. Install and license the Easysoft JDBC-ODBC Bridge on the machine where you created the data source.

    For installation instructions, see the Easysoft JDBC-ODBC Bridge documentation.

Using Salesforce SOQL from Java

  1. Copy EJOB.jar to a machine where you have a Java Development Kit installed.

    If the Easysoft JDBC-ODBC Bridge is already installed on this machine, skip this step.

    EJOB.jar is installed in the following location on the machine where you install the Easysoft JDBC-ODBC Bridge:

    <easysoft_install>\Jars

    The default location for <easysoft_install> is <drive>:\Program Files (x86)\Easysoft Limited\Easysoft JDBC-ODBC Bridge.

  2. Add this Java code to a file named ConnectToSalesforce.java:
    import java.sql.*;
    import java.util.Properties;
    
    public class ConnectToSalesforce {
    
       public static void main(String[] args) {
    
          // Replace the <mydsn> value with your 32-bit Salesforce DSN.
          // If the JDBC-ODBC Bridge Server (JOB) is not located on the same machine as
          // ejob.jar replace localhost with the remote host name or IP address.
          // The :logonuser attribute value is a Windows user on the machine where
          // the JOB Server is running.
          String connectionUrl = "jdbc:easysoft://localhost/<mydsn>" +
             ":logonuser=mywindowsuser:logonpassword=mywindowspassword";
    
          Driver driver = null;
          DriverPropertyInfo props[] = null;
          Connection con = null;
          Statement stmt = null;      
          ResultSet rs = null;
    
          try {
           // Register the Easysoft JDBC-ODBC Bridge client.
           Class.forName("easysoft.sql.jobDriver");
           driver = DriverManager.getDriver(connectionUrl);
    
           con = DriverManager.getConnection(connectionUrl);
    
           stmt = con.createStatement();
    
           // You need to edit this query
           rs = stmt.executeQuery("SELECT Account.Name, (SELECT Contact.LastName FROM Account.Contacts) FROM Account");
    
           System.out.print("Name LastName" + "\n");
           
           while (rs.next()) {
               String n = rs.getString("Name");
               String l = rs.getString("LastName");
               System.out.print(n + " " + l + "\n");
           }
    
           rs.close();
           rs = null;
           stmt.close();
           stmt = null;
           con.close(); // Return to connection pool
           con = null;  // Make sure we do not close it twice	 
    
          }
    
          // Handle any errors that may have occurred.
          catch (Exception e) {
              e.printStackTrace();
          }
          finally {
              if (rs != null) try { rs.close(); } catch(Exception e) {}
              if (con != null) try { con.close(); } catch(Exception e) {}
          }
       }
    }
    
  3. In a command window, add EJOB.jar to the Java class path. For example:
    set CLASSPATH="%CLASSPATH%;C:\Program Files (x86)\Easysoft Limited\Easysoft JDBC-ODBC Bridge\Jars\EJOB.jar"
  4. cd to the directory where ConnectToSalesforce.java is located, and compile and run the Java file. For example:
    "C:\Program Files\Java\jdk1.8.0_144\bin\javac.exe" ConnectToSalesforce.java
    java ConnectToSalesforce
    Name LastName
    GenePoint Ltd Frank
    United Oil & Gas, UK James
    United Oil & Gas, Singapore D'Cruz
    United Oil & Gas, Singapore Ripley