Connect Your Java Application to SAP B1 HANA and Post Journal Entry using JCO

After several hours looking for a solution of how to connect a java program to SAP Business One HANA, I finally found that SAP B1 HANA provides a wrapper for B1DI library, called JCO (Java Connector). This is an example of how do I connect our java program using JCO to SAP B1 HANA and then post a journal entry document. Hope, this several lines of codes can help you connect your java application to SAP B1 HANA.

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

package hanapostingje;

 

import com.sap.smb.sbo.api.*;

 

/**

 *

 * @author Alfa

 */

public class Application {

// company interface

    public ICompany company;

 

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

// TODO code application logic here

        Application application = new Application();

        application.connect();

    }

 

    /**

     * Create connection and initialize company instance

     *     

* @return 0 if success, -1 if fail

     */

    public int connect() {

        int rc = 0;

        try {

            System.out.println("Connecting to SAP ...");

            company = SBOCOMUtil.newCompany();

            company.setServer("hanaserver:30015");

            company.setCompanyDB("SBODEMOAU");

            company.setUserName("manager");

            company.setPassword("1234");

            company.setDbServerType(SBOCOMConstants.BoDataServerTypes_dst_HANADB);

            company.setUseTrusted(false);

            company.setLanguage(SBOCOMConstants.BoSuppLangs_ln_English);

            company.setDbUserName("SYSTEM");

            company.setDbPassword("MyPassword");

            company.setLicenseServer("hanaserver:40000");

            rc = company.connect();

            if (rc == 0) {

                System.out.println("Connected!");

                this.postJournalEntry();

                this.freeConnection();

            } else {

                SBOErrorMessage errMsg = company.getLastError();

                System.out.println(

                        "Cannot connect to database server: "

                        + errMsg.getErrorMessage()

                        + " "

                        + errMsg.getErrorCode());

            }

        } catch (Exception e) {

            e.printStackTrace();

            return -1;

        }

        return rc;

    }

 

    /**

     * Disconnect application from database and SAP

     */

    public void freeConnection() {

        company.disconnect();

        System.out.println("Application disconnected successfully");

    }

 

    /**

     * Post journal entry to SAP

     */

    public void postJournalEntry() {

        try {

            System.out.println("Posting journal entry ...");

            int retVal = 0;

            IJournalEntries je = SBOCOMUtil.newJournalEntries(company);

            IJournalEntries_Lines jeLines = je.getLines();

            jeLines.setAccountCode("110001");

            jeLines.setDebit(100.0);

            jeLines.add();

            jeLines.setAccountCode("110002");

            jeLines.setCredit(100.0);

            jeLines.add();

            retVal = je.add();

            if (retVal != 0) {

                System.out.println("Posting journal entry failed");

                throw new Exception(company.getLastErrorCode() + " " + company.getLastErrorDescription());

            } else {

                System.out.println("Journal entry posted successfully");

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

To utilize the wrapper, you need to include sboapi.jar and sborwrapper.jar into your project library—I was using Netbeans to manage my project. Both of them are located in “C:\Program Files (x86)\SAP\SAP Business One DI API\JCO\LIB” and were installed in your client PC when you installed SAP B1 HANA client.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s