Azure ClearDB: Access denied for user 'xxxx' to database 'xxxx' - java

So I recently set up a database through Microsoft Azure using clearDB's MySQL db.
I connected to it through java with the given server, name and user credentials given to me, and everything worked fine, for eight hours... suddenly out of nowhere when I connected (without changing anything that actually had to do with the connection) I got the error message "Access denied for user 'my-given-username'#'%' to database 'my-db-name'"
I double checked everything, and even made a new program trying to connect, but same error message. On the Azure portal and clearDB page it says that the status of the DB is healthy, that it's absolutely not filled up (1.09 %) and that the number of connections is 0 out of maximum 4.
I made a new database the same way, changed the connecting settings accordingly and everything worked just fine!
Does anyone have any idea what might be wrong?
Even though everything is working now with the new DB, I'm afraid the same thing will happen to this one after a while.

Per my experience, the issue is normally related to MySQL user privileges.
I tried to test several sql query in Java to reproduce this issue on Azure ClearDB, and found these SQL cases below caused this issue.
Execute the sql CREATE DATABASE <new-db> in current database
Connect the Jdbc Url jdbc:mysql://<db-host>:3306/mysqlwith current database user & password
Execute the sql query after some grant operation, such as cancel select privilege on table
Although I don't know what sql query your Java program do caused this issue, I think these cases can help you checking.
Any concern, please feel free to let me know.

Related

java- How to handle exception where you are unable to connect to database?

I was working on some program in Java and involving MySQL database. As we know, most programs need database to work with.
As I am thinking there could exceptions that could handles in the application, for example:
Failed to insert to db
table does not exist
etc
When I catch these kind of exceptions, I have the option to store this error message in a form of error code in my table under a Error column.
Now consider this, to establish a connection between an application and the database, I need the IP along with it's username and password before the program works.
In that case, if I were to "break" the program by giving a wrong password , or I easily terminate MySQL service while the application is running. If this happens, the application will throw some sort of database error connection since it couldn't connect to it. At the same time, I can't update the Error column of the table since no connection establish between the program and the db.
I can't seem to figure out the best way to handle this or if there is even a way to do so.
How would one handle this kind of scenario?
Approach#1
You can log these errors in a log file.
If you really want to add every error in the database, then later on you can fetch the errors by scraping the log file and insert them into the database.
Approach#2
You can either try to reconnect to the database & if successful, write the error to the database. If not able to connect then you can fallback to approach#1.

UCanAccess "User lacks privilege or object not found" error on opening database

I have a Java program that needs to pull from a Microsoft Access database. I recently had to change the code to use UCanAccess instead of ODBC as the JDBC-ODBC Bridge is not supported in Java SE 8 (Manipulating an Access database from Java without ODBC)
When I run the code shown below (just the sample code given on the UCanAccess website), I get an error -
ERROR: net.ucanaccess.jdbc.UcanaccessSQLException: user lacks privilege or object not found: NULL
The code is as follows
Connection conn=DriverManager.getConnection("jdbc:ucanaccess://Z:\\Assignment.accdb");
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery("SELECT * FROM tbl2014janjun"); //tbl2014janjun ORDER BY ID
while (rs.next()) {
System.out.println(rs.getString(1));
}
While testing, I noticed that a separate database, Work Order, is able to be accessed by this code and the output is as expected. In addition, whenever this code tries to run, it produces the locking laccdb file in the server directory. Because of this, I believe the issue lies with user privileges. While searching this site for possible answers, most of them related to the second half of the error message - the table was misspelled, etc. I have made sure that the spelling/location/file structure is correct.
Thank you for any help you can provide.
I had exactly the same issue after moving from Tomcat 6 to 7.
Two databases in the same place with the same tables and columns, just the data is different (different projects).
One worked fine, the other one gave the above-mentioned error.
Fixed it by doing a "Compact and Repair Database" operation from within the Access windows app. (In Access 2007 it's Manage > Repair from the main toolbar dropdown.)

Accessing mysql database server from another computer?

we just started learning about sql statements and such, and right now i am totally confused on how to do this:
I need to access the database of another computer (we are using mysql workbench) using java codes
What i have done so far:
grant all privileges using '%'
i can already connect to the database (mysql) in my own computer (java codes and jdbc)
my problems are:
what are the steps do i need to do to be able to access the database from another computer, there are no direct answers in google...and now i am totally confused on how to do this because every site keep on telling me about cpanel, which i dont know how to have one...then this remote access that requires me to change my sql server configuration, i dont know how to find this...
CAN SOMEONE GIVE ME A STEP BY STEP SETTING/SETUP i need to do, to be able to access the database/connection from another computer? i only need the Normal steps, no need for safety/security setups and descriptive steps, i can do the rest of specific research on Google
SOLUTION
Hey Just configure that system IP in the JDBC connection call then you will be able top connect and retrieve the data in the Java Layer.
String DB_URL = "jdbc:mysql://localhost/EMP";
String USER = "username";
String PASS = "password";
Connection conn = DriverManager.getConnection(DB_URL,USER,PASS);
Instead of "localhost" give your destination MySQL system IP address.
Just run the the mysql in one device then get that device Ip address, then on the other device use the http://IPADDRESS:8081/yourWebsite
for example instead of
instead of http://localhost:8081/yourWebsite = http://271.1.1.10:8081/yourWebsite
it will work from there, not need to configure anything. the other device will be using the same project and same database
note both devices should be on the same network

MySQL Java application works when URL is uses `localhost` but not `127.0.0.1`

I'm creating a Java application that uses MySQL for its database.
The program wasn't working and after some trial and error, I found that it worked perfectly when I changed the database address from
jdbc:mysql://127.0.0.1:3306/caches
to
jdbc:mysql://localhost:3306/caches
But aren't these two the same thing? Why would one work but not the other?
A likely reason is how MySQL grants are set up. Log in using localhost and look at the grant tables using SHOW GRANTS, as well as database permissions.
It is very likely that different permissions are set for clients from 127.0.0.1 than localhost.
If this isn't the issue, there are miscellaneous local networking fixes available.

prevent access to mysql database from unauthorized software

i am writing an application in java and i want to enable it to access a mysql remote server.
my problem is that if the application have the user name and password someone can take them and use my db with a different software.
is there a way of preventing it ?
UPDATE
i found this workaround for connecting to a remote MySQL database from an android device.
you put a service in the middle. like a php page that code the data in JSON format which the android app gets via http.
here is the example i found :
Connecting to MySQL database
Having the username and password is designed specifically to grant access to the database. That's the whole point.
You could go to some extra lengths like restricting database connectivity to specific hosts, so at least your paying customers get access to the database and no else can access it, but your customers might choose to use different software to access the database. There's no way around it except licensing terms.
What you could do is run an intermediary program on your own hardware that connects to the database and restrict access to the database to software that is under your direct administrative control. Your program would validate all requests from software under control of your customers and allow the queries that you want to allow and refuse (and log) the queries you do not have to allow. (You do not need to send raw SQL data back and forth -- you can do any amount of processing on the data and queries.)
You can setup JDBC Data Source on your application server. Here you can see example for Oracle Glassfish.
So, your credential won't be used in your code/resources.
If you are saying that you have an application trying to access a MySQL remotely (not in the same box), then i think you need not worry, as the connection that will be established by your application codes will not expose the username and password when it is trying to authenticate and authorize itself to the MySQL server.
You can limit the access to the MySQL-server so that only certain IP-addresses or IP-ranges have access to it.
As a side note, make sure that the user you use in your application only has the needed permissions to the database. For example, the user might not need to be able to create or delete tables. You can even specify permissions for the user on table and column level.

Categories