2015-01-07 3 views
0

저는 (가상의) 학생 데이터가 포함 된 매우 기본적인 데이터베이스 인 Java로 작은 애플리케이션을 프로그래밍하고 있습니다. 지금까지 조용하게 작동하지만, 학생들이 자주 다니는 대학의 커리큘럼이 포함 된 목록을 만들 때 문제가 발생했습니다. 가능한 모든 학위를 나열하고 싶습니다.이 중 6 학기는 모든 범주에서 수강 할 수있는 코스와 함께, 주당 몇 시간 씩 걸릴지 등등입니다. 첫 번째 비트가 작동하지만 각 학기마다 각 카테고리별로 다른 카테고리를 채우려 할 때 문제가 발생합니다. Heres는 코드 (나는 별 2 개 동봉하는 문제와 관련된하다고 생각하는 것들) :리스팅의 SQL 예외

@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

"FROM MODULE m, STUDIES st, CURRICULUM c" + "WHERE st.SKABB = ? AND c.SEM = ? " 

이 될 것이다. –

답변

5

쿼리가 실행 중일 때 c 뒤에 공백을 넣으십시오. 당신은`교과 과정 c` 뒤에 공백 누락

"FROM MODULE m, STUDIES st, CURRICULUM c " + // Note space 
"WHERE st.SKABB = ? AND c.SEM = ? " 
+0

아,이 질문에 댓글을 달았습니다. –

+0

지금은 바보 같아. 해결 됐어. 고맙습니다. –

+0

멍청한 느낌이 들지 마라. 빈번히 발생하고 때로는 다른 눈 쌍이 필요할 때가있다. 행운을 빌어 요. – Todd