Jeff Douglas -- 一个SF开发者的Blog,记录一下
http://blog.jeffdouglas.com/2010/05/17/java-command-line-app-using-the-salesforce-wsc/
The Force.com Web Service Connector (WSC) is a high performance web services stack that is much easier to implement than the “tried and true” Force.com Web Services API. Here’s a quick command line app you can use as a starter application. This class simply creates a new Account and then queries for the 5 newest Accounts by created date.
To get started, download wsc-18.jar from the WSC project’s download page to your desktop (any location will do). Now log into your Developer org and download the Partner WSDL (Setup -> App Setup -> Develop -> API) to your desktop as “partner.wsdl”. Now we’ll need to generate the stub code from the Partner WSDL. Make sure you have Java 1.6 installed and open a command prompt. Now run wsdlc on the Partner WSDL you just downloaded (detailed instruction are here):
java -classpath wsc-18.jar com.sforce.ws.tools.wsdlc partner.wsdl partner.jar
package com.jeffdouglas; import com.sforce.soap.partner.*;import com.sforce.soap.partner.sobject.*;import com.sforce.ws.*; public class Main { public static void main(String[] args) { ConnectorConfig config = new ConnectorConfig(); config.setUsername("YOUR-USERNAME"); config.setPassword("YOUR-PASSWORD-AND-SECURITYTOKEN"); PartnerConnection connection = null; try { // create a connection object with the credentials connection = Connector.newConnection(config); // create a new account System.out.println("Creating a new Account..."); SObject account = new SObject(); account.setType("Account"); account.setField("Name", "ACME Account 1"); SaveResult[] results = connection.create(new SObject[] { account }); System.out.println("Created Account: " + results[0].getId()); // query for the 5 newest accounts System.out.println("Querying for the 5 newest Accounts..."); QueryResult queryResults = connection.query("SELECT Id, Name from Account " + "ORDER BY CreatedDate DESC LIMIT 5"); if (queryResults.getSize() > 0) { for (SObject s: queryResults.getRecords()) { System.out.println("Id: " + s.getField("Id") + " - Name: "+s.getField("Name")); } } } catch (ConnectionException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }