我使用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());
        }


它会在我这里尝试做的工作吗?

问候
阿里安

最佳答案

我通常这样做是这样的:

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();

10-08 09:02