我在 java 中运行语句时遇到问题,该语句应该为创建的每个新用户帐户提供一个唯一表。我在 Create Table 语句中收到语法错误,从我目前检查的内容来看,一切似乎都井井有条。我认为 XAMMP 或 phpmyadmin 中可能存在错误,这是我用于数据库的工具。这是代码:
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)