简体   繁体   English

如何从mysql获取数据到jList?

[英]how to get data from mysql to jList?

I'm new in java. 我是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. 我如何从mysql获取数据并将其显示在Jlist(Jlist名称:JLISTF)中,我对向量和模型感到困惑。

Below is the photo of my JFRAME 以下是我的JFRAME的照片

the yellow part means JLIST and it's name is JLISTF I want to display the data from mysql 黄色部分表示JLIST,它的名称是JLISTF。我想显示mysql的数据

Thank you 谢谢 在此处输入图片说明

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()
{...........}
}

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. 假设您已连接到数据库,并且知道要查询的信息,则可以使用以下方法填充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. 您将把JLISTF变量作为第一个参数传递到此函数中。

The following line assumes that your column name is "item_code", you will want to replace this with your columns actual name. 下面的行假定您的列名是“ item_code”,则将其替换为列的实际名称。

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. 然后list.setModel(model)告诉列表开始使用新模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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