使用数据库内容填充SWT表

时间:2014-03-20 05:23:42

标签: java mysql swt

我正在使用PhpMyAdmin将数据保存在数据库中。我有一个SWT表来填充数据库内容。

这是我的代码..

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();
    }
}

它有效。

现在我在swt表中显示数据库内容时遇到了一些问题。我的程序做的是,它在一行中设置所选的(由上面的程序中的限制定义)DB的内容(逐个方式),但我希望在SWT表的下一行中列出下一行DB表。你能就此提出一些建议吗? ! Screenshot of my swtTable

1 个答案:

答案 0 :(得分:1)

看起来应该是这样的:

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();
    }
}