简体   繁体   中英

java and sql into JList

What is the best way to get my mysql data into a JList. I can connect to the database and output the results. I've been looking at various ways online but not getting the desired results. Instead of posting any code, are there any great tutorials showing how this is done, or any code snips.

    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);
        }
    }       
}

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 .

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 .

Mybatis! or Hibernate - for the shortest answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM