473,890 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get Databases Name in DB2

5 New Member
Hello, all I am a newbie in DB2 (and thescripts.com) =P

I want to ask:
How can I get (list) Databases name / aliases in DB2 using Java?

I am using windows XP
DB2 v8.1.7.445
JDK 1.4.1

thx for the answer... =)
Feb 4 '08 #1
7 33552
r035198x
13,262 MVP
Hello, all I am a newbie in DB2 (and thescripts.com) =P

I want to ask:
How can I get (list) Databases name / aliases in DB2 using Java?

I am using windows XP
DB2 v8.1.7.445
JDK 1.4.1

thx for the answer... =)
You do know how to connect to DB2 using JDBC right?
You could then try the
Expand|Select|Wrap|Line Numbers
  1. list database directory
command.
Feb 4 '08 #2
hosesurya
5 New Member
You do know how to connect to DB2 using JDBC right?
You could then try the
Expand|Select|Wrap|Line Numbers
  1. list database directory
command.
Hm...
I'd try that...
I can connect to DB2 using JDBC and I've tried that command...
But, the exception return this message:
"[IBM][JDBC Driver] CLI0637E QUERY cannot be found."

maybe you can give me further explanation or suggestion (with details)?
My goals is I want to list all DB2 databases that connect trough my JDBC.
i.e: I have DBs: UNIVERSITY1, UNIVERSITY2, UNIVERSITY3
and I want to get all the DBs name (UNIVERSITY1, UNIVERSITY2, and UNIVERSITY3) trough Java and stored in a variable.
So, I can displayed on Combobox all the DBs name.

As far as I know that command will works when we use it in DB2 Command Line Processor, but I think that command will not works when we used it like ordinary query.
Feb 5 '08 #3
docdiesel
297 Recognized Expert Contributor
As far as I know that command will works when we use it in DB2 Command Line Processor, but I think that command will not works when we used it like ordinary query.
That's right. The only commands accepted via JDBC are SQL statements. DB2 commands like Load, Runstats or "List db directory" will be rejected.

May I suggest a workaround: Add a table to DB UNIVERSITY1 and keep a list of your databases in there. With some statement like
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.   a.dbname
  3. from
  4.   meta.listofdatabases a
  5. order by
  6.   a.dbname  asc
you'll be able to offer a dropbox with the databases names easily.

Regards,

Bernd
Feb 5 '08 #4
r035198x
13,262 MVP
That's right. The only commands accepted via JDBC are SQL statements. DB2 commands like Load, Runstats or "List db directory" will be rejected.

May I suggest a workaround: Add a table to DB UNIVERSITY1 and keep a list of your databases in there. With some statement like
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.   a.dbname
  3. from
  4.   meta.listofdatabases a
  5. order by
  6.   a.dbname  asc
you'll be able to offer a dropbox with the databases names easily.

Regards,

Bernd
You're right, JDBC will take only SQL statements. Isn't there a database catalogue table already in DB2 like MySQL's information_sch ema?
Feb 5 '08 #5
hosesurya
5 New Member
Thanks for all replies i got... =)

May I suggest a workaround: Add a table to DB UNIVERSITY1 and keep a list of your databases in there. With some statement like
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.   a.dbname
  3. from
  4.   meta.listofdatabases a
  5. order by
  6.   a.dbname  asc
you'll be able to offer a dropbox with the databases names easily.
It's truly a good idea, and i very appreciate that... =)
But, I think in my cases that idea cannot be applied...
Because, i have some databases and anybody (who have access) can create a new database and alter any table (he or she wants) for some purposes.
So, i want to previews all the exist databases without need any extra works (like adding information into new table or update a file).
I'd found that in DB2 the information about databases is stored in Instances or nodes so, i'm still struggling to get this information trough Java...

You're right, JDBC will take only SQL statements. Isn't there a database catalogue table already in DB2 like MySQL's information_sch ema?
I still search for this too... I hope this database catalogue table is really exist...
If yes, it's very helpful for me to develop in more simplicity... =)
At least, i don't need to think hard =)

Thanks for the help and suggestions =)

Best Regards,
Hose Surya
Feb 6 '08 #6
docdiesel
297 Recognized Expert Contributor
You're right, JDBC will take only SQL statements. Isn't there a database catalogue table already in DB2 like MySQL's information_sch ema?
No, for there's no central database - at least in the instance and on LUW systems. The only way to discover the existing databases is through the DB2 administration server, but that one isn't talking JDBC.

Within the instances all databases are visible. I'd create a shell skript to discover them and write them into my central table, by crontab every 10 min within working hours; something like this (english Db2 version):

