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).
Related
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.
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'
I tried using this code to connect, but it doesn't work:
Class.forName("com.mysql.jdbc.Driver").newInstance();
//line 4 Connection con = DriverManager.getConnection("jdbc:mysql://example.com:3306/db", "user", "pass");
Statement st = con.createStatement();
String sql = ("SELECT * FROM users;");
ResultSet rs = st.executeQuery(sql);
if(rs.next()) {
int id = rs.getInt("id");
String str1 = rs.getString("imei");
System.out.print(id+"+++"+str1);
I receive this error:
java.sql.SQLException: Access denied for user 'user'#'ip' (using
password: YES) at
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055) at
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) at
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3558) at
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3490) at
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:919)
This user has all privileges to the database. How do I fix this?
Write following command into remote machine Mysql Server.
mysql> GRANT ALL ON *.* to root#'localhost' IDENTIFIED BY 'your-root-password';
mysql> FLUSH PRIVILEGES;
This command helps you to connect remote machine to your machine
I think you dont have Grant for 'ip'
Try this:-
grant all privileges on db.* to 'user'#'%' to allow connections originating from anywhere
Even though that the user already has all the privileges, you still need to deliver the user information in a line like this:
DriverManager.getConnection("jdbc:mysql://localhost/db-name","user","password");
You have to replace "db-name" for the name of your database, "user" for the user you created and "password" for the password of the user (if you don't have a password then leave it as "").
You can even create a variable type Connection and added to your code in case you need this in the future, like:
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/db-name","user","password");
Hope that helps!
To access database remotely from java you need to set database user access permission to access data from anywhere execute following query in your mysql terminal/
GRANT ALL PRIVILEGES ON database_name.* TO 'mysql_user'#'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
In above query % shows that mysql_user can access database_name database from any ip globally. If you want to allow specific ip then replace your ip with %
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);
I am trying to fetch the data from Table of .mdb file. I am able to connect to the file but when i execute the query I get the following error.
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Record(s) cannot be read; no read permission on
However, if I open the .mdb file directly with same user login I can view/edit the table.
Following is the code which I am writing,
conn = DriverManager.getConnection(database, "", "");
Statement s = conn.createStatement();
String selTable = "SELECT * FROM myTable";
ResultSet rs = s.executeQuery(selTable);
What I am doing wrong?
Thanks in advance.
If your access database hasn't any username you can get connection without it. Can you try this:
conn = DriverManager.getConnection(database);
If you are prompted for a username and password when you open the database in Access then the database has "user-level security" enabled. If that is the case then you should supply the username and password of an appropriate Access user as part of the connection string. For some examples, look here.