How to reject DML and DDL queries? - java

I agree that the database user privileges should be tuned to prevent him for running DML or DDL queries but I'm trying to do a similar thing from the client side. The JDBC side.
I downloaded Oracle driver found here.
While the driver and it's companion JARs are in the classpath, I ran the following piece test code:
Connection connection = DriverManager.getConnection(url, username, password);
connection.setAutoCommit(false);
// connection.setReadOnly(true); // Doesn't help
Statement statement = connection.createStatement();
statement.executeQuery("ALTER TABLE GELBANA_TEST.TABLE1 ADD HQ varchar2(40) DEFAULT 'NY'");
connection.rollBack();
But the column is added to the table !!
I can also drop the column.
Shouldn't this be enough to prevent the user from running DML or DDL queries ? I need to stop him from making any changes to the database. He should be allowed to only select data. I checked the connection URL parameters for something that could reject such queries but I found nothing close.

Related

Oracle Express 18c log on from Java jdbc application

new Oracle Express user here,
I did the db install w/o problems in my Win 64 environment,
used sqlplus from terminal to create user and grant him roles and privileges,
and finally this user works fine with the local db from Oracle SQLcl utility -
tables, queries etc.
The problem comes when I try to use the provided boilerplate code from Oracle for Java.
In Eclipse, in new project, with added jdbc8 jars the tested in SQLcl user creadentials
are rejected with exception: java.sql.SQLException: ORA-01045: user C##DBUSER lacks CREATE SESSION privilege; logon denied.
Here is the Oracle boiler code used, with my local details :
OracleDataSource ods = new OracleDataSource();
ods.setURL("jdbc:oracle:thin:#//localhost:1521/XEPDB1");
ods.setUser("c##dbUser");
ods.setPassword("dbUserPassowrd");
Connection conn = ods.getConnection();
PreparedStatement stmt = conn.prepareStatement("SELECT 'Hello World!' FROM dual");
ResultSet rslt = stmt.executeQuery();
while (rslt.next()) {
System.out.println(rslt.getString(1));
}
Would appreciate your help, thanks!
Correct connection string had to be: "jdbc:oracle:thin:#//localhost:1521/XE",
to account for the DB name where user was created and granted priviliges.
As Oracle says:
user C##DBUSER lacks CREATE SESSION privilege; logon denied.
Connect to the Oracle database as a privileged user (such as SYS) and
grant create session to c##dbuser;
(or whichever user name it really is).

Do the underlying JPA JDBC connection can stream?

