我使用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());
}
我在这里尝试做的是否有用??
问候 阿里安
答案 0 :(得分: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();