2

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()
{...........}
}
mix
  • 137
  • 2
  • 3
  • 12
  • Do you [mean javax.swing.JList](http://docs.oracle.com/javase/6/docs/api/javax/swing/JList.html)? What exactly do you not understand - MySQL database integration or Swing based UI implementation? – home Feb 23 '12 at 16:48
  • I want to populate JList to mysql but I don't know how to start – mix Feb 23 '12 at 23:16
  • @home I don't know what variables I will replace because I really don't understand how to populate JList. There are tutorials but they don't really explain the variables used. For example, In the link that you gave where I will replace the code with my JLIST variable name which is JLISTF – mix Feb 23 '12 at 23:26
  • What is JLISTF? Please show the relevant parts of the code you have so far. – home Feb 24 '12 at 06:07
  • @home I edited my question, please see the photo. I don't have any codes for JLIST since I don;t know how to start . Please help me. I'm stack for the past few weeks – mix Feb 24 '12 at 06:26
  • please learn java naming conventions and stick to them – kleopatra Feb 25 '12 at 10:25

1 Answers1

4

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.

Lunchbox
  • 1,603
  • 11
  • 14
  • Thank you so much @lunchbox for clear explanation for a beginner like me. I'll try this out :) – mix Feb 24 '12 at 08:28
  • I changed item_code to the data that I want to retrieve but still it doesn't display in my JList. Do I need to add someething like in formWindowOpened like in Jcombobox? – mix Feb 24 '12 at 10:04
  • Yes, you will need to add a line similar to this `populateJList(JLISTF, "SELECT item_code FROM your_table", connection);` somewhere after you create JLISTF and after you create your Connection. – Lunchbox Feb 24 '12 at 16:16
  • somewhere inside the class? or inside the savebutton action? :) – mix Feb 24 '12 at 18:31
  • 1
    It depends on your code. You just need to make sure that you put that code somewhere that's going to be executed. So if you want the list to populate when you click your save button, then yes, put it in your savebutton action. If you want it to populate before you show your form, then put it in your constructor or in the line before you call `setVisible(true);` If you post your code we can tell you where to put it. – Lunchbox Feb 24 '12 at 19:32
  • I don't see your JLISTF variable anywhere in the code you posted. The relevant part of your code would be where you call something similar to `JList JLISTF = new JList()`. My assumption is that you can call `populateJList()` at the end of your constructor `public Inventory() {..}` – Lunchbox Feb 28 '12 at 22:03