user1061666
user1061666

Reputation:

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

Upvotes: 1

Views: 968

Answers (3)

trashgod
trashgod

Reputation: 205775

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.

Upvotes: 4

The Cat
The Cat

Reputation: 2475

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 .

Upvotes: 2

r0ast3d
r0ast3d

Reputation: 2637

Mybatis! or Hibernate - for the shortest answer.

Upvotes: 0

Related Questions