Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create Read only Connection in Java for Oracle Database

Tags:

java

sql

oracle

I have to design a tool which having text area where user can enter the their query and by giving submitting results will be shown. My requirement is such a way that it should accept only select queries but not update, insert, delete. If it is select results should be shown, otherwise error should be thrown. I have tried as below to make DB connection read only.

Connection.setReadOnly(true) which is not recommendable and its throwing an error.

'setReadOnly' is not supported on WebSphere java.sql.Connection implementation.

Can some one please help me in getting Read only connection for Database or if any design changes are needed

like image 636
Ranjithkumar Avatar asked Sep 12 '14 05:09

Ranjithkumar


People also ask

How do I make my Oracle database read only?

Answer: You can make a read only user and make read only tablespaces but you can also make the entire database read-only with an alter command. ORACLE instance started. SQL> alter database open read only; Database altered.

What is the correct syntax of establishing connection with database?

Using the DriverManager Class. Java DB: jdbc:derby:testdb;create=true , where testdb is the name of the database to connect to, and create=true instructs the DBMS to create the database. Note: This URL establishes a database connection with the Java DB Embedded Driver.

How does JDeveloper connect to database?

Start JDeveloper. From the View menu, go to Database and select Database Navigator. The Database Navigator is displayed, showing you a list of available connections. Right-click IDE Connection, and from the shortcut menu, select New Connection.


2 Answers

A common concept is to give the DB-User only the grants that are needed. In your case the application could use an application-specific user with select-grants on the allowed tables / views.

To clarify some misunderstandings: I am talking about the user your JDBC-Connection works with.

like image 178
DaniEll Avatar answered Oct 10 '22 20:10

DaniEll


You can create a read only user in the following way :

SQL> create user read_only identified by read_only;

User created.

SQL> grant create session, select any table, select any dictionary to read_only;

Grant succeeded.

SQL> conn read_only@pdborcl/read_only;
Connected.
SQL> create table t(col number);
create table t(col number)
*
ERROR at line 1:
ORA-01031: insufficient privileges


SQL> desc scott.emp;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 EMPNO                                     NOT NULL NUMBER(4)
 ENAME                                              VARCHAR2(10)
 JOB                                                VARCHAR2(9)
 MGR                                                NUMBER(4)
 HIREDATE                                           DATE
 SAL                                                NUMBER(7,2)
 COMM                                               NUMBER(7,2)
 DEPTNO                                             NUMBER(2)

SQL> select count(*) from scott.emp;

  COUNT(*)
----------
        14

SQL> update scott.emp set ename='LALIT';
update scott.emp set ename='LALIT'
             *
ERROR at line 1:
ORA-01031: insufficient privileges


SQL> insert into scott.emp(ename) values('LALIT');
insert into scott.emp(ename) values('LALIT')
                  *
ERROR at line 1:
ORA-01031: insufficient privileges


SQL>
like image 42
Lalit Kumar B Avatar answered Oct 10 '22 19:10

Lalit Kumar B