【发布时间】:2021-07-20 11:26:46
【问题描述】:
我有一个项目管理系统,用于从文本文件中读取数据,但现在正在更改为从数据库中读取数据。
我试图弄清楚如何根据项目表中的项目编号在不同的表中显示特定信息。 我数据库中的所有表都与这个项目表相关。
我的数据库的可视化展示
我的问题
整齐地显示项目的所有信息。
我想弄清楚的是,如何根据项目表中的外键在一个表中获取特定记录?
例如,如果一个项目有一个建筑师名为“Bryan”的建筑师,那么如何仅显示“Bryan”的建筑师详细信息?
我的 displayAll 方法的代码
public static void viewAllProjects() {
// Calling the 'connectionAttempt' method to connect to the 'ebookstore' database.
Connection connection = connectionAttempt();
PreparedStatement ps;
// Using a try- catch block to print the data for a specific record in the 'books' table.
try {
// Creating and using a normal statement to select the specific record in the database.
Statement statement = connection.createStatement();
// SQL select statement
String q = "select * from architect\n" +
"union all\n" +
"select * from contractor\n" +
"union all\n" +
"select * from customer" +
"union all\n" +
"select * from shopping_basket\n" +
"union all\n" +
"select * from projects\n" +
"union all\n" +
"select * from building_info";
ResultSet rs = statement.executeQuery(q); // Executing statement
// Printing ResultSet
if (rs.next()) {
String projectDetails = "\nProject Number: " + rs.getString("project_number") + "\n" +
"Project Name: " + rs.getString("project_name") + "\n" +
"Type of building: " + rs.getString("HERE") + "\n" +
"ERF Number: " + rs.getString("HERE") + "\n" +
"Physical Address: " + rs.getString("physical_address") + "\n" +
"Total fee charged for project: " + rs.getString("HERE") + "\n" +
"Total amount already paid: " + rs.getString("HERE") + "\n" +
"Deadline of the project: " + rs.getString("deadline") + "\n" +
"Project Finalised: " + rs.getString("finalised") + "\n" +
"\nArchitect Name: " + rs.getString("architect_name") + "\n" +
"Architect Telephone Number: " + rs.getString("id") + "\n" +
"Architect Email-Address: " + rs.getString("id") + "\n" +
"Architect Physical Address: " + rs.getString("id") + "\n" +
"\nContractor Name: " + rs.getString("contractor_name") + "\n" +
"Contractor Telephone Number: " + rs.getString("id") + "\n" +
"Contractor Email-Address: " + rs.getString("id") + "\n" +
"Contractor Physical Address: " + rs.getString("id") + "\n" +
"\nCustomer Name: " + rs.getString("HERE") + "\n" +
"Customer Telephone Number: " + rs.getString("HERE") + "\n" +
"Customer Email-Address: " + rs.getString("HERE") + "\n" +
"Customer Physical Address: " + rs.getString("HERE") + "\n" +
"----------------------------------\n";
System.out.println(projectDetails);
} else {
System.out.println("Record Not Found...");
}
connection.close();
} catch (Exception e) {
System.out.println(e);
}
}
connectionAttempt 方法的代码可以正常工作,就像我之前使用的那样。
如果我可以提供任何其他详细信息,请告诉我。
【问题讨论】: