1

我在 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)
4

1 回答 1

0

您需要在数据库连接中允许多个语句。添加

?allowMultiQuery=true"

到您的连接字符串。

或者你可以建立一个存储过程来为你做这件事。

但是,您还需要查找 SQL 注入,关于 SO 的大量信息。

第三,你到底为什么想要每个用户一张桌子?

于 2013-10-28T17:38:38.810 回答