我想将来自JAVA DB的三列数据放入一个JList中。当我运行程序时,我会得到一个列表-但是它们都是数据所在位置的指针!我尝试了几件事,陷入僵局。
//这是我班上的方法
public void getNames()
throws SQLException
{
try
{
// Create a Statement object for the query.
Statement stmt =
conn.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
System.out.println("connecting to db for getNames()"); // This is for DEBUGGING
// Execute the query.
ResultSet rst = stmt.executeQuery(
"SELECT first_name, last_Name, person_id FROM person_info ORDER BY last_name");
// Get the number of rows
rst.last(); // Move to last row
int numRows = rst.getRow(); // Get row number
rst.first(); // Move to first row
// Create an array for the names.
personData = new Object[numRows][3];
// Store the columns in the personData array.
for (int row = 0; row < numRows; row++)
{
for (int col = 0; col < 3; col++)
{
personData[row][col] = rst.getObject(col + 1);
}
// Go to the next row in the ResultSet.
rst.next();
}
// Close the connection and statement objects.
conn.close();
stmt.close();
System.out.println("Printing" + Arrays.deepToString(personData)); // This is for DEBUGGING
}
catch (SQLException ex)
{
System.out.println("Hell:" + ex);
}
}
//This is the method to get the above data
public Object[][] getPersonData()
{
System.out.println("getPersonData()" + personData); // This is for DEBUGGING
return personData;
}
///这就是我所说的并构建Jlist JList nameList;
private void buildListPanel()
{
try
{
System.out.println("Building Panel"); // This is for DEBUGGING
// Create a panel.
listPanel = new JPanel();
// Add a titled border to the panel.
listPanel.setBorder(BorderFactory.
createTitledBorder("Select a Name"));
// Create an AddressBookManager object.
pbManager = new AddressBookManager();
pbManager.getNames();
// Get the table data.
Object[][] data = pbManager.getPersonData();
System.out.println("build list panel" + Arrays.deepToString(data)); // This is for DEBUGGING
// Create a JList object to hold the names.
nameList = new JList<>(data);
// Set the number of visible rows.
nameList.setVisibleRowCount(5);
// Put the JList object in a scroll pane.
scrollPane = new JScrollPane(nameList);
// Add the scroll pane to the panel.
listPanel.add(scrollPane);
}
catch(SQLException ex)
{
// If something goes wrong with the database,
// display a message to the user.
JOptionPane.showMessageDialog(null, ex.toString());
}
}
我似乎看不到要显示的结果的图片
I have a list of names but what appears is a list of these:
[Ljava.lang.Object;@9225652
[Ljava.lang.Object;@654f0d9c
[Ljava.lang.Object;@6a400542
[Ljava.lang.Object;@658ocfdd
[Ljava.lang.Object;@7e0b85f9