Universal query to check whether a table exist in all RDBMS - java

I have searched a lot but i could not find a query which will apply to any RDBMS to check whether a table exits. Some are ok with mysql and h2 but its not compatible with oracle. Any one has a solution for this.
This works for oracle but not h2 or mysql
select count(*) as tblCount from user_tables where table_name = 'ALERTS_HISTORY';

The java.sql.DatabaseMetaData object (which is obtainable from a Connection via .getMetaData() has a getTables(…) function, which does what you want; the driver will care about the SQL.

Related

How to get postgres schema from runinng query in Java

assume you do query on pg_stat_activity table and you get example result:
datid
datname
pid
usesysid
usename
application_name
client_addr
client_hostname
client_port
backend_start
xact_start
query_start
state_change
wait_event_type
wait_event
state
backend_xid
backend_xmin
query
backend_type
7198
10
rdsadmin
7195
16384
rdsadmin
32375
10
rdsadmin
PostgreSQL JDBC Driver
16409
c-t-s
21143
16410
c-t-s
c-t-s
10.10.3.1
48037
2021-01-18 13:19:03
2021-01-18 13:31:23
2021-01-18 13:31:23
Client
ClientRead
idle
COMMIT
client backend
I would like to know on which schema the query COMMIT was executed?
My case is i have schema-based multitenancy and i would like to distinguish between schemas (tenants). We always make a single-schema queries, so we dont mix them. To achieve that we set search_path on each getConnection method invocation. Code is developed in java and we dont use schema names in queries, as it is always dynamic -- taken from current request context and set in getConnection method.
With current result I dont know which tenant (schema) is causing slow / long queries.
I have tried to select from pg_class by ids taken from pg_stat_activity but without luck.
So far the comments did not answer my problem, is that possible at all?

Does hsqldb provide functionality similar to an Oracle CURSOR?

In running my junit tests, I'm being given the error:
user lacks privilege or object not found: CURSOR
The query is trying to load up a large number of records, with a subquery returning a corresponding one-to-many set of ids.
Code:
SELECT br.rateid, br.precedence, CURSOR (SELECT rt.trailerid FROM ratetrailer rt WHERE rt.rateid = br.rateid) AS trailer_ids FROM rate br WHERE br.statusID = ?
This works just fine as part of the java code, returning a ResultSet within the main ResultSet.
I have oracle compatibility turned on (jdbc:hsqldb:mem:testDB;sql.syntax_ora=true), but have a feeling this is an oracle shortcut/function/whatever rather than a simple syntax tweak. Is it?
Returning a ResultSet in a column of another ResultSet is an Oracle feature that is not supported by HSQLDB and most other databases.

Get All tables allowed to user with jdbc

I'm connecting to a database using jdbc, getting list of all schemas and tables from database (I assume that some databases may return at this point only tables which current user can query, but some of databases return full list of tables) and when user try to query some tables he get "insufficient privileges" error.
Is there a way to get only tables, user can query using only jdbc capabilities? Without writing special query to database.
Now I'm looking at
DatabaseMetaData dbMeta = connection.getMetaData()
dbMeta.getTablePrivileges(null, null, null);
But from result of this query it's not so clear which exactly tables can user query.
Currently I'm working with SAP HANA database, but in general it may be any database, so I'm looking for some common approach.
Please look at
http://docs.oracle.com/javase/7/docs/api/java/sql/DatabaseMetaData.html#getTablePrivileges%28java.lang.String,%20java.lang.String,%20java.lang.String%29
You have to get the each row from the ResultSet and query of column name TABLE_NAME which contains the table name and PRIVILEGE which contains the access of each table.

How I can Export ddl of all objects like table, index etc in SYBASE IQ/ SYBASE ASE without using any tools?

I have created a sample program in which I want to get ddl of all objects like table, trigger etc using get_ddl method. When I tried following queries in oracle it worked.
SELECT DBMS_METADATA.GET_DDL('TABLE', TABLE_NAME) FROM USER_TABLES;
SELECT DBMS_METADATA.GET_DDL('TRIGGER', TRIGGER_NAME) FROM USER_TRIGGERS;
SELECT DBMS_METADATA.GET_DDL('VIEW', VIEW_NAME) FROM USER_VIEWS;
SELECT DBMS_METADATA.GET_DDL('FUNCTION', OBJECT_NAME) FROM USER_PROCEDURES WHERE OBJECT_TYPE = 'FUNCTION';
SELECT DBMS_METADATA.GET_DDL('PROCEDURE', OBJECT_NAME) FROM USER_PROCEDURES WHERE OBJECT_TYPE = 'PROCEDURE';
SELECT DBMS_METADATA.GET_DDL('INDEX', INDEX_NAME) FROM USER_INDEXES ;
But when I try to create same sample for sybase to get ddl or script of all objects it doesn't work.because get_ddl not supported in sybase database. Can anyone help me to know that whether sybase Iq 15 supports get_ddl methods or there are any other method/way or queries for creating ddl/script of all objects.
I want to post it on SAP forums but all sites are unavailable can anyone suggest me link for post my problem.
Thanks in Advance!!
The ddl for triggers, stored procedures, and views can be pulled from sys.syssource. Unfortunately IQ doesn't store ddl for other objects
For ase,
Use sybsystemprocs
Go
sp_helptext (object)
Go
For views, stored procedures, and triggers

Hibernate multiple native SQL statements

I want to run a native SQL from a file using Hibernate. The SQL can contain several statements creating the database structure (i.e. tables, constraints but no insert/update/delete statements).
Example, very simple query is below (which contains the following two SQL statements)
CREATE DATABASE test;
CREATE TABLE test.testtbl( id int(5));
I am using MySQL db, and when I run the above query I am gettng syntax error returned. When I run them one by one, its ok.
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near
'CREATE TABLE test.testtbl( id int(5))' at line 1
The code to run the query is below (above statement is assigned to 'sql' variable):
session = sf.openSession();
session.beginTransaction();
Query qry = session.createSQLQuery(sql);
qry.executeUpdate();
session.getTransaction().commit();
Any help would be appreciated.
As others have explained
You must run these queries one by one.
The hibernate code gets translated into running one update statement on JDBC.
But you provided two update statements.
In addition,
I personally prefer to have the code that creates tables outside of the Java application, in some DB scripts.
The parameters of the method createSQLQuery is t-sql code;
t-sql code to ensure that in the mysql interface analyzer correctly.
You can try changed the sql :'CREATE TABLE testtbl(id int(5));'
by the way you can use JDBC Connection api (Don't recommend to do so)
Such as:
java.sql.Connection conn=session.connection();

Categories