如何在JTable(JAVA)中显示FK ID的引用

时间:2019-06-14 21:22:36

标签: java mysql swing jtable

enter image description here嗨,

我的数据库中有多个表,其中一些表与employee_details表有关系,现在,我想显示从employee_details表到JTable的记录。但我希望值FK ID字符串而不是实际ID。

  

公共无效DBTable(){

    try {
        String InsertToTable = "Select EmpDetails_ID, First_Name, Last_Name, Gender, Position_ID, Department_ID, "
                + "Office_Location_ID, Employee_Status, Office_Number, Mobile_Number, Email_Address, Remarks, Image From employee_details";
        pst = con.prepareStatement(InsertToTable);
        rs= pst.executeQuery();
        EmpDBTable.setModel(DbUtils.resultSetToTableModel(rs));
    } catch (SQLException ex) {
        Logger.getLogger(Employee_Details.class.getName()).log(Level.SEVERE,null,ex);
    }
}

1 个答案:

答案 0 :(得分:0)

我修改了我在网上找到的示例代码。我下载了rs2xml.jar以获得“ DbUtils.resultSetToTableModel(rs)”。我在本地sql服务器中创建了表employee_details和department。我使用了我给您的SQL Join示例,并且能够在JTable上获得该额外的列。请将此示例与您自己的代码进行比较。在这段代码中,我得到以下结果: enter image description here

下面是代码(从在线示例中修改):

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.*;
import java.awt.event.*;
import com.microsoft.sqlserver.jdbc.SQLServerDriver;
import net.proteanit.sql.DbUtils;

public class SearchResult {
JFrame frame, frame1; 
JPanel panel;
static JTable table;

String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://localhost;instance=DESKTOP-8QFTIP4;databaseName=deneme;user=sa;password=******";
String userName = "sa";
String password = "*****";    

public void createUI()
{
    showTableData();
} 
public void showTableData()
{

    frame1 = new JFrame("Database Search Result");
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame1.setLayout(new BorderLayout()); 
    table = new JTable(); 
    table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    table.setFillsViewportHeight(true);
    JScrollPane scroll = new JScrollPane(table);
    scroll.setHorizontalScrollBarPolicy(
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    scroll.setVerticalScrollBarPolicy(
            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 
    try
    {
        Class.forName(driverName);
        System.out.println("conn before");
        Connection con = DriverManager.getConnection(url, userName, password);
        System.out.println("conn after");
        String sql ="Select EmpDetails_ID, First_Name, Last_Name, Gender, Position_ID, "
                + " employee_details.Department_ID,department.Department_Name , Office_Location_ID, Employee_Status, "
                + " Office_Number, Mobile_Number, Email_Address, Remarks, Image "
                + " From employee_details inner join department on employee_details.Department_ID = department.Department_ID";
        PreparedStatement ps = con.prepareStatement(sql);
        System.out.println("about to execute");
        ResultSet rs = ps.executeQuery();
        int i =0;
        System.out.println("after to execute"); 
        table.setModel(DbUtils.resultSetToTableModel(rs));

    }
    catch(Exception ex)
    {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(null, ex.getMessage(),"Error",
                JOptionPane.ERROR_MESSAGE);
    }
    frame1.add(scroll);
    frame1.setVisible(true);
    frame1.setSize(400,300);
}

public static void main(String args[])
{
    SearchResult sr = new SearchResult();
    sr.createUI();
}
}