列表中的SQL异常

时间:2015-01-07 15:39:15

标签: java database sqlexception

我正在用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某处出现错误,但我似乎无法找到它。在我试图自己解决问题时,任何帮助,即使只是轻推,都会非常感激。

1 个答案:

答案 0 :(得分:5)

您的查询一起运行,在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 = ? "