我從樹形圖填充我的表格,並且樹形圖正從我的sql數據庫填充,錯誤是數據出現在我的表格中的順序。 我發佈了一個截圖給你看。JTable沒有填充正確的信息
,這裏是從我的SQL
public class LibraryData {
private Connection con;
private Statement stmt;
private ResultSet res;
public LibraryData() {
try {
// con = DriverManager.getConnection("jdbc:mysql://;localhost:3306")
String connectionUrl = "jdbc:sqlserver://SQL-SERVER;" + "databaseName=oe323;integratedSecurity=true;";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(connectionUrl);
} catch (Exception ex) {
System.out.println("Erro: " + ex);
}
}
public String listAll1() {
String output = "";
try {
stmt = con.createStatement();
res = stmt.executeQuery("SELECT * FROM LibraryTable");
while (res.next()) { // there is a result
// the name field is the thrid one in the ResultSet
// Note that with ResultSet we count the fields starting from 1
output += res.getString(1) + " " + res.getString(2) + " - "
+ res.getString(3) + " " + res.getString(4) + " "
+ res.getString(5) + "\n";
System.out.println(output);
library.put("" + res.getInt(1), new Item(res.getString(2), res.getString(3), Integer.parseInt(res.getString(4))));
}
} catch (Exception e) {
System.out.println(e);
return null;
}
return output;
}
public static class Item {
Item(String n, String a, int r) {
name = n;
artist = a;
rating = r;
}
// instance variables
private String name;
private String artist;
private int rating;
private int playCount;
public String toString() {
return name + " - " + artist;
}
}
private static Map<String, Item> library = new TreeMap<String, Item>();
//
static {
}
public static String listAll() {
String output = "";
Iterator iterator = library.keySet().iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
Item item = library.get(key);
output += key + " " + item.name + " - " + item.artist + "\n";
}
return output;
}
public static void fillTable() {
Iterator iterator = LibraryData.library.keySet().iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
LibraryData.Item item = LibraryData.library.get(key);
DeleteSong.data = new Object[1][3];
DeleteSong.data[0][0] = key;
DeleteSong.data[0][1] = item.artist;
DeleteSong.data[0][2] = item.name;
DeleteSong.modeltable.addRow(DeleteSong.data[0]);
}
}
public static void fillTable1() {
// listAll1();
Iterator iterator = LibraryData.library.keySet().iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
LibraryData.Item item = LibraryData.library.get(key);
CheckLibrary.data = new Object[1][4];
CheckLibrary.data[0][0] = key;
CheckLibrary.data[0][1] = item.artist;
CheckLibrary.data[0][2] = item.name;
CheckLibrary.modeltable.addRow(CheckLibrary.data[0]);
CheckLibrary.data[0][3] = item.rating;
也許你忘了在表格中添加藝術家名字了嗎?所有其他列都顯示在錯誤的地方。如果有些顯示一些代碼 –
只是意識到藝術家和曲目名稱是錯誤的方式,特別是你想看到什麼代碼的一部分 – Reynolds
相關的代碼,代碼顯示我們你的錯誤。 –