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 %
Related
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).
When I'm trying to get connetcion with online mysql database I get this error:
java.sql.SQLException: Access denied for user ''#'89.229.59.50' (using password: NO)
My code is:
public static Statement stmt = null;
public static Connection conn = null;
private static final String dbURL ="jdbc:mysql://db4free.net:3306/dolar;user=neir#localhost&password=password";
private static void createConnection() {
try {
conn = DriverManager.getConnection(dbURL);
} catch (Exception except) {
except.printStackTrace();
}
}
And yes, I've added mysql connector to my project.
You have to make sure the user (neir) has remote access privileges.
If your using a hosting service make sure they allow your remote IP (89.229.59.50) to connect to MySQL Server.
If you're managing the MySQL Server make sure that it's configured to be accessed remotely, that's done in the my.cnf file, search and comment the line:
bind-address = 127.0.0.1
Hope this helps.
From the error you can already see that the user and password parameters are not used. This might give you a clue that there is something wrong with the JDBC connection URL.
You cannot put the user and password parameters in the connection URL.
You should use: getConnection(String url, String user, String password)
See: https://docs.oracle.com/javase/8/docs/api/java/sql/DriverManager.html#getConnection-java.lang.String-java.lang.String-java.lang.String-
This might be for user has not access privileges so you should do this.
CREATE USER 'enterusername'#'xxx.xxx.xxx.xxx' IDENTIFIED BY 'enterpassword';
Where xxx.xxx.xxx.xxx is you IP or if you want to do this for any IP then just write '%' instead 'xxx.xxx.xxx.xxx'
To grant privileges
GRANT ALL PRIVILEGES ON mor.* TO 'enterusername'#'xxx.xxx.xxx.xxx' WITH GRANT OPTIONĀ ;
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 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.
I am trying to connect to a MySQL Server using JDBC tool in java (using eclipse). I was just wondering how to enter 2 user/password combinations. The first one is the one I use to connect to the server(for example when I ssh into the server) and the second one I enter into phpmyadmin. As of now, I am putting in the phpmyadmin password only into the jdbc connection properties and it's not connecting. This is my current statement:
conn = DriverManager.getConnection("jdbc:mysql://[IP of server]:3306/[Database Table name]", "[UserName (same as phpmyadmin)]","[Password (same as phpmyadmin)]");
I am getting a
java.sql.SQLException: null, message from server: "Host '[My computer's full host name]' is not allowed to connect to this MySQL server"
I was just wondering if I needed to enter my server login/password (the one I use for ssh) as well in addition to the phpmyadmin username/pwd. I am new to JDBC and MySQL server, so I would appreciate any tips.
Just for background, I am able to connect successfully through ssh and I can login to the server through phpmyadmin.
Here's how you can create an account that can access your server from another client machine:
CREATE USER 'bobby'#'localhost' IDENTIFIED BY 'some_password';
That creates the user, and says he can connect from localhost.
If he is on the machine 192.168.0.5, you'd do something like this:
CREATE USER 'bobby'#'192.168.0.5' IDENTIFIED BY 'some_password';
Then of course, you have to grant privileges appropriately:
GRANT ALL PRIVILEGES ON databasename.* TO 'bobby'#'localhost' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON databasename.* TO 'bobby'#'192.168.0.5' WITH GRANT OPTION;
That's been my experience anyway.
You're probably better off reading this section on how to specify MySQL accounts.
When you log in from PHPMyAdmin, the web server is located on the same server that hosts the Mysql database (in your case). Mysql, by default, does not allow connections from remote hosts; only the local machine may access the database.
I'll take a shot in the dark and say that the computer you're running the java code on is not the same machine that is hosting the mysql server. You must configure Mysql to allow connections from remote hosts via the config file and then change the Host row of the mysql.users table for the specified user to allow connection from your IP address (or, if security isn't your concern, any IP address.)
To configure mysql to allow connections from remote hosts you must remove the "bind-address=" line from the configuration file.
To allow any host to log on to a specific mysql user, you must set the mysql.users Host` column to "%".
Both of these must be done.
public class MysqlConnect{
public static void main(String[] args) {
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "jdbctutorial";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
}
}