‎2006 Jun 26 4:30 PM
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.
‎2006 Jun 26 4:39 PM
Hi,
You need JCO(Java Connector)
Check this sample code.
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
‎2006 Jun 26 4:37 PM
Hi,
Nothing much you need to do. Just have to call the RFC <b>RFC_READ_TABLE</b> with the table parameter
Regards
vijay
‎2006 Jun 26 4:39 PM
Hi,
You need JCO(Java Connector)
Check this sample code.
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