1

当我编译我的程序时,它是第一行,它是空的。我认为这是Java中的默认设置。如何删除它?这是一个 MySQL 数据库客户端程序,我们可以在其中查看数据库中的数据。但这第一行很烦人。

String dbtime;
String query = "Select * FROM EMP";
String[][] celDatas = null;
String[] celNames = null;
try {
    // Load the JDBC driver
    String driverName = "org.gjt.mm.mysql.Driver"; // MySQL MM JDBC driver
    Class.forName(driverName);

    // Create a connection to the database
    String serverName = "localhost";
    String mydatabase = "TestyNaukalne";
    String url = "jdbc:mysql://" + serverName +  "/" + mydatabase; // a JDBC url
    String username = "root";
    String password = "";
    connection = DriverManager.getConnection(url, username, password);

    Statement stmt = connection.createStatement();
    ResultSet rs = stmt.executeQuery(query);
    ResultSetMetaData rsmd = rs.getMetaData();

    int NumOfCol = rsmd.getColumnCount();
    rs.last();
    int rowCount = rs.getRow();
    rs = stmt.executeQuery(query);
    celNames = new String[NumOfCol];
    celDatas = new String[rowCount+1][NumOfCol];

    for(int weq=1; weq<=NumOfCol; weq++) {
        System.out.println(rsmd.getColumnName(weq));                   
        celNames[weq-1] = rsmd.getColumnName(weq);
        int tmp = 1;
        while (rs.next()) {
            dbtime = rs.getString(weq);
            System.out.println(dbtime);
            celDatas[tmp][weq-1] = dbtime;
            tmp++;
        } //end while
        rs = stmt.executeQuery(query);                   
        System.out.println();
    }
} catch (ClassNotFoundException e) {
    e.printStackTrace();
} catch (SQLException e) {
    e.printStackTrace();
}

final JTable source = new JTable(celDatas,celNames);
        JScrollPane pane = new JScrollPane(source);
        pane.setSize(f.getSize().width-60,300);
        pane.setLocation(30,20);
4

1 回答 1

2
        int tmp = 1; // ******
        while (rs.next()) {
           dbtime = rs.getString(weq);
           System.out.println(dbtime);
           celDatas[tmp][weq - 1] = dbtime;
           tmp++;
        } // end while

数组基于 0,使数组的 celDatas[0] 行为空。解决方案:不要那样做;将 tmp 初始化为 0。

于 2012-04-15T12:51:56.083 回答