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.
- Install and license the Easysoft JDBC-ODBC Bridge on the machine where your Java is located.
- Install and license the Salesforce ODBC driver on the machine where the JDBC-ODBC Bridge server is installed.
- Start ODBC Data Source Administrator (which you use to create a data source). To do this, in the Windows Run dialog box, enter:
%windir%\syswow64\odbcad32.exe
- In ODBC Data Source Administrator, choose the System DSN tab, and then choose Add.
- In the Create New Data Source dialog box, choose Easysoft Salesforce SOQL ODBC driver, and then choose Finish.
- Complete the DSN Setup dialog box:
Setting Value DSN Salesforce User Name The name of your Salesforce user. For example, myuser@mydomain.com
.Password The password for your Salesforce user. Token The security token for your Salesforce 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 emails the security token to the email address associated with your Salesforce user account. If you have not received a security token, you can regenerate it. Salesforce will then email the new security token to you. To regenerate your security token, log in to Salesforce and then choose Setup from the user menu. Search for "security token" in the Quick Find box. Choose 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.
- Use the Test button to verify that you can successfully connect to Salesforce.
Using Salesforce SOQL from Java
- 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
isdrive:\Program Files (x86)\Easysoft Limited\Easysoft JDBC-ODBC Bridge
. - 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 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 JDBC-ODBC Bridge 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) {} } } }
- In a Command Prompt window, add
EJOB.jar
to the JavaCLASSPATH
. For example:set CLASSPATH="%CLASSPATH%;C:\Program Files (x86)\Easysoft Limited\Easysoft JDBC-ODBC Bridge\Jars\EJOB.jar"
cd
to the directory whereConnectToSalesforce.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