我很难找出引发此异常的原因以及如何解决它。
此行似乎引发异常
statement.executeUpdate("INSERT INTO " +tableName+ " VALUES (" +questionNumber+ ", '" +question+ "', '"
+possAnswerA+ "', '" +possAnswerB +"', '" +possAnswerC+ "', '" +possAnswerD + "', '" +answer+ "')");
这是输出:
java.sql.SQLSyntaxErrorException: Syntax error: Encountered "s" at line 1, column 57.
at org.apache.derby.client.am.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
at org.apache.derby.client.am.ClientStatement.executeUpdate(Unknown Source)
at Trivia.InputOutput.setQuestions(InputOutput.java:89)
这是表的Create语句。 我知道它是在创建表,因为我能够运行一种方法来检查表是否存在,该表将“ true”返回到输出。 另外,我可以在Netbeans-> Services-> Databases ...等中检查该表已创建,但是当我尝试向表中插入数据时似乎抛出错误。
switch (questionCategory){
case 1:
tableName = "ENTQUESTIONS"; //entertainment questions
break;
等.........
String sqlCreateTable = "CREATE TABLE " +tableName+ "(QUESTIONNUMBER INT, QUESTION VARCHAR(150), POSSIBLEANSWERA VARCHAR(100), "
+ "POSSIBLEANSWERB VARCHAR(100), POSSIBLEANSWERC VARCHAR(100), POSSIBLEANSWERD VARCHAR(100), ANSWER VARCHAR(100))";
statement.executeUpdate(sqlCreateTable);
System.out.println(checkTableExisting(tableName));
产生以下输出:
运行:
1
true
INSERT INTO ENTQUESTIONS VALUES (1, 'Who is the lead singer of the band Nirvana?', 'Homer Simpson', 'Kurt Cobain', 'Michael Jackson', 'Jimi Hendrix', 'Kurt Cobain')
INSERT INTO ENTQUESTIONS VALUES (2, 'In the movie "Lord of the Rings" what is the last name of the Hobbit Frodo?', 'Gamgee', 'Baggins', 'Gandalf', 'Took', 'Baggins')
INSERT INTO ENTQUESTIONS VALUES (3, 'In the cartoon series "Scooby Doo" what is the name of Scooby's nephew?', 'Yabba Doo', 'Scooby Dee', 'Scrappy Doo', 'Skippy Doo', 'Scrappy Doo')
ERROR CODE: 30000
Apr 17, 2019 4:01:30 PM Trivia.InputOutput setQuestions
MESSAGE: Syntax error: Encountered "s" at line 1, column 100.
SEVERE: null
java.sql.SQLSyntaxErrorException: Syntax error: Encountered "s" at line 1, column 100.