cancel
Showing results for 
Search instead for 
Did you mean: 

Help on accessing tables of SAP from the Java Application

former_member55105
Participant
0 Kudos

Hi All

I want to access some tables available in SAP through my Java program. Do I have to write any code for database connectivity or straightaway I can write SQL statements in my Java code?

Can anyone please suggest the sample code for this?

Thanks in Advance,

Vijay.

Accepted Solutions (0)

Answers (4)

Answers (4)

Former Member
0 Kudos
Former Member
0 Kudos

Hi Vijay,

You would have to use JCO(Java Connector).

Using this you would be able to access the remotely enabeled function modules in SAP.

In this function modules, you can write your code of fetching data.

I have some excellent document on this which shall be useful to you.

Let me know your email address and i shall send it to you.

Regards,

Tanveer.

<b>Please mark helpful answers</b>

former_member55105
Participant
0 Kudos

Hi Tanveer

I dont have any problem with the JCO connectivity. I have to access the tables available in SAP R/3 system and I have to run queries in my java code. Plz do the needful.

My mail id is pvijaykumar@graffiti.net.

Your early response is highly appreciable.

Thanks in Advance,

Vijay.

former_member182372
Active Contributor
0 Kudos

Hi Vijay,

Check this http://sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/ep/portal-content/simpl... queries of sap tables from java

People saying that it is pretty usefull document.

Best regards, Maksim Rashchynski.

Former Member
0 Kudos

Tanveer,

Can you please send me the doc.

Thanks.

former_member188685
Active Contributor
0 Kudos

the above one is with out connection pool, check this with connection pool.

import com.sap.mw.jco.*;

public class JcoTest {
 private static JCO.Client theConnection;
 private static IRepository theRepository;
    private static final String POOL_NAME = "myPool";
      
 public static void main(String[] args) {
  
    JCO.Pool connPool = JCO.getClientPoolManager().getPool(POOL_NAME);
    if (connPool == null) {
      JCO.addClientPool(POOL_NAME,
                        5,      //number of connections in the pool
                        "client",
                        "username",
                        "paswword",
                        "EN",
                        "hostname",
                        "00");
    }  
    
        theConnection = JCO.getClient(POOL_NAME);
  retrieveRepository();  
  try {
  JCO.Function function = getFunction("RFC_READ_TABLE");
  JCO.ParameterList listParams = function.getImportParameterList();
  
  listParams.setValue("BSAUTHORS", "QUERY_TABLE");
  
  theConnection.execute(function);
  
  JCO.Table tableList = function.getTableParameterList().getTable("DATA");
  
  if (tableList.getNumRows() > 0) {
   do {
    for (JCO.FieldIterator fI = tableList.fields();
      fI.hasMoreElements();)
     {
      JCO.Field tabField = fI.nextField();
      System.out.println(tabField.getName()
           + ":t" +
           tabField.getString());
     }
     System.out.println("n");
   }
   while (tableList.nextRow() == true);
  }
  }
  catch (Exception ex) {
   ex.printStackTrace();
  }
  
  JCO.releaseClient(theConnection);
  
 }
 
 private static void retrieveRepository() {
  try {
   theRepository = new JCO.Repository("saprep", theConnection);
  }
  catch (Exception ex)
  {
   System.out.println("failed to retrieve repository");
  }
 }
  public static JCO.Function getFunction(String name) {
    try {
         return theRepository.getFunctionTemplate(name.toUpperCase()).getFunction();
    }
    catch (Exception ex) {
     ex.printStackTrace();
    }
      return null;
    }  
}

Regards

vijay

former_member188685
Active Contributor
0 Kudos

Hi,

You need JCO(Java Connector)

Check this sample code.you have to call the RFC RFC_READ_TABLE

import com.sap.mw.jco.*;

public class JcoTest {
 private static JCO.Client theConnection;
 private static IRepository theRepository;
  
 public static void main(String[] args) {
  createConnection();
  retrieveRepository();  
  try {
  JCO.Function function = getFunction("RFC_READ_TABLE");
  JCO.ParameterList listParams = function.getImportParameterList();
  
  listParams.setValue("BSAUTHORS", "QUERY_TABLE");
  
  theConnection.execute(function);
  
  JCO.Table tableList = function.getTableParameterList().getTable("DATA");
  
  if (tableList.getNumRows() > 0) {
   do {
    for (JCO.FieldIterator fI = tableList.fields();
      fI.hasMoreElements();)
     {
      JCO.Field tabField = fI.nextField();
      System.out.println(tabField.getName()
           + ":t" +
           tabField.getString());
     }
     System.out.println("n");
   }
   while (tableList.nextRow() == true);
  }
  }
  catch (Exception ex) {
   ex.printStackTrace();
  }
  
  
  
 }
 
 private static void createConnection() {
  try {
   theConnection = JCO.createClient("000", "DDIC", "minisap", "en", "sincgo", "00");
   theConnection.connect(); 
  }
  catch (Exception ex) {
   System.out.println("Failed to connect to SAP system");
  }
 }
 private static void retrieveRepository() {
  try {
   theRepository = new JCO.Repository("saprep", theConnection);
  }
  catch (Exception ex)
  {
   System.out.println("failed to retrieve repository");
  }
 }
  public static JCO.Function getFunction(String name) {
    try {
         return theRepository.getFunctionTemplate(name.toUpperCase()).getFunction();
    }
    catch (Exception ex) {
     ex.printStackTrace();
    }
      return null;
    }  
}

Regards

vijay

Former Member
0 Kudos

This message was moderated.