mix
mix

Reputation: 137

how to get data from mysql to jList?

I'm new in java. how can I get the data from mysql and display it in Jlist (Jlist name: JLISTF) I'm confused with vector and the model.

Below is the photo of my JFRAME

the yellow part means JLIST and it's name is JLISTF I want to display the data from mysql

Thank youenter image description here

code:

public class Inventory extends javax.swing.JFrame {


        Connection connect = null;
        ResultSet rs = null;
        PreparedStatement pst = null;
        ResultSet rs2 = null;


    public void populateJList(JList ItemList, String query, Connection connection) throws SQLException
{
    DefaultListModel model = new DefaultListModel(); //create a new list model

    Statement statement = connection.createStatement();
    ResultSet resultSet = statement.executeQuery(query); //run your query

    while (resultSet.next()) //go through each row that your query returns
    {
        String ItemList2 = resultSet.getString("ItemCode"); //get the element in column "item_code"
        model.addElement(ItemList2); //add each item to the model
    }
    ItemList.setModel(model);

    resultSet.close();
    statement.close();

}


public Inventory() {..}

  private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) { ......}

private void saveButton3ActionPerformed(java.awt.event.ActionEvent evt) {
String inventcodef = inventCodeField.getText();
try{.........}
catch()
{...........}
}

Upvotes: 2

Views: 17517

Answers (1)

Lunchbox
Lunchbox

Reputation: 1633

Assuming you have a connection to your database and you know what information you want to query, you can use the following method to populate your JList.

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.DefaultListModel;
import javax.swing.JList;

...

public void populateJList(JList list, String query, Connection connection) throws SQLException
{
    DefaultListModel model = new DefaultListModel(); //create a new list model

    Statement statement = connection.createStatement();
    ResultSet resultSet = statement.executeQuery(query); //run your query

    while (resultSet.next()) //go through each row that your query returns
    {
        String itemCode = resultSet.getString("item_code"); //get the element in column "item_code"
        model.addElement(itemCode); //add each item to the model
    }
    list.setModel(model);

    resultSet.close();
    statement.close();

}

You would pass your JLISTF variable into this function as the first parameter.

The following line assumes that your column name is "item_code", you will want to replace this with your columns actual name.

String itemCode = resultSet.getString("item_code");

This function creates a new Model (see How to Use Models), then executes your query. It gets each value that your query returns and adds it to the new model. list.setModel(model) then tells the list to start using your new model.

Upvotes: 4

Related Questions