2012-02-12 2 views
1

Java로 데이터베이스와의 연결을 만들었으며 두 테이블의 데이터를 표시하려고합니다.Java에서 두 테이블의 값 표시

쿼리 문에서 JOIN 명령을 사용했지만 구문 오류로 고생하고 있습니다. 이 문제에 대한 조언이 필요했습니다.

try 
    { 
     Class.forName(driverName); 
     connection = DriverManager.getConnection(SourceURL, user, password); 


     Statement listDisplay = connection.createStatement(); 
     ResultSet displayAll = listDisplay.executeQuery("SELECT AnimalType.typeID, AnimalType.description, Animal.name " 
                +"FROM Animal " 
                +"JOIN AnimalType "  
                +"ON AnimalType.typeID = Animal.typeIDForeign"); 
     while(displayAll.next()) 
     { 
      int typeId = displayAll.getInt(1); 
      String description = displayAll.getString(2); 
      String name = displayAll.getString(3); 

      System.out.println(typeId + " " + description + " " + name); 
     } 

     connection.close(); 

     } 
     catch(SQLException sql) 
     { 
      JOptionPane.showMessageDialog(null, sql.toString()); 
     } 
     catch(ClassNotFoundException exe) 
     { 
      JOptionPane.showMessageDialog(null, exe.toString()); 
     } 

내가 여기서하려고하는 것이 작동 할 것인가?

관해서는 나는 일반적으로 다음과 같이 다소 그것을 할

+2

스택 추적을 게시하시기 바랍니다. – Paul

+0

명령 줄 앱에서 같은 오류를 재현 할 수 있으므로 GUI가 없다면 Swing과 관련이 없습니다. –

+0

예. 감사. 쿼리와 약간의 변경을 가하고 지금은 괜찮습니다 ... 감사합니다. ("SELECT AnimalType.typeID, AnimalType.description, Animal.name"+ "AnimalType, Animal"+ "AnimalType.typeID = Animal.typeIDForeign"); – Arianule

답변

1

아리안 :

if (displayAll.first()) 
{ 
    do 
    { 
     int typeId = displayAll.getInt(1); 
     String description = displayAll.getString(2); 
     String name = displayAll.getString(3); 

     System.out.println(typeId + " " + description + " " + name); 
    } while(displayAll.next()); 
} 
displayAll.close(); 
listDisplay.close();