有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JTable未填充正确的信息

我从树映射填充表,而树映射从sql数据库填充,错误是数据在表中的显示顺序。 我已经发布了一个截图给你看

enter image description here

这是我的sql中的图像

enter image description here

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;

共 (1) 个答案

  1. # 1 楼答案

    问题是您的键实际上是数字,您希望按数字顺序对它们进行排序,但您的library键是String的,它们按字典顺序排序,例如,1、10、11、12、13、14、15、16、17、18、19、2、20(就像a、aa、ab、ac、ad、b、ba、bb、bc、)。这种顺序在屏幕截图中很明显

    这是因为,出于某种原因,您决定在库映射中放置时将整数键转换为字符串:

    private static Map<String, Item> library = new TreeMap<String, Item>();
    ... 
    // in listAll1() and elsewhere:
    library.put("" + res.getInt(1), ...
    

    您应该使用适合所存储信息的数据类型。如果密钥是整数,请使用整数,例如:

    private static Map<Integer, Item> library = new TreeMap<Integer, Item>();
    ...
    // in listAll1() and elsewhere:
    library.put(res.getInt(1), ...
    

    你有一个屏幕截图显示了这一点,但没有代码。然后您添加了代码,但删除了屏幕截图。我在你的问题中添加了屏幕截图。将来,请尝试发布与您看到的问题直接相关的代码和信息