0

In MySQL command line client after logging in as root, I typed:

connect mydb;
grant all privileges on mydb.* to 'admin'@'localhost' identified by 'pass';

Now within Java, I succesfully connect to the db using the admin userid using drivers.

 Statement put=connect.createStatement();

 //**WORKS succesfully**
 put.execute("insert into mydb.emp values(100,joe)");   

 //**does NOT work**
 put.execute("grant all privileges on mydb.* to 'john'@'localhost' identified by 'pass'"); 

Why does an insert command work but grant command never work through Java?

Please help.

6
  • One thing that I see immediately is that your INSERT query is a string, while your GRANT query is not. Commented Jul 10, 2013 at 10:19
  • @Xperiaz X as root you mean from linux terminal for example? Commented Jul 10, 2013 at 10:19
  • Not all databases support DDL commands via JDBC, so you might not be able to modify the database structure via JDBC at all. Commented Jul 10, 2013 at 10:25
  • no to enter sql command line client in windows we need to enter a root password. MySQL root not linux root. Commented Jul 10, 2013 at 10:39
  • Does MySQL support DDL? or a new driver is required? Commented Jul 10, 2013 at 10:40

5 Answers 5

1
put.execute(MySQL query) 

in here you can execute only MySQL query but

grant all privileges on mydb.* to 'admin'@'localhost' identified by 'pass';

Not a MySQL query it is just a command for MySQL.

Sign up to request clarification or add additional context in comments.

6 Comments

Oh, I see. So is it possible to send commands to MySQL via java?
You have to set user privileges via MySQL admin terminal(command line)
Is it not possible using java? My application is adding an employee (thus adding a DB user). The employee then has access to other tables in the database
You can't set privileges via java for MySQL user.
Yes, finally I implemented it. for my program ,we can set all privileges except grant option via java as long as we have root id and password. Anyway this answer really helped me a lot. Thank you :)
|
0

You just forgot the quotation marks.

2 Comments

My bad, In the code I had put quotation but here I forgot to type it
@XperiazX in that case you should look my answer. Check the ExtendedAnsiSQL flag
0

Try following

put.execute("grant all privileges on mydb.* to 'john'@'localhost' identified by 'pass'"); 

And also make sure you have ExtendedAnsiSQL flag to 1.

1 Comment

Sorry,In the code I had put quotation but here I forgot to type it here. ExtendedAnsiSQL flag? please do tell what it is?
0

I tried working with query parameters - what I found somewhat confusing is that you have to put a blank between the parameter and @localhost for example - this here worked for me: (jpaEm is a JpaEntityManager here)

// Create user
String sql = "CREATE USER ? @localhost";
Query query = jpaEm.createNativeQuery(sql);
query.setParameter(1, user.getUserName());
jpaEm.getTransaction().begin();
query.executeUpdate();
jpaEm.getTransaction().commit();

// Set password
sql = "SET PASSWORD FOR ? @localhost = PASSWORD(?)";
query = jpaEm.createNativeQuery(sql);
query.setParameter(1, user.getUserName()); 
query.setParameter(2, user.getPassword());
jpaEm.getTransaction().begin();
query.executeUpdate();
jpaEm.getTransaction().commit();

Comments

0
  1. Maybe this can help someone so far. I resolved it with this code below.

     String db = "data";
     String us = "Luis";
     String pas = "123456";
     //-----Connection to the server------------
     Class.forName("com.mysql.cj.jdbc.Driver");
     cntConnec = 
     DriverManager.getConnection("jdbc:mysql:///","admin","admin");
     Statement stmMysql = cntConnec.createStatement();
     JOptionPane.showMessageDialog(null, "Successful Connection");
     //-------Create a new DB------------
     stmMysql.execute("CREATE DATABASE "+db);
     stmMysql.execute("USE "+db);
     stmMysql.execute("CREATE TABLE bobo_table (f00 char(31))");
     //-------Create a user with all privileges--------------
     stmMysql.executeUpdate("CREATE USER "+us+"@'localhost' IDENTIFIED BY 
                    '"+pas+"'");
     stmMysql.executeUpdate("GRANT ALL PRIVILEGES ON "+db+".* TO 
                    "+us+"@'localhost' WITH GRANT OPTION");
     stmMysql.executeUpdate("FLUSH PRIVILEGES");
     stmMysql.close();
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.