This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JDBC and the fly likes How to access schema  for db2  through java code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to access schema  for db2  through java code" Watch "How to access schema  for db2  through java code" New topic
Author

How to access schema for db2 through java code

shri Sonparote
Ranch Hand

Joined: May 07, 2007
Posts: 30
Hi
Is there any way to access schema from java code .
Actually i am trying to make connection to db2 .
I want any generalized way.

Thanks
Shrikant
Les Hayden
Greenhorn

Joined: Jun 19, 2002
Posts: 28
Could you explain more what you mean by "access schema?" Do you want to find the schema name? Do you want to access the data in a particular schema?

Regards,
Les


Les Hayden
shri Sonparote
Ranch Hand

Joined: May 07, 2007
Posts: 30
Hi
I have created all my databse inABC schema.

I made connection to db2 databse using code .


but when try to execute any query using statement .It is refering to table or procs that are in my default schema.
To solve this problem I have to prepend ABC. when I am executing any table or procs from my code.
Other wise using statement object i have to execute command
set current schema =ABC.
But this both the two approch are not useful for me .

I want any approch such that it will refer to ABC schema.


waiting for reply
Shrikant
Les Hayden
Greenhorn

Joined: Jun 19, 2002
Posts: 28
I have not tried to use that feature of JDBC, but here is my opinion:

In DB2 schema is also your ID in the database. I would doubt that DB2 will let you sign on and then change the schema. Changing the schema before signing on, would serve no purpose.

I typically use the schema.objectname form of referencing database objects in DB2. So if I were doing a select from table XYZ in your example I would use the following SQL:

SELECT col1, col2
FROM ABC.XYZ;

You can also execute a command in DB2 to access another schema from your schema without using the schema prefix, but if there are objects with the same name in your schema, you will have to use the prefix to resolve the ambiguity.

The other alternative would be to login directly to the ABC schema. However, if that is a production schema, many companies restrict access to the schema.

Hope this helps,
Les
Brian Mulholland
Ranch Hand

Joined: Mar 12, 2009
Posts: 61
Actually, DB2 does allow you to change the default. Indeed, our application does it automatically upon every connection. Just issue:



as a statement to the DB as you would any SQL statement.
 
wood burning stoves
 
subject: How to access schema for db2 through java code