1

将我的 mysql 数据放入 JList 的最佳方法是什么?我可以连接到数据库并输出结果。我一直在网上寻找各种方法,但没有得到想要的结果。除了发布任何代码之外,是否有任何很棒的教程来展示这是如何完成的,或者任何代码片段。

    try {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection
        ("jdbc:mysql://www", "....", "....");

        st = con.createStatement();

        } catch (Exception ex) {
        System.out.println("error" + ex);
        }
        try {           

            String query = "select * from demo";
            rs = st.executeQuery(query);
            System.out.println("Records from Database");
            while(rs.next()) {                      
            String number  = rs.getString("number");
            System.out.println(number);         

            }
        } catch (Exception ex) {

        System.out.println("error" + ex);
        }
    }       
}
4

3 回答 3

4

It looks like you understand how to query the database using JDBC. Now you just need to use the results to construct your preferred ListModel, as shown in How to Use Lists.

于 2012-10-22T16:26:24.603 回答
2

I use a spring class called SimpleJdbcTemplate that you can use to query the database. Its fairly lightweight but avoids having to set up the XML mappings needed in Mybatis! or Hibernate.

Here is a fairly comprehensive tutorial .

于 2012-10-22T16:26:02.317 回答
0

米巴蒂斯!或休眠 - 最短的答案。

于 2012-10-22T16:13:04.360 回答