Expand|Select|Wrap|Line Numbers
  1. db2 connect to mydb
  2. db2 list database db | grep alias | awk '{print $4;}' | xargs -i echo insert into my.table\(dbnames \(\'{}\'\) | db2 -v
  3.  
Don't know if there's some central database on z/OS and i5 DB2 systems, but somehow I doubt it.

Regards,

Bernd
Feb 6 '08 #7
hosesurya
5 New Member
Thanks for all your concern... =)

But, with the help of my mentor Mr. Haris...
I know how to get a list of DB directories...
You can use the command:
db2cmd -c -w -i db2 list db directory | findstr /c:"Database alias" > DatabaseList.tx t
this command can export the database alias line to "DatabaseList.t xt" file.
But, maybe the exec() function cannot process the pipelining argument
"| findstr /c:"Database alias" > DatabaseList.tx t"
So, i choose to simplify the command to:
"db2cmd -c -w -i db2 list db directory"

Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.io.*;
  3.  
  4. public class Surya
  5. {
  6.     public static void main( String[] args )
  7.     {
  8.         String cmdStrBuff, DBAlias;
  9.         int n;
  10.         try
  11.         {
  12.             //Process command to command shell
  13.             Process cmdStrProc = Runtime.getRuntime().exec( "db2cmd -c -w -i db2 list db directory" );
  14.             //Get the result from command
  15.             DataInputStream cmdInpStream = new DataInputStream( cmdStrProc.getInputStream() );
  16.             try
  17.             {
  18.                 //Read the result line per line and store it in a String variable
  19.                 while( ( cmdStrBuff = cmdInpStream.readLine() ) != null )
  20.                 {
  21.                     /*
  22.                       Sometimes the result is just only a feedline
  23.                       So, we must get real information.
  24.  
  25.                       ex:  Database alias                       = UNIVERSITY1
  26.                             Database name                      = UNIV_1
  27.                             Node name                            = UNIVERSITY
  28.  
  29.                       So, we must search the information we need,
  30.                       I want to search Database alias.
  31.                       If variable n return more than 0, than we got the alias!
  32.                     */
  33.                     n = cmdStrBuff.indexOf( "alias" );
  34.                     if( n > 0 )
  35.                     {
  36.                         //Substring to get the DB alias name.
  37.                         DBAlias = cmdStrBuff.substring( 40 );
  38.                         //Show the DB alias name to the java console...
  39.                         System.out.println(DBAlias);
  40.                     }
  41.                 }
  42.             }
  43.             catch( IOException e )
  44.             {
  45.                 System.exit( 2 );
  46.             }
  47.         }
  48.         catch( IOException e1 )
  49.         {
  50.             System.err.println( e1 );
  51.             System.exit( 1 );
  52.         }
  53.  
  54.     }
  55. }
  56.  
  57.  
i so thanks to Bernd and r035198x for the replies...
and i hope my answers can complete the Bernd's.
Bernd's answer is maybe suitable for *nix platform...
and mine for Windows platform...

Best Regards,
Hose Surya
Feb 13 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1850
by: Hans Forbrich | last post by:
Section 1. Ballot: ------------------- 1.YES NO: I agree that there should be a periodic post describing the newsgroup charter and providing a FAQ on newsgroup usage. 2.MONTHLY BI-WEEKLY WEEKLY: The post should be transmitted with this frequency.
0
1830
by: Hans Forbrich | last post by:
Section 1. Ballot: ------------------- 1.YES NO: I agree that there should be a periodic post describing the newsgroup charter and providing a FAQ on newsgroup usage. 2.MONTHLY BI-WEEKLY WEEKLY: The post should be transmitted with this frequency.
0
1652
by: Cara | last post by:
LAST CALL FOR VOTES (of 2) unmoderated group comp.databases.etl Newsgroups line: comp.databases.etl Extraction, transformation, loading issues. Votes must be received by 23:59:59 UTC, 12 Dec 2003. This vote is being conducted by a neutral third party. Questions about the proposed group should be directed to the proponent.
2
4870
by: CxG | last post by:
We need to copy about 25 databases whenever we get a new software delivery. Currently, the process is to manually use the DTS database copy wizard. Doing some research, I created a DTS package to copy databases. But this was not going to work since I have to hard code the database names in the package. Back to RTFMing, I discovered the Dynamic Properties Task. So now I setup a .bat file that calls dtsrun and via global variables...
1
4214
by: Prakash RudraRaju | last post by:
Hi, I have recently migrated MSAccess Tables (nearly 120) to MySQL. To test successful migration I want to compare all tables. I have linked MySQL tables through ODBC connection. I want to create a report that compares all records between all tables in both databases. I am looking for a report with differences in number of records and differences in fields if 2 records are different. Probabaly it can be acheived through query, but I...
3
978
by: Sam | last post by:
Hi, In my vb.net form I have a textbox in which the user can enter a server name, and then the combobox below is filled in dynamically with all the databases available on this server. How can I retrieve these databases name ? Not sure if that is the correct forum to post to though... Thx
2
1544
by: Jonathan Villa | last post by:
Is it possible to create a database to be owned by a user that only exists to postgresql or do they have to be shell accounts as well. $ createdb --owner=testcon testdb but the owner ends up being postgres I'm planning to create a different database for each different project I have. I want these users to be only used to connect to the db, not have a shell account. So far, my only experience has been on a closed
0
1153
by: hosesurya | last post by:
Hello, all I am a newbie in Java + DB2 (and thescripts.com) =P I want to ask: How can I get (list) Databases name / aliases in DB2 using Java? I am using windows XP DB2 v8.1.7.445 JDK 1.4.1 thx for the answer... =)
0
1532
by: GetgoodV | last post by:
Hi all, I wonder if anybody can help with a problem I have accessing (caaloging) databases on my network (drive V) I have installed DB2 V8 on Windows XP: - ========================================================= About DB2 Administration Tools Environment ========================================================= DB2 administration tools level: Product identifier SQL08020 Level identifier 03010106
0
9970
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11207
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10794
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10443
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8000
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7153
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5830
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4652
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3259
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.