Unable to use Crate JDBC 2.6.0 in read only mode - java

I want to restrict delete/update statements
This is what I am trying
ResultSet rs = stmt.executeQuery("delete from test where id=243640033")
Ideally executeQuery method of JDBC should not allow update, delete and etc. But Crate Database is simply executing delete queries. Then I tried connection.setReadOnly(true) and it did not work
Is there any way to restrict Crate JDBC doing update/delete/drop operations from stmt.executeQuery(somequery) method ?

You could create a CrateDB user with Data Query Language (DQL) privileges only and subsequently use this user to connect with JDBC. This will prevent all INSERT / UPDATE / DELETE / DROP operations.
CREATE USER read_only WITH (password = '<secure-password>');
GRANT DQL TO read_only;
If required you can further restrict access only to specific schemas, tables or views. Compare the documentation on how to achieve this.

Related

How to restrict hibernate native query to only SELECT

I have an application with a feature where user can make native SQL queries to database. Is there a way in hibernate to restrict native SQLQuery to only make SELECT queries?
Currently doing the query like this:
Query query = session.createSQLQuery(sql);
query.setResultTransformer(AliasToEntityOrderedMapResultTransformer.INSTANCE);
List<Map<String, Object>> aliasToValueMapList = query.list();
I know I have an option to create a SQL user with restricted privileges but would be nice to have this feature as backup as sometimes I have no control over the DB management.
I would not want to use sql.contains("update") style approach as in some occasions these "keywords" (update/delete etc.) could be needed in searching the data.

Java Connection String to query from two database

I am having a problem. I have a query that checks one database table and updates another database table. I am using MySQL 5.1
UPDATE dldd.temp,test.temp
SET test.temp.name = dldd.temp.word
WHERE dldd.temp.id = test.temp.id
this is my SQL statement that is working fine. Now I want to execute this statement using Java PreparedStatement . The problem is I don't know how to write the Connection String to select two database i.e
"jdbc:mysql://localhost:3306/"+dbname+"?characterEncoding=UTF-8"
What should come in place of dbname. Can I select multiple db there.
Have a look at http://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html.
If the database is not specified, the connection is made with no default database. In this case, either call the setCatalog() method on the Connection instance, or fully specify table names using the database name (that is, SELECT dbname.tablename.colname FROM dbname.tablename...) in your SQL. Opening a connection without specifying the database to use is generally only useful when building tools that work with multiple databases, such as GUI database managers.

how to check database user permission on fmp database using JAVA

I want to know how to run FileMaker(FMP pro) database command in java. I have got the connection to database,but not sure how to execute below query.
Get(AccountPrivilegeSetName)
ref:
http://www.filemaker.com/11help/html/func_ref2.32.4.html#1051898
You will need to add a calculation field in FileMaker to return as the result of the JDBC query. This is because you cannot call functions via JDBC, you can only retrieve field values.

Change Table names in derby database using entitymanager

I am using an APACHE DERBY database, and basing my database interactions on EntityManager, and I don't want to use JDBC class to build a query to change my tables' names (i just need to put a prefix to each new user to the application, but have the same structure of tables), such as:
//em stands for EntityManager object
Query tableNamesQuery= em.createNamedQuery("RENAME TABLE SCHEMA.EMP_ACT TO EMPLOYEE_ACT");
em.executeUpdate();
// ... rest of the function's work
// The command works from the database command prompt but i don't know how to use it in a program
//Or as i know you can't change system tables data, but here's the code
Query tableNamesQuery= em.createNamedQuery("UPDATE SYS.SYSTABLES SET TABLENAME='NEW_TABLE_NAME' WHERE TABLETYPE='T'");
em.executeUpdate();
// ... rest of the function's work
My questions are :
This syntax is correct?
Will it work?
Is there any other alternative?
Should I just use the SYS.SYSTABLES and find all the tables that has 'T' as tabletype and alter their name their, will it change the access name ?
I think you're looking for the RENAME TABLE statement: http://db.apache.org/derby/docs/10.10/ref/rrefsqljrenametablestatement.html
Don't just issue update statements against the system catalogs, you will corrupt your database.

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