1
我在java中運行一個語句時應該爲每個創建的新用戶帳戶提供一個唯一表。我在Create Table語句中得到了一個語法錯誤,並且從我目前檢查的內容來看,所有內容似乎都是按順序排列的。我想在XAMMP或phpmyadmin中可能存在一個錯誤,這是我用於數據庫的工具。下面是代碼:使用sql在java中爲用戶帳戶創建一個表格
public static void CreateAccount(){
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loaded");
Connection connection=DriverManager.getConnection
("jdbc:mysql://localhost/users", "root", "");
System.out.println("Database connected");
Statement statement=connection.createStatement();
statement.execute
("CREATE USER '"+user+"'@'localhost' IDENTIFIED BY '"+password+"'; "+
"CREATE TABLE "+user+" (AccountID int, FirstName varchar (255), LastName varchar (255), Age int); "
+ "GRANT SELECT, INSERT, UPDATE, DELETE ON USERS."+user+" TO '" +user+"'@'localhost';"
+"INSERT INTO"+user+"VALUES (1, '"+FirstName+"' ,'"+LastName+"',"+Age+");");
}catch (SQLException | ClassNotFoundException a){
JOptionPane.showMessageDialog(null,"Can't connect to database");
a.printStackTrace();
}
}
以下是錯誤消息:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TABLE mark (AccountID int, FirstName varchar (255), LastName varchar (255' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2536)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2465)
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:734)
at SimpleJdbc.CreateAccount(SimpleJdbc.java:54)
at Basic.main(Basic.java:113)
我到底需要重寫我的代碼才能正常工作? –
沒有MySQL的方便,但我想你在連接字符串的末尾添加「?allowMultiQuery = true」(如果這就是你要求的) – Andrew
試過了,但它似乎並沒有工作。我沒有連接字符串,我只是有一個直接的連接,你可以在我提供的代碼中看到 –