Preface – This post is part of “Salesforce Integration with SOAP API” series.

In my previous post, I have explained how can you configure your Eclipse IDE so that you can start writing you own Java program for connecting to Salesforce using SOAP API and also the Java program to check the connectivity. If you are not familiar, I would request you to go through my previous posts. The links are given below –

From this post, I will try to become more technical and concentrate on CRUD (Create, Read, Update and Delete) operation using Java Program and Salesforce’s SOAP API methods.
This is the first post where I will explain how you can query Salesforce records using Java program and SOAP API. The below program will fetch a specific Account record and display the information in console –
Note: I have written a helper class ConnectionHelper.java which I am using to connect to Salesforce using Enterprise WSDL. To know more about that, please refer my previous post @ 
Below is the java program which will fetch details of the account named “Burlington Textiles Corp of America”. 
FindAccount.java
import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.soap.enterprise.QueryResult;
import com.sforce.soap.enterprise.sobject.Account;
import com.sforce.ws.ConnectionException;

public class FindAccount {
private EnterpriseConnection connection;

private FindAccount() {
setConnection(ConnectionHelper.doConnect());
}

public static void main(String[] args) {
FindAccount findAccount = new FindAccount();
findAccount.doFind();
}

private void doFind(){
QueryResult result = new QueryResult();
try{
result = getConnection().query("SELECT Id,Name,Phone,Rating FROM Account WHERE Name = 'Burlington Textiles Corp of America'");
if(result.getSize() > 0){
System.out.println("Found " + result.getSize() + " records.");
Account anAccount = (Account)result.getRecords()[0];
System.out.println("ID: " + anAccount.getId() + " Name: " + anAccount.getName() + " Phone: " + anAccount.getPhone() + " Rating: " + anAccount.getRating());
}
}catch(ConnectionException e){
e.printStackTrace();
}
}

public EnterpriseConnection getConnection() {
return connection;
}

public void setConnection(EnterpriseConnection connection) {
this.connection = connection;
}

}

Explanation: 
Below is what Salesforce’s developer guide tells us regarding query() method:

I hope this post will help you to understand how you can fetch a single record using query() method. In my next post, I will explain how you can handle huge amount of data while doing the query. Till then good bye and keep learning.

Any feedback, please let me know. Thanks.