Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate a Table for a user account in java using sql
    primarykey
    data
    text
    <p>I am having issues running a statement in java that should give me a unique table for every new user account that is created. I get a syntax error on the Create Table statement and from what I have checked so far, everything seems to be in order. I am thinking there might be a bug in XAMMP or phpmyadmin which are the tools I am using for the database. Here is the code:</p> <pre><code>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(); } } </code></pre> <p>Here is the error message:</p> <pre><code>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) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload