Get All tables allowed to user with jdbc - java

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.

Related

Updating two sql tables using one query in java

I want to update two sql tables at once in java. I'm using SQLiteManager. Could someone please suggest a way of doing that?
Assuming you have the driver and connection to the database, you should be able to run most sql commands from java. There is not a single sql statement that will update two tables at once but you can update each table in turn which will have the same effect.
See http://www.javaworkspace.com/connectdatabase/connectSQLite.do for some examples.
For a table update, use
statement.execute(sql);
where sql is a string of the form
sql = "UPDATE myTable SET myColumn = newValue WHERE someOtherColumn=value";

How to insert data in auth_table from servlet?

How do i insert data into auth_user table of django rest framework from servlets. I have connected the servlet to the database. I tried inserting the username and password by
query = new String("insert into Auth_User(password, username) values('"+pswd+"','"+loginid+"')");
st.executeUpdate(query);
But i am getting an error- java.sql.SQLException: ORA-00947: not enough values. I figured there are other fields in the auth_user table which needs to be filled. So how do i insert the date_joined value and others in the table using java commands.
A comma was missing, query works fine now.

Universal query to check whether a table exist in all RDBMS

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.

How to retrieve data from a Query instead of from Tables with Jackcess

I have an Access DB, to make the job easier, I did the queries into DB. How I can take data from the query instead of from tables?
To take data from tables I used this code:
Table table = db.getTable("matchTable");
Cursor cursor = CursorBuilder.createCursor(table);
while (cursor.findNextRow(Collections.singletonMap("idMatch", 15))) {
Row row = cursor.getCurrentRow();
...
}
According to the Jackcess FAQ (specifically, here):
Can Jackcess execute SQL queries?
As of the 1.1.19 release, Jackcess has the ability to read the Queries saved in an Access database (i.e. interpret the data stored in the system Queries table). However, Jackcess does not have the ability to execute these Queries.
It means that Jackcess can retrieve the SQL code associated with a saved query in Access, but Jackcess cannot execute that SQL code (or return its results).
In other words, if we have a saved query in Access named [MySavedQuery] then Jackcess can retrieve a String that contains the SQL command, e.g.,
SELECT * FROM Customers WHERE City='wherever'
but Jackcess cannot execute that command to actually return the results of that query. Also, the Jackcess method db.getTable() can open an Access table, but it cannot open a saved query as if it was a table.
However, UCanAccess can execute saved Access queries and return their results. That is, UCanAccess can do
SELECT * FROM MySavedQuery

How to display a database columns using CRUD in play framework?

I want to display the data of a postgresql database using the CRUD in the play framework; I looked for any examples to get idea which I didn't find after a long time of searching in google. Someone help me with this if you can or post a valid link regarding this. Thanks in advance!
I use, Play 1.2.5, java and postgresql.
I assume you want to do this in your application code in runtime.
You can execute query using DB plugin to search DB metadata using native PostgreSQL queries.
Here's an example how to get column names of my system DOCUMENT table:
List<String> columns = new ArrayList<>();
ResultSet result = DB.executeQuery("select column_name from INFORMATION_SCHEMA.COLUMNS where table_name = 'DOCUMENT';");
while(result.next()) {
String column = result.getString(1);
columns.add(column);
}
This note that code is somewhat simplified and you should use prepared statements if anything in this query will be inserted from data that user or any other system entered.
Use DB.getConnection().prepareStatement() to get PreparedStatement instance.

Categories