gpt4 book ai didi

java - JTable 中第一个不必要的空行。如何去除它?

转载 作者:行者123 更新时间:2023-11-30 04:49:30 26 4
gpt4 key购买 nike

当我编译程序时,它生成第一行,该行为空。我认为这是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);

最佳答案

        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。

关于java - JTable 中第一个不必要的空行。如何去除它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10162071/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com