
Been trying to add a primary key to a MySQL table using Java for two days with no success. I'm new to Java and JDBC so bear patience with me. It connects to the database without problem so I know that's not the issue..so I suspect my statment syntax but have been unable to find a good example.
My code sample:
try
{
alterStatement = connection.createStatement();
out.println();
alterStatement.executeUpdate("ALTER TABLE machine ADD PRIMARY KEY (SerialNo)");
out.println("Primary key added");
}
catch (SQLException s)
{
out.println("Error inserting primary key");
}
I get the message: 'error inserting primary key'. Help, please and thank you.

Hello Friend,
You code works fine here. Print the SQLException object to check which type of error occurs. Then let me know, we will help you.
Thanks

Do you mean use 'printStackTrace()'? (I'm clueless about SQL excpetion handling to be honest 8-P )
Did that and got this response...
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: ALTER command denied to user 'root'@'localhost' for table 'machine'
Now if that thing is basically telling me "access denied" then I know the computer just doesn't like me because there shouldn't be anything wrong with my account; it was set up properly, performs all the other queries and naturally has full privileges. Meh.
I ended up dropping the table and creating it from scratch with the primary key in the create table statement because I was working under deadline yesterday but I'd still like to know what went wrong.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.