creating user in mysql using java

前端 未结 4 1212
情书的邮戳
情书的邮戳 2021-01-28 10:55

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

connect mydb;
grant all privileges on mydb.* to \'admin\'@\'localhost\' identifi         


        
相关标签:
4条回答
  • 2021-01-28 11:49

    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.

    0 讨论(0)
  • 2021-01-28 11:51

    You just forgot the quotation marks.

    0 讨论(0)
  • 2021-01-28 11:53
    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.

    0 讨论(0)
  • 2021-01-28 11:54

    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();
    
    0 讨论(0)
提交回复
热议问题