清单中的 SQL 异常
SQL-Exception in listing
我正在用 Java 编写一个小应用程序,这是一个包含(虚构的)学生数据的非常基本的数据库。到目前为止它运行良好,但我在尝试列出包含学生经常光顾的大学课程表时遇到了问题。我想列出所有可能的学位,所有学位都有 6 个学期,以及每个类别可以选修的课程,他们每周修读多少小时等等。第一点有效,但我在每个学期尝试用各自的课程填充不同类别时遇到问题。这是代码(我认为与问题相关的东西用两颗星括起来):
@Override
public List<List<String>> getStudyingplan(Studies s) throws ApplicationException {
List<List<String>> curriculum= new ArrayList<List<String>>();
ArrayList<String> line = new ArrayList<String>();
ArrayList<String> categories = new ArrayList<String>();
String currentCategory;
String name = s.getName();
String abb= s.getAbbreviation();
String category;
String categoryHeader= ("Mod E C P Cr");
line.add("Course of studies\n" + name + " (" + abb+ ")");
for (int i = 1; i <= 6; i++) {
line.add(i + ". Semester");
}
line.add("");
curriculum.add(line);
line= new ArrayList();
line.add("Category");
for (int i = 1; i <= 6; i++) {
line.add(categoryHeader);
}
line.add("Sum");
curriculum.add(line);
line= new ArrayList();
//Connect to database:
try {
Class.forName(driver).newInstance();
con = DriverManager.getConnection(database);
PreparedStatement giveCategories = con.prepareStatement("SELECT distinct k.* "
+ "FROM CATEGORY c, CURRICULUM cu, MODULE m "
+ "WHERE cu.SABB = ? AND cu.MABB = m.MABB AND m.KABB = c.KABB "
+ "ORDER BY c.INDEX");
**PreparedStatement giveModule = con.prepareStatement("SELECT distinct m.*" +
"FROM MODULE m, STUDIES st, CURRICULUM c" +
"WHERE st.SKABB = ? AND c.SEM = ? "
+ "AND c.MABB = m.MABB AND m.KABB = ?");**
giveCategories.setString(1, abb);
**giveModule.setString(1, abb);**
ResultSet categoriesGiven= giveCategories.executeQuery();
while (categoriesGiven.next()) {
categories.add(categoriesGiven.getString("NAME") + " (" +
categoriesGiven.getString("KABB") + ")");
}
**for (int i = 0; i < 6; i++) {
currentCategory = categories.get(i);
line.add(currentCategory);
giveModule.setString(3, currentCategory);
for (int j = 0; j < 6; j++) {
Integer seme = new Integer(j);
seme++;
giveModule.setString(2, seme.toString());
ResultSet current Modules = giveModules.executeQuery();
while (currentModules.next()) {
zeile.add(currentModules.getString("MABB"));
}
}
line.add("Sum");
curriculum.add(line);
line= new ArrayList();
}**
*A bunch of other stuff happens*
} catch (Exception e) {
System.out.println("getStudyingPlan has encountered an error.");
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return curriculum;
}
我不断收到的错误是:
java.sql.SQLSyntaxErrorException: Syntax error: Encountered "st" at line 1, column 73
所以我假设 PreparedStatement
某处有错误,但我似乎找不到它。在我试图自己解决问题时,任何帮助,即使只是轻推,都将不胜感激。
你的查询是运行在一起,在c
之后加一个space。
"FROM MODULE m, STUDIES st, CURRICULUM c" +
"WHERE st.SKABB = ? AND c.SEM = ? "
应该变成
"FROM MODULE m, STUDIES st, CURRICULUM c " + // Note space
"WHERE st.SKABB = ? AND c.SEM = ? "
我正在用 Java 编写一个小应用程序,这是一个包含(虚构的)学生数据的非常基本的数据库。到目前为止它运行良好,但我在尝试列出包含学生经常光顾的大学课程表时遇到了问题。我想列出所有可能的学位,所有学位都有 6 个学期,以及每个类别可以选修的课程,他们每周修读多少小时等等。第一点有效,但我在每个学期尝试用各自的课程填充不同类别时遇到问题。这是代码(我认为与问题相关的东西用两颗星括起来):
@Override
public List<List<String>> getStudyingplan(Studies s) throws ApplicationException {
List<List<String>> curriculum= new ArrayList<List<String>>();
ArrayList<String> line = new ArrayList<String>();
ArrayList<String> categories = new ArrayList<String>();
String currentCategory;
String name = s.getName();
String abb= s.getAbbreviation();
String category;
String categoryHeader= ("Mod E C P Cr");
line.add("Course of studies\n" + name + " (" + abb+ ")");
for (int i = 1; i <= 6; i++) {
line.add(i + ". Semester");
}
line.add("");
curriculum.add(line);
line= new ArrayList();
line.add("Category");
for (int i = 1; i <= 6; i++) {
line.add(categoryHeader);
}
line.add("Sum");
curriculum.add(line);
line= new ArrayList();
//Connect to database:
try {
Class.forName(driver).newInstance();
con = DriverManager.getConnection(database);
PreparedStatement giveCategories = con.prepareStatement("SELECT distinct k.* "
+ "FROM CATEGORY c, CURRICULUM cu, MODULE m "
+ "WHERE cu.SABB = ? AND cu.MABB = m.MABB AND m.KABB = c.KABB "
+ "ORDER BY c.INDEX");
**PreparedStatement giveModule = con.prepareStatement("SELECT distinct m.*" +
"FROM MODULE m, STUDIES st, CURRICULUM c" +
"WHERE st.SKABB = ? AND c.SEM = ? "
+ "AND c.MABB = m.MABB AND m.KABB = ?");**
giveCategories.setString(1, abb);
**giveModule.setString(1, abb);**
ResultSet categoriesGiven= giveCategories.executeQuery();
while (categoriesGiven.next()) {
categories.add(categoriesGiven.getString("NAME") + " (" +
categoriesGiven.getString("KABB") + ")");
}
**for (int i = 0; i < 6; i++) {
currentCategory = categories.get(i);
line.add(currentCategory);
giveModule.setString(3, currentCategory);
for (int j = 0; j < 6; j++) {
Integer seme = new Integer(j);
seme++;
giveModule.setString(2, seme.toString());
ResultSet current Modules = giveModules.executeQuery();
while (currentModules.next()) {
zeile.add(currentModules.getString("MABB"));
}
}
line.add("Sum");
curriculum.add(line);
line= new ArrayList();
}**
*A bunch of other stuff happens*
} catch (Exception e) {
System.out.println("getStudyingPlan has encountered an error.");
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return curriculum;
}
我不断收到的错误是:
java.sql.SQLSyntaxErrorException: Syntax error: Encountered "st" at line 1, column 73
所以我假设 PreparedStatement
某处有错误,但我似乎找不到它。在我试图自己解决问题时,任何帮助,即使只是轻推,都将不胜感激。
你的查询是运行在一起,在c
之后加一个space。
"FROM MODULE m, STUDIES st, CURRICULUM c" +
"WHERE st.SKABB = ? AND c.SEM = ? "
应该变成
"FROM MODULE m, STUDIES st, CURRICULUM c " + // Note space
"WHERE st.SKABB = ? AND c.SEM = ? "