We have a Java EE application using JPA.
User can ask the app to generate some output (big charts, etc) based on results of a large query, so in that case we decided to use directly JDBC and looping over the resultset.
We have implemented our code like this:
#Resource(mappedName = "jdbc/resource")
private DataSource dataSource;
Connection connection = dataSource.getConnection();
[...]
Connection conn = dataSource.getConnection();
stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
stmt.setFetchSize(Integer.MIN_VALUE);
[...]
ResultSet rs = stmt.executeQuery(sql);
while (rs.hasNext()) {
[....]
}
Anyway it always load all the results in memory, we have tried with different techniques (useCursorFetch true and fetch size = 10, etc) with no luck, we always fail to have a stream resultset.
The "jdbc/resource" is the same resource used by jpa entity manager (so it's working fine), the connection have no "weird" options, we are using glassfish 4.1 for our tests and we are using the connector/J version 5.1.26 and 5.1.32.
It there a way to have a working streaming resultset?

JDBC has issues creating users for mysql

So I'm trying to create a user in JDBC with
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306", "root", "password");
Statement statement = connection.createStatement();
String command = "create user 'username'#'localhost' identified by 'pass'";
statement.execute(command);
Every time I try to run this I get the error below.
java.sql.SQLException: Operation CREATE USER failed for 'username'#'localhost'`
This I log into mysql as root and the same thing happens. I try the same statement and get Operation CREATE USER failed for 'username'#'localhost'. So I then run FLUSH PRIVILEGES and I can then create the user. As soon as I run the program again though the same thing happens.
What is the cause of this and how can I fix it?
It indicate that the user already exists or did exist. Thats why you are getting error while creating it
You can try the following Steps-
Drop the user DROP USER 'username'#'localhost'
Flush privileges - FLUSH PRIVILEGES
then Create the user - CREATE USER 'username'#'localhost'IDENTIFIED BY 'Identification' or CREATE USER 'username'#'localhost'IDENTIFIED BY PASSWORD 'password'

Java MySql Connection Through DSN

I am trying to connect to one of my MySql Databases through a System DSN I set up. The DSN is set up correctly with my SSL certs, username, password, port, and the databases populate the DSN database drop down and the "Test" connection passes. I can't seem to get a connection in Java. I have spent 2 days looking through some examples on Stack but they all refer to an Access database and using JDBC-ODBC bridge which is no longer available in Java 8. I tried using UCanAccess with Jackcess but I have gotten no where. The code below is what I have been tinkering with the last few hours. I normally connect to MySql databases with PHP and receive result in JSON or directly with JDBC driver but for this project neither are really an option. Any ideas. I appreciate the help.
//String username = "<username>";
//String password = "<password>";
//String database = "<database_name>";
try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
//Connect to cllients MySql Database
Connection conn = DriverManager.getConnection("jdbc:ucanaccess:" + database);
//Call VerifyLabel(<MAC>,<MODEL>); Call provided client
CallableStatement cStmt = conn.prepareCall("{CALL verify(?, ?)}");
//MAC
cStmt.setString(1, "mac address");
//model
cStmt.setString(2, "model");
cStmt.execute();
//Getting results from "Status" column
ResultSet rs1 = cStmt.getResultSet();
//Iterate results and print.
while (rs1.next()) {
System.out.println(rs1.getString("Status"));
}
//Close connection conn
rs1.close();
} catch (SQLException ex) {
Logger.getLogger(CambiumStoredTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(CambiumStoredTest.class.getName()).log(Level.SEVERE, null, ex);
}
Using MySql Driver:
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql:"+ database);
also tried:
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+ database);
Error for MySql Driver:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
1) DSN is most commonly assocatiated with ODBC (and often with MS-Access). Hence all the links. ODBC is NOT required for a DSN.
2) Do NOT use Ucanaccess. Use J/Connector for mySQL.
3) Make sure you can communicate with mySQL from the command line. Then focus on getting a small "hello world" JDBC app to connect. Your second and third examples look OK. Be sure to check the mySQL logs for any warnings/errors.
Well, after an entire day of trying to get this to work and sleeping on it for a couple hours I finally got it to work. UCanAccess and mysql-connector did not work. The easiest thing since no other method of connecting to this clients database was acceptable was to push this application in Java 7 rather than 8. This allowed me to Coo=nnect to my DSN with no problems. I understand that this method is not the best solution but it is what is working flawlessly and efficiently. Also, instead of using some rigged up 3rd party libs and jars, I am able to use Connector/J. Thanks everyone for trying to help me. Just incase anyone else runs into this issue, this is how I made it work.
Develope app in Java 7 - not 8.
Set Up System DSN
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//You do not need to provide username or password if it is setup in DSN
Connection conn = DriverManager.getConnection("jdbc:odbc:"+ database);

MySql Jdbc Connector - localization issue

I am trying to write a simple Java
client program that uses koi8r as its character set, and keep on failing.
Class.forName("com.mysql.jdbc.Driver");
Connection conn2 = DriverManager.getConnection ("jdbc:mysql://localhost:3306/test","root",null);
Statement stmt = conn2.createStatement();
int result;
result = stmt.executeUpdate("SET CHARACTER SET koi8r");
stmt = conn2.createStatement();
result = stmt.executeUpdate("DROP TABLE IF EXISTS װֱֱֲֳֹּ, t1, t2");
stmt.close();
assertEquals(0, result);
I'm getting
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 '???????, t1, t2' at line 1
When I put these commands in a script file and execute them using MySql client it works fine
SET CHARACTER SET koi8r
DROP TABLE IF EXISTS װֱֱֲֳֹּ, t1, t2
I sniffed the network and I saw the the jdbc connector sends it with ?????? to the server, so I guess I'm missing some setting to the connection.
Actually I tried (setEncoding, setCharactersEncoding, setConncetionCollation ...), but still failed.
You need to tell the JDBC driver which encoding it should use to transfer the characters over network. It namely defaults to platform default encoding which is in your case apparently not UTF-8.
You can do this by adding the following two query parameters to the JDBC URL:
jdbc:mysql://localhost:3306/test?useUnicode=yes&characterEncoding=UTF-8
See also the documentation.

Categories

Resources