1

I am trying to find out a SQL query through which i can get to know if i am connected to database or not. I will be using JAVA program for executing this query. I am in search of a generic query that when executed from my java program can give me a result if i am connected to database. I am not aware of the database structure(tables in database), i just know database name, to which i am trying to connect.

I am in search of a generic SQL query which can work on any database irrespective of the underlying database such as SQL Server, MySql, Oracle or any other.

Till now i have got some queries but they are database specific they cannot be used with every database.

  1. select count(*) as TablesCount from sys.tables :- this query is SQL server specific.
  2. select 1 :- works for every database but according to me this is not a good aproach.
  3. show tables :- i cannot use show tables as thiquery displays the name of tables in the database

but i am just concerned whether i can access database or not.

  1. question 1:- Please suggest if i can make a query that can execute irrespective of underlying database(I am not aware of database structure).
  2. question 2:- can i store result of query (query - show tables) in resultset.
Deepshikha
  • 9,896
  • 2
  • 21
  • 21
user3279837
  • 65
  • 1
  • 6
  • Can you give us the bigger picture what you try to do ? There may be better solution if we know the final goal. This seems as a DB hacking app too me ;) – d.raev May 30 '14 at 06:46
  • if you are working in java . you can track SQL exception. – Pramod S. Nikam May 30 '14 at 06:49
  • Why is 2. not a good approach? I thought it was common sense to use "select 1" (e.g. mysql) or "select 1 from dual" (e.g. oracle) – michael667 May 30 '14 at 06:49
  • for tracking SQLException, i need to fire a query that should try to connect with database and throw exception in case connection cannot be made. I am in search of this query only. Which query should i execute that should work on each database irrespective of the underlying databse. – user3279837 May 30 '14 at 06:51
  • @michael667 if i have two databases abc and xyz i want to check whether i am able to connect to xyz database how can i check connection status with xyz using select 1 query through a java program. – user3279837 May 30 '14 at 06:55
  • This depends on the frameworks you are using. See here for c3p0: http://www.mchange.com/projects/c3p0/#configuring_connection_testing See also here: http://stackoverflow.com/questions/3668506/efficient-sql-test-query-or-validation-query-that-will-work-across-all-or-most – michael667 May 30 '14 at 07:06

0 Answers0