on 09-03-2008 11:25 AM
Hello,
I have a Java Application and would like to connect to a SAP BO Database using JCO and DI API.
I want a simple java example that just connects to the BO Database and returns an item name or value or a recordset from the database.
Since i dont have the names of what kind of fields, items , tables exist in the SAP BO Demo database i need a basic example to make sure that i can connect to the database and retrieve data from the DB.
Any help in this regard would be appreciated...
Amit
Dear Amit Hingher,
Please refer following sample about how to use Recordset in JCO, hope it is helpful for yoru.
public class RecordsetQuery {
public static void main(String[] args) throws Exception {
doQuery();
}
protected static void doQuery() throws Exception {
SBOErrorMessage errMsg = null;
ICompany com = null;
IRecordset RecSet = null;
int i;
String FldName;
String FldVal;
Object index;
String sQueryItemList1 = "Select TargetType,TrgetEntry,LineStatus,OpenQty,OpenCreQty From RDR1";
ConnectSAP conn = new ConnectSAP();
int rc = conn.conn();
if (rc == 0) {
try {
RecSet =
SBOCOMUtil.runRecordsetQuery(
conn.company,
sQueryItemList1);
int Count = RecSet.getFields().getCount().intValue();
while (RecSet.isEoF().equals(new Boolean(false))) {
for (i = 0; i < Count; i++) {
index = new Integer(i);
FldName = RecSet.getFields().item(index).getName();
FldVal = String.valueOf(RecSet.getFields().item(index).getValue());
//'Here you can manipulate the data as you want
System.out.println(i + "FldName = " + FldName);
System.out.println(i + "FldVal = " + FldVal);
}
//Move to the next record
RecSet.moveNext();
}
} catch (Exception ex) {
errMsg = conn.company.getLastError();
ex.printStackTrace();
}
}
RecSet = null;
com = null;
}
}
Best Regards
Jane Jing
SAP Business One Forums team
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Jang,
thanks once again. Your reply was really helpful.
What i would really like to know is are there good amount of people out there in the community who have used Java with SAP Business One ?
I mean i can find many resources pertaining Java <-> SAP R/3 but there arnt many people who have really worked with Java <-> SAP Business One (atleast i did not find many except you, who could help me in this regard).
Could you point us out some websites or books except the "DI API Help Kit" where we can get some basics in this regard ?
Looking forward to one more helpful answer ..
Regards,
Amit
Edited by: Amit Hingher on Sep 5, 2008 11:11 AM
Dear Amit Hingher,
The B1 JCO is a java wrapper for DI API so basically you could refer to DI help for all objects, methods and properties.
Here the jave sample for connection function:
package test;
import com.sap.smb.sbo.api.*;
public class ConnectSAP {
// company interface
public ICompany company;
private SBOErrorMessage errMsg = null;
public static void main(String[] args) {
ConnectSAP company = new ConnectSAP();
company.conn();
}
//method make connection andinitialize company instance
public int conn() {
int rc = 0;
try {
company = SBOCOMUtil.newCompany();
company.setServer("(local)");
company.setCompanyDB("test");
company.setUserName("manager");
company.setPassword("manager");
company.setDbServerType(...);
company.setUseTrusted(new Boolean(false));
company.setLanguage(SBOCOMConstants.BoSuppLangs_ln_English);
company.setDbUserName("Sa");
company.setDbPassword("123");
company.setAddonIdentifier("...");
company.setLicenseServer("...");
rc = company.connect();
if (rc == 0) {
System.out.println("Connected!");
} else {
errMsg = company.getLastError();
System.out.println(
"I cannot connect to database server: "
+ errMsg.getErrorMessage()
+ " "
+ errMsg.getErrorCode());
}
} catch (Exception e) {
e.printStackTrace();
return -1;
}
return rc;
}
public void freeConnection(){
company.disconnect();
}
}
Best Regards
Jane Jing
SAP Business One Forums team
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Jang,
thanks for the example.
We do have the help file for DI API but all examples given there are in Visual Basic or in .NET and as you must be knowing there are some changes in method definitions and instantiation of classes when using VB / .NET. We cannot just make a 1 to 1 translation from VB to Java.
Here is our question once again :
Can you give us an example where we fetch recordsets from the database using the demo database provided with the SAP BO installation. Since we have very minimum or no information on the name of tables, field names and item names we need a simple example to make sure that we are able to fetch records from the database.
It would be great if you or someone from the community can help us in this regard,
Regards,
Amit
Hi Nawaz,
You can take a look to the help file shipped with sap because the names of the methods exposed by the sdk are the same even in java. For example, if you take a look to the contacts object in the help file you can find this:
ActivityType - Sets or returns the type of the activity.
Field name: CntctType.
This is a foreign key to the ActivityTypes object.
In Java you can find the methods getActivityType and setActivityType that works as described in the help.
Anyway I will try to help you.
bye
User | Count |
---|---|
103 | |
12 | |
11 | |
6 | |
5 | |
4 | |
3 | |
3 | |
3 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.