2013-11-03 40 views
0

我想讓Java爲我創建一個數據庫和一個用戶。我正在使用Netbeans,以便我可以查看存在哪些數據庫。當我創建數據庫時,我可以在Netbeans - >服務 - >數據庫中看到數據庫存在。在mySQL中創建數據庫似乎沒有正常工作

該程序最初檢查數據庫是否已經存在,以便我可以多次運行它。讓我感到意外的是,當我第二次運行它時,它不知道數據庫已經存在,因此它想要重新創建數據庫。如果Netbeans看到它,爲什麼我的Java程序沒有看到它? (我在授權聲明中發現我的錯誤,所以我會修復它是正確的。)

我有刪除數據庫和用戶的例程。在這裏,如果我刪除一個數據庫,它將在Netbeans - > Services - > Databases中消失。

與用戶的情況是沒有問題的。如果我放下一個用戶然後創建數據庫,它會識別出該用戶不存在並創建他。如果我只刪除數據庫,它會識別用戶已經存在。

我將包括降數據庫和用戶的一些代碼片段:

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());} 

可以看出有什麼特別的地方。在創建數據庫中,我認爲這個問題可能源自我在這個階段沒有創建任何表的事實。 (編輯刪除添加一個表。)

好吧,在這個階段數據庫沒有表。我需要第二次去,現在我希望能夠連接到剛剛創建的數據庫,並且擁有對數據庫有特權的用戶。我得到的錯誤是

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

我剛剛創建mytst1,我在Netbeans - >服務 - >數據庫下看到它。它目前沒有表格。同樣的錯誤是在GRANT語句....

下面是創建數據庫的程序:

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()); } 
} 

我會希望發生的是,它將在日常俗語的頂部退出數據庫已經存在。如果我要求使用一直以來用於存儲數據的數據庫,它現在確實退出了。舊的數據庫顯然有很多表。

我的錯誤是在「授予mytst1的所有權限」而不是「授予mytst1。*的所有權限」,即mytst1中的所有表。

感謝, 宜蘭

回答

1

當你創建一個用戶或模式,用戶和模式是不一樣的,但你必須添加一個用戶權限的模式操作,並把它們丟失一旦放棄任他們。當您創建單獨的語句以授予特權時,請確保該用戶存在。 FLUSH語句重新加載mysql模式的權限以使其在用戶連接後生效。如果該架構中沒有記錄,則效率較低。如果要訪問數據庫元數據,則還需要授予用戶權限mysql模式,特別是在開發階段。

+0

我不知道我明白你在說什麼。我的代碼首先創建數據庫,然後檢查用戶是否存在。如果不是,他創建。如果他已經存在,他將被授予剛剛創建的數據庫的特權。在connect2datbase(false,false)中,它會嘗試連接到存在或不存在的數據庫。如果失敗connect2database(false,true)使用root密碼連接到數據庫mysql。這是你所指的模式?也許我應該連接到其他東西,而不是MySQL?我不知道我還能連接到什麼。 –

+0

對不起,我做了一些愚蠢的事情。通過添加「調試信息」並創建表格,我在連接到mysql時做了它,而不是剛剛創建的數據庫。愚蠢的...所以我回到我的原始代碼沒有添加表,但我無法連接到我的新創建的數據庫與其用戶。我不明白爲什麼不。 –

+0

在連接url中,您可以指定默認模式。 – 2013-11-03 11:18:27