Hello Account (Java without wsc)

Java で Hello Account。

$ java -classpath ./:/usr/local/axis-1_4/lib/axis-ant.jar:/usr/local/axis-1_4/lib/commons-discovery-0.2.jar:/usr/local/axis-1_4/lib/jaxrpc.jar:/usr/local/axis-1_4/lib/wsdl4j-1.5.1.jar:/usr/local/axis-1_4/lib/axis.jar:/usr/local/axis-1_4/lib/commons-logging-1.0.4.jar:/usr/local/axis-1_4/lib/log4j-1.2.8.jar:/usr/local/axis-1_4/lib/saaj.jar org.apache.axis.wsdl.WSDL2Java partner.wsdl

$ javac -classpath ./:/usr/local/axis-1_4/lib/axis-ant.jar:/usr/local/axis-1_4/lib/commons-discovery-0.2.jar:/usr/local/axis-1_4/lib/jaxrpc.jar:/usr/local/axis-1_4/lib/wsdl4j-1.5.1.jar:/usr/local/axis-1_4/lib/axis.jar:/usr/local/axis-1_4/lib/commons-logging-1.0.4.jar:/usr/local/axis-1_4/lib/log4j-1.2.8.jar:/usr/local/axis-1_4/lib/saaj.jar com/sforce/soap/partner/*.java

$ find com/ | grep "class$" | xargs jar -cvf partner.jar

$ javac -classpath ./:/usr/local/axis-1_4/lib/axis-ant.jar:/usr/local/axis-1_4/lib/commons-discovery-0.2.jar:/usr/local/axis-1_4/lib/jaxrpc.jar:/usr/local/axis-1_4/lib/wsdl4j-1.5.1.jar:/usr/local/axis-1_4/lib/axis.jar:/usr/local/axis-1_4/lib/commons-logging-1.0.4.jar:/usr/local/axis-1_4/lib/log4j-1.2.8.jar:/usr/local/axis-1_4/lib/saaj.jar HelloAccount.java

$ java -classpath ./:/usr/local/axis-1_4/lib/axis-ant.jar:/usr/local/axis-1_4/lib/commons-discovery-0.2.jar:/usr/local/axis-1_4/lib/jaxrpc.jar:/usr/local/axis-1_4/lib/wsdl4j-1.5.1.jar:/usr/local/axis-1_4/lib/axis.jar:/usr/local/axis-1_4/lib/commons-logging-1.0.4.jar:/usr/local/axis-1_4/lib/log4j-1.2.8.jar:/usr/local/axis-1_4/lib/saaj.jar HelloAccount
import com.sforce.soap.partner.LoginResult;
import com.sforce.soap.partner.QueryResult;
import com.sforce.soap.partner.SessionHeader;
import com.sforce.soap.partner.SforceServiceLocator;
import com.sforce.soap.partner.SoapBindingStub;
import com.sforce.soap.partner.sobject.SObject;

public class HelloAccount {
  public static void main(String[] args) {
    try {
      SoapBindingStub binding = (SoapBindingStub) new SforceServiceLocator().getSoap();
      LoginResult loginResult = binding.login(<user name>, <password>);
      
      SessionHeader sh = new SessionHeader();
      sh.setSessionId(loginResult.getSessionId());      
      binding._setProperty(SoapBindingStub.ENDPOINT_ADDRESS_PROPERTY, loginResult.getServerUrl());
      binding.setHeader(new SforceServiceLocator().getServiceName().getNamespaceURI(), "SessionHeader", sh);
  
      QueryResult queryResult = binding.query("SELECT Id, Name FROM Account");
      for (SObject sobj : queryResult.getRecords()) {
        System.out.println(sobj.getId() + ", " + sobj.get_any()[1].getValue());
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Hello Account (Java with WSC)

Java (WSC) で Hello Account。

$ wget https://sfdc-wsc.googlecode.com/files/wsc-23.jar
$ java -classpath wsc-23.jar com.sforce.ws.tools.wsdlc partner.wsdl partner-wsc.jar
$ javac -classpath ./:wsc-23.jar:partner-wsc.jar HelloAccount.java
$ java -classpath ./:wsc-23.jar:partner-wsc.jar HelloAccount
import com.sforce.soap.partner.*;
import com.sforce.soap.partner.sobject.SObject;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;
import java.io.FileNotFoundException;

public class HelloAccount {
  private PartnerConnection connection;
  
  private String authEndpoint = "https://test.salesforce.com/services/Soap/u/30.0";
  private String userName = <user name>;
  private String password = <password>;
  private String query = "SELECT Id, Name FROM Account";
  
  public static void main(String[] args) throws FileNotFoundException {
    HelloAccount helloAcct = new HelloAccount();
    if (helloAcct.login()) {
      helloAcct.getAccounts();
    }
  }
  
  private boolean login() throws FileNotFoundException {
    boolean success = false;
    ConnectorConfig config = new ConnectorConfig();
    config.setUsername(userName);
    config.setPassword(password);
    config.setAuthEndpoint(authEndpoint);
    config.setTraceFile("trace.log");
    config.setTraceMessage(true);
    config.setPrettyPrintXml(true);
    try {
      connection = Connector.newConnection(config);
      success = true;
    } catch (ConnectionException e) {
      e.printStackTrace();
    }
    return success;
  }
  
  private void getAccounts() {
    QueryResult queryResult;
    try {
      queryResult = connection.query(query);
      for (SObject sobj : queryResult.getRecords()) {
        System.out.println(sobj.getId() + ", " + sobj.getField("Name"));
      }
    } catch (ConnectionException e) {
      e.printStackTrace();
    }
  }
}