Zcyan Zcyan - 4 months ago 23
MySQL Question

Error in SQL syntax at line 1

I'm trying to create an examination using MySQL in Java where repetition of number is not allowed.

I'm having a hard time locating my error. I've been debugging my code for hours now.

Error : 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 'null WHERE number = 1' at line 1

Below is my code

String examNames = examName.getName();
String searchNumber = "SELECT * FROM " + examNames + " WHERE number = ?";
PreparedStatement preparedNumber = connect.prepareStatement(searchNumber);
int examNumbers = Integer.parseInt(textNumber.getText());
preparedNumber.setInt(1, examNumbers);
ResultSet results = preparedNumber.executeQuery();
if (results.next()) {
JOptionPane.showMessageDialog(null, "Exam number was already added");

else {
String examLabel = examName.getText();
String insertContent = " Insert INTO " + examLabel
+ "(number, content, choiceA, choiceB, choiceC, choiceD, correctans) values(?,?,?,?,?,?,'"
+ correctAns + "')";
PreparedStatement preparedStatement = connect.prepareStatement(insertContent);
int examNumber = Integer.parseInt(textNumber.getText());
String content = textAreaQuestion.getText();
String choiceA = textAreaA.getText();
String choiceB = textAreaB.getText();
String choiceC = textAreaC.getText();
String choiceD = textAreaD.getText();
String correct = correctAns;
preparedStatement.setInt(1, examNumber);
preparedStatement.setString(2, content);
preparedStatement.setString(3, choiceA);
preparedStatement.setString(4, choiceB);
preparedStatement.setString(5, choiceC);
preparedStatement.setString(6, choiceD);
JOptionPane.showMessageDialog(null, "Added");


If it says

'null WHERE number = 1'

then it means that

String examNames= examName.getName();

is returning null