简体   繁体   English

用数据库内容填充SWT表

[英]populate SWT table with database content

I am using PhpMyAdmin to save my data in database. 我正在使用PhpMyAdmin将数据保存在数据库中。 I have a SWT table to populate with database content. 我有一个SWT表来填充数据库内容。

here is my code.. 这是我的代码。

public static void fetchDatafromDB(String StartIndex, String FinalIndex) {
    try {
        Class.forName(GlobalVariables.SQL_driver).newInstance();
        Connection conn = DriverManager.getConnection(GlobalVariables.DB_url + GlobalVariables.DB_name, GlobalVariables.DB_Username, GlobalVariables.DB_password);
        Statement st = conn.createStatement();
        String query = "SELECT  `From`, `To`, `IDno`, `TimeStamp` FROM `callsheet` WHERE TimeStamp BETWEEN '" + StartIndex + "' AND '" + FinalIndex + "'";
        ResultSet rs = st.executeQuery(query);
        java.sql.ResultSetMetaData rsmd = rs.getMetaData();
        int columnsNumber = rsmd.getColumnCount();
        while (rs.next()) {
            for (int i = 1; i <= columnsNumber; i++) {
                // System.out.print(rs.getString(i));
                item.setText(i, rs.getString(i));
            }
            // System.out.println();
        }
    } catch (Exception P) {
        P.printStackTrace();
    }
}

it worked. 有效。

Now I am getting some problem with tabling the DB content in my swt table. 现在我在将swt表中的数据库内容制表时遇到了一些问题。 What my program does, is that, it sets the selected (defined by limit in program above) content of DB in one row (one by one manner) but I want the next row of DB table to be tabled in next row of SWT table. 我的程序要做的是,它将一行(一一对应)设置数据库的选定内容(由上述程序中的限制定义),但我希望将数据库表的下一行记录在SWT表的下一行中。 Could you suggest something about this? 您能提出一些建议吗? ! Screenshot of my swtTable 我的swtTable的屏幕截图

It should look something like this: 它看起来应该像这样:

public static void fetchDatafromDB(String startIndex, String finalIndex) {
    try {
        Class.forName(GlobalVariables.SQL_driver).newInstance();
        Connection conn = DriverManager.getConnection(GlobalVariables.DB_url + GlobalVariables.DB_name, GlobalVariables.DB_Username, GlobalVariables.DB_password);
        Statement st = conn.createStatement();
        String query = "SELECT  `FROM`, `To`, `IDno`, `TimeStamp` FROM `callsheet` WHERE TimeStamp BETWEEN '" + startIndex + "' AND '" + finalIndex + "'";
        ResultSet rs = st.executeQuery(query);
        java.sql.ResultSetMetaData rsmd = rs.getMetaData();
        int columnsNumber = rsmd.getColumnCount();

        TableItem item;
        while (rs.next()) {
            // Create a new TableItem for each entry in the result set (each row)
            item = new TableItem(table, SWT.NONE);
            for (int i = 1; i <= columnsNumber; i++) {
                // Populate the item (mind the index!!)
                item.setText(i - 1, rs.getString(i));
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM