gpt4 book ai didi

java - 列表中的 SQL 异常

转载 作者:行者123 更新时间:2023-12-01 17:00:42 24 4
gpt4 key购买 nike

我正在用 Java 编写一个小型应用程序,这是一个包含(虚构的)学生数据的非常基本的数据库。到目前为止,它运行得很好,但是当我试图制作一个包含学生经常光顾的大学类(class)的列表时遇到了一个问题。我想列出所有可能的学位,所有学位都有 6 个学期,以及每个类别可以修读的类(class)、每周需要多少小时等等。第一点是有效的,但是当我试图在每个学期用各自的类(class)填充不同的类别时遇到一个问题。这是代码(我认为与问题相关的内容用两​​颗星括起来):

@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后面添加一个空格。

"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 - 列表中的 SQL 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27823032/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com