简体   繁体   中英

create database in mySQL doesn't seem to be working correctly

I want to have Java create a database and a user for me. I am using Netbeans so that I can see what databases exist. When I create the database I can see in Netbeans -> Services -> Databases that the database exists.

The program initially checks if the database already exists, so that I can run it multiple times. What surprised me was when I ran it a second time it didn't know that the database already existed so that it wanted to recreate the database. If Netbeans sees it, why doesn't my Java program see it? (I found my error in the grant statement, so I'll fix it to be correct.)

I have routines to drop databases and users. Here too if I drop a database, it disappears inside the Netbeans -> Services -> Databases.

The situation with users is working with no problems. If I drop a user and then create the database, it will recognize that the user doesn't exist, and create him. If I drop just the database, it recognizes that the user already exists.

I will include some code snippets for the drop database and user:

    String sql = "drop user '" + user + "'@'%'";
    try {
        stm1 = con1.createStatement();
        executeStatement(stm1, sql);
        stm1.close();
        con1.close();
    } catch (Exception e) { System.out.println(e.getMessage());}

    String sql = "drop database " + dbName;
    try {
        stm1 = con1.createStatement();
        executeStatement(stm1, sql);
        stm1.close();
        con1.close();
    } catch (Exception e) { System.out.println(e.getMessage());}

As can be seen there is nothing special going on here. In the create database, I thought the problem could stem from the fact that I wasn't creating any tables at this stage. (Edited to remove the adding of a table.)

OK, at this stage the database has no tables. I need to go in a second time and now I expect to be able to connect to the database I just created with the user who has privileges on the database. The error I get is

Access denied for user 'myuser'@'%' to database 'mytst1'

I just created mytst1, and I see it under Netbeans -> Services -> Databases. It currently has no tables. Again the error was in the grant statement....

Here is the routine to create the database:

void createDB() {
    Connection con1 = connect2database(false, false);
    if( con1 != null) {
        JOptionPane.showMessageDialog(this, "The database already exists.\n"
            + "Maybe you only need to create the tables?");
        return;
    }
    con1 = connect2database(false, true);
    if( con1 == null) {
        JOptionPane.showMessageDialog(this, "Can't connect to mySQL server.\n"
            + "Is the path to the database correct, including IP (with :3306)\n"
            + "The database name itself (the last part) is ignored, but the path is used.\n"
            + "Is the root password correct?");
        return;
    }
    String tmp, user = jTextUser.getText();
    String host, pwTmp;
    String userPw = jTextPW.getText();
    String sql = "create database if not exists " + dbName;
    boolean OK1, itExists;
    Statement stm1, stm2;
    ResultSet rSet;
    try {
        stm1 = con1.createStatement();
        OK1 = executeStatement(stm1, sql);
        if( !OK1) {
            JOptionPane.showMessageDialog(this, "Create database failed.");
            return;
        }

        sql = "select host, user from mysql.user where user = '" + user + "'";
        rSet = stm1.executeQuery(sql);
        itExists = false;
        while(rSet.next()) {
            itExists = true;
            host = rSet.getString(1);
            tmp = rSet.getString(2);
        }

        if(!itExists) {
            sql = "create user '" + user + "'@'%' identified by '" + userPw + "'";
            OK1 = executeStatement(stm1, sql);
            if( !OK1) {
                JOptionPane.showMessageDialog(this, "Create user failed.");
                return;
            }
        }
        sql = "grant all privileges on " + dbName + ".* to '" + user + "'@'%' identified ";
        sql += "by '" + userPw + "' with grant option";
        executeStatement(stm1, sql);
        sql = "flush privileges";
        executeStatement(stm1, sql);
        stm1.close();
        con1.close();
    } catch (Exception e) { System.out.println(e.getMessage()); }
}

What I would expect to happen is that it would exit at the top of the routine saying the database already exists. It does exit at this point if I ask to use a database which I have been using for ages to store my data. The old database obviously has lots of tables.

My error was doing a "grant all privileges on mytst1" instead of "grant all privileges on mytst1.*", ie all table in mytst1.

Thanks, Ilan

When you create a user or schema, the user and schema is not the same but you have to add a user privileges to operate on schema, and them are lost once you drop either of them. When you create a separate statement to grant privileges make sure that the user exists. FLUSH statement reloads privs from the mysql schema to make it effective once the user is connected. If there are not records in that schema it is less effective. Granting the user privs to mysql schema is also required if you want to have access to the database metadata, especially during a development phase.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM