如何使用数据库内容填充SWT表?

时间:2014-03-19 09:50:07

标签: java mysql swt

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

我试过下面的代码。它在控制台中完美地打印服务器数据,但它不会填充在我的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();
    }
}

我在setText()行收到错误。当我试图在TextField(SWT)中打印ID no(RFID)时,发生了类似的事情。

请问您能为上述问题提供一些具体的解决方案吗?

发生以下错误。

java.lang.NullPointerException
at java2MySql.fetchDatafromDB(java2MySql.java:200)
at vehicleUserLog.NewVehicleCallSheet$6.widgetSelected(NewVehicleCallSheet.java:618)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:248)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1057)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4170)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3759)
at vehicleUserLog.NewVehicleCallSheet.open(NewVehicleCallSheet.java:167)
at java2MySql.ReadFromDB(java2MySql.java:72)
at java2MySql.SignInParser(java2MySql.java:107)
at ReportWindow.verifyfromDB(ReportWindow.java:294)
at ReportWindow$1.widgetSelected(ReportWindow.java:211)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:248)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1057)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4170)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3759)
at ApplicationLauncher.<init>(ApplicationLauncher.java:18)
at ApplicationLauncher.main(ApplicationLauncher.java:30) 

0 个答案:

没有答案