2013-07-28 63 views
-3

我有一個新的懷疑。我想在macfast數據庫中創建一個名爲admin的表。成功創建了表,但是當我以手動方式插入值時(使用查詢),它似乎是一個error.my代碼如下VALUE插入錯誤

public void createTableAdmin() { 
    try{ 

      Class.forName("com.mysql.jdbc.Driver"); 
      conn = (Connection) DriverManager.getConnection(DB_URL_table, USER, PASS); 
      stmt = (Statement) conn.createStatement(); 

      String sql1 = "CREATE TABLE IF NOT EXISTS admin " + 
        "(id INTEGER not NULL, " + 
        " user_name VARCHAR(255), " + 
        " password VARCHAR(255), " + 
        " isAdmin BOOLEAN NOT NULL DEFAULT '0', " + 
        " PRIMARY KEY (id))"; 
      stmt.executeUpdate(sql1); 

      String insert="INSERT INTO admin(id,user_name,password,isAdmin)VALUES(1,admin,admin,1)"; 
      stmt.executeUpdate(insert); 


     }catch(SQLException se){ 
       //Handle errors for JDBC 
       se.printStackTrace(); 
     }catch(Exception e){ 
       //Handle errors for Class.forName 
       e.printStackTrace(); 
     }finally{ 
       //finally block used to close resources 
     try{ 
       if(stmt!=null) 
       conn.close(); 
     }catch(SQLException se){ 
     }// do nothing 
     try{ 
       if(conn!=null) 
       conn.close(); 
     }catch(SQLException se){ 
       se.printStackTrace(); 
     }//end finally try 
    }//end try 

} 

的錯誤是一樣的東西

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'admin' in 'field list' 
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
+3

不喊在您的標題。它很煩人。 ***我可以撒嬌,而且會更惱人。*** – hexafraction

回答

3

你錯過了引號''各地VARCHAR列值。

String insert = "INSERT INTO admin (id,user_name,password,isAdmin) " + 
       "VALUES (1, 'admin', 'admin', 1)"; 
3

你沒有正確引用你的字符串;

String insert= 
    "INSERT INTO admin(id,user_name,password,isAdmin)VALUES(1,admin,admin,1)"; 

應該

String insert= 
    "INSERT INTO admin(id,user_name,password,isAdmin)VALUES(1,'admin','admin',1)"; 
0

你值需要引號或他們作爲字段名。

公共無效createTableAdmin(){ 嘗試{

 Class.forName("com.mysql.jdbc.Driver"); 
     conn = (Connection) DriverManager.getConnection(DB_URL_table, USER, PASS); 
     stmt = (Statement) conn.createStatement(); 

     String sql1 = "CREATE TABLE IF NOT EXISTS admin " + 
       "(id INTEGER not NULL, " + 
       " user_name VARCHAR(255), " + 
       " password VARCHAR(255), " + 
       " isAdmin BOOLEAN NOT NULL DEFAULT '0', " + 
       " PRIMARY KEY (id))"; 
     stmt.executeUpdate(sql1); 

     String insert="INSERT INTO admin(id,user_name,password,isAdmin)VALUES(1,'admin','admin',1)"; 
     stmt.executeUpdate(insert); 


    }catch(SQLException se){ 
      //Handle errors for JDBC 
      se.printStackTrace(); 
    }catch(Exception e){ 
      //Handle errors for Class.forName 
      e.printStackTrace(); 
    }finally{ 
      //finally block used to close resources 
    try{ 
      if(stmt!=null) 
      conn.close(); 
    }catch(SQLException se){ 
    }// do nothing 
    try{ 
      if(conn!=null) 
      conn.close(); 
    }catch(SQLException se){ 
      se.printStackTrace(); 
    }//end finally try 

} //結束嘗試

}