on 07-26-2006 10:55 AM
Hello together,
following problem. I use a CMP entity-EJB for storing some information on the javadatabase from a j2ee-application. A problem which I encountered was, that the bean throws an exception, if I want to save a string, which is to long for the field. Is it possible to check the field length of the table-field out of my java-app?
Thanks in advance
Thorsten Boersch
Hi Thorsten,
Are you using any session beans as facade for CMP beans? You can perform such validation is session bean.
Best regards, Maksim Rashchynski.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes, I am using a session-bean, but I think the biggest problem will be to get the length of the field out of the dictionary-dc into my application-dc, there the session-bean is. Or can I find this length somethere else? It is no problem the check the length of a string hardcoded, but I want to get the length of the field from somethere and check the string-length against this.
Best regards
Thorsten
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Thorsten,
import com.sap.dictionary.runtime.IBroker;
import com.sap.dictionary.runtime.ISimpleType;
import com.sap.dictionary.runtime.DdDictionaryPool;
import com.sap.dictionary.runtime.DdException;
....
ClassLoader cl = this.getClass().getClassLoader();
Locale l = Locale.getDefault();
IBroker broker = DdDictionaryPool.getInstance().createBroker( cl, l);
((ISimpleType)broker.getDataType("com.sap.sdn.dict.TypeName")).getMaxLength();
...
Best regards, Maksim Rashchynski.
Hi Maksim,
I tried to test the code, but it doesn't work
DdDictionaryPool pool = DdDictionaryPool.getInstance();
broker = DdDictionaryPool.getInstance().createBroker(cl, l);
IDataType type = broker.getDataType("com.sap.sdn.dict.TypeName");
Getting the instances gives me a pool-object back, without any entries. Trying to create the broker seems like throwing a null pointer exception, but I am not sure, because I come never back to the method. And even if this works...what is the meaning of "com.sap.sdn.dict.TypeName"?
Hi Thorsten,
what do you mean by "javadatabase"? Is it an oracle dbms or max db?
If your db is jdbc compliant, you can use the following:
Connection connection = .... // get connection from i.e. jndi
DatabaseMetaData md = connection.getMetaData();
int size = 0;
ResultSet rsCol = md.getColumns(
"catalog", // a catalog name; "" retrieves those without a catalog; null means drop catalog name from the selection criteria
"schema", // a schema name pattern; "" retrieves those without a schema
"table", // a table name
"column"); // column name
if (rsCol.next()){
size = rsCol.getInt("COLUMN_SIZE");
}
Hi Sebastian,
I think that I should work with both database. I am working with the NDWI. So I habe on my local development-station it is a maxdb. The other systems on which I will deploy my project(develop, test and productive) are oracle. The whole connection to the java-database is implemented by container managed entity beans which are be mapped to the tables of one dictionary-project. This entity-beans are controlled by a stateless session bean, which will be used in my application for saving the data.
So is maybe not the best solution to get another connection to the database? I do not realy know, what the best way is in this situation.
Best regards
Thorsten
Hi Sebastian,
I am not so familiar with this connection-stuff. At the Moment I build a connection to the data-source-alias which I think, hat it should be my database. This is defined in the ejb-jar.xml and the ejb-j2ee-engine.xml as a resource reference. So I tried the following code:
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/DATA-SOURCE-ALIAS");
con = ds.getConnection();
md = con.getMetaData();
int size = 0;
ResultSet set = md.getTableTypes();
ResultSet rsCol = md.getColumns(
null, // a catalog name; "" retrieves those without a catalog; null means drop catalog name from the selection criteria
null, // a schema name pattern; "" retrieves those without a schema
"TABLE_NAME", // a table name
"BPNUMBER"); // column name
Connection and metadata seems to work, but if I try to get the resultsets the programm jumps out Any ideas?
Best regards
Thorsten
Hi Thorsten,
it seems that your resultset is empty. Please check the parameters of "getColumns".
The table and column name information is case sensitive. Also make sure that your table really does not belongs to a schema. If there is no schema use "" instead of
null
. I just tried this on a oracle db and it works.
Regards
Sebastian
Hello Sebastian,
I have done as you told me. I can't find any schema-name, so I set "" instead of null. But with the same result. I have checked the tablename and the colum name, but they are both written in capital letters.
I have done a simple:
java.sql.Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from TABLE_NAME");
before the other resultset.
With this I get the columnnames and I think the position in the table back. So the connection seems to work realy.
At the moment I am a little bit confused.
So I will look on it on monday. If you have any other suggestions?
Best regards and a nice we
Thorsten
Hi Thorsten,
Ever tried to debug to find out what happens really in the code Maksim provided?
You should replace "com.sap.sdn.dict.TypeName" with the name of your type. Might need to do a little search for the correct syntax (I think there is some prefix you have to use). So I guess the NullPointerEx showed because of course no type with the name "com.sap.sdn.dict.TypeName" could be found.
For the JDBC metadata part you also tried:
I guess you store your data in the system database. The system database uses OpenSQL which comes with some restrictions with regards to datatypes and JDBC API. This might be the reason why retrieving the metadata from your datasource connection does not work.
Regards
Markus
Hello together,
@ Sebastian: I could not use this syntax, because I developing on the MaxDB. I didn't found any owner-entry or schema-names of the table.
@ Markus: Yes, I tried to debug it. The programms jumps out at: broker = DdDictionaryPool.getInstance().createBroker(cl, l);. So at this point I have not asked for any TypeName. I use this construct in the sessionbean.
Yes, I use the system-database with openSql.
It seems, that this isn't a easy topic.
Is it a problem to use a CLOB-Field for informations, whose length I don't know?
Best regards
Thorsten
Hi Maksim,
after debugging the second time, I found the exception.
java.lang.NoClassDefFoundError: com/sap/typeservices/ITextServices
at com.sap.dictionary.runtime.DdBroker.<init>(DdBroker.java:48)
at com.sap.dictionary.runtime.DdDictionaryPool.createBroker(DdDictionaryPool.java:124)
at
Have I to include another class, or something like this?
Best regards
Thorsten
Hi Maksim,
so I found out, that this exception was raised because of a missing jar-file. I corrected this.
Now I can get the field-length of some simple-types like com.sap.dictionary.string. But I don't understand how to get the length of the fields in my database. Because they can be of the type, but I can enter a length at creationtime for the field and this is the information I need later in my EJB.
Maybe I overlook something?
Best regards
Thorsten Börsch
User | Count |
---|---|
88 | |
10 | |
10 | |
9 | |
7 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.