我想从数据库表中提取数据并显示在JTable
中的表格中
以下是我想在数组dataValues [] []
中添加数据库库中的数据的一些代码
在此i中存储dataValues [][]
静态值,但我想在数据库表中存储dataValues[][]
中的值。
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection connection = DriverManager.getConnection(url, username, password);
Statement statement = connection.createStatement();
String query = "select command_name, omc_name, to_module, start_time, end_time, status, priority, cmd_id from sync_task_table";
ResultSet resultset = statement.executeQuery(query);
dm=new DefaultTableModel();
String columnNames[] = { "Column 1", "Column 2", "Column 3" };
// Create some data
String dataValues[][] = {
{ "12", "234", "67" },
{ "-123", "43", "853" },
{ "93", "89.2", "109" },
{ "279", "9033", "3092" }
};
// Create a new table instance
table = new JTable( dataValues, columnNames );
// Add the table to a scrolling pane
scrollPane = new JScrollPane( table );
答案 0 :(得分:3)
答案 1 :(得分:3)
首先,您应该以首选的JDBC方式注册驱动程序
Class.forName("oracle.jdbc.driver.OracleDriver");
// instead of
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
现在是你问题的答案。
通过以下方式初始化dataValues:
String columnNames[] = { "Column 1", "Column 2", "Column 3" };
List<Object[]> dataValueList = new ArrayList();
while(resultset.next()){
Object[] nextRow = new Object[columnNames.length];
for(int col = 0; col < columnNames.length; col++){
String colName = columnNames[col]
Object colValue = resultset.getObject(colName);
// do type conversion here - e.g. sql type to string
nextRow[col] = colValue;
}
dataValueList.add(nextRow);
}
Object[][] dataValues = (Object[][]) dataValueList.toArray(new Object[dataValueList
.size()][columnNames.length]);