如何打印加入JDBC的3个表中的列名?

问题描述 投票:0回答:1

我加入了3个表,此查询将执行并打印出表数据。

try{
    Connection conn = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD);
    System.out.println("Connected");
    Statement st = conn.createStatement();

    String queary = "SELECT s.*, sup.name as supplierName , p.name as partName "+
                    "FROM supplies s "+
                    "INNER JOIN supplier sup on s.supplierNum = sup.supplierNum "+
                    "INNER JOIN parts p on s.partNum = p.partNum";


    ResultSet rs = st.executeQuery(queary);

    while(rs.next()) {
        System.out.println(rs.getString("supplierNum"));
        System.out.println(rs.getString("partNum"));
        System.out.println(rs.getString("quantity"));
        System.out.println(rs.getString("supplierName"));
        System.out.println(rs.getString("partName"));
        space();
        }

}catch(Exception ex){
    System.out.println(ex);
}

但是我试图添加列名,而不是控制台打印enter image description here

它将打印级联的列名

供应商编号:S1

partNum:P1

数量:300

名称:史密斯

部分:坚果

java jdbc relational-database
1个回答
0
投票
while(rs.next()) {
    System.out.println("Supplier Name " + rs.getString("supplierNum"));
    System.out.println("Part Name "+rs.getString("partNum"));
    System.out.println("Quantity "+ rs.getString("quantity"));
    System.out.println("SupplierName "+rs.getString("supplierName"));
    System.out.println("PartName "+rs.getString("partName"));
    space();
    }
© www.soinside.com 2019 - 2024. All rights reserved.