如何在netbeans中从jlist中选择项目名?

7xzttuei  于 2021-06-17  发布在  Mysql
关注(0)|答案(2)|浏览(367)

我一直在尝试从mysql中检索数据 list 这很好,但问题是我不能选择 namelist .. 我要选择 name (不是索引或值)以便我可以完成where子句
这是我的密码:-

private void proceed(){

    PreparedStatement stmt = null;
    Connection conn = null;
    ResultSet rs=null;

    String i = jList1.getSelectedValue();

    try    {

     Class.forName("java.sql.DriverManager");

     conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/hotel","root","root");

      stmt = conn.prepareStatement("select * from hotelbookings where GuestName = '"+i+"'");

    rs = stmt.executeQuery();

    if (rs.next()){

    String BN = rs.getString("BookNo");
    String GN = rs.getString("GuestName");
    String AD = rs.getString("Address");
    String NOD = rs.getString("No_of_Days");
    String PN = rs.getString("PhoneNo");
    String ID = rs.getString("ID_Proof");
    String CN = rs.getString("Country");
    String ARD = rs.getString("Arival_Date");
    String DRD = rs.getString("Departure_Date");

    NewJFrame1_1 second = new NewJFrame1_1(BN,GN,AD,NOD,PN,ID,CN,ARD,DRD);
    second.setVisible(true);

    }

} catch(Exception e){
 JOptionPane.showMessageDialog(null, e);
}

}

这是我的列表的图像..显示列表从mysql检索数据..继续按钮用于获取所选名称进行修改
这是节目单的代码button:-

private void fillList(){

        PreparedStatement stmt = null;
        Connection conn = null;

         try    {
         Class.forName("java.sql.DriverManager");

         conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/hotel","root","root");

          stmt = conn.prepareStatement("select GuestName from hotelbookings");

         stmt.executeQuery();

             ResultSet rs = stmt.getResultSet();
             int i =0;
             DefaultListModel info = new DefaultListModel();

             while (rs.next()){
                 String[] data = new String[100];
                 data[i] = rs.getString("GuestName");
                 jList1.setModel(info);
                 info.addElement(data[i]);
                 i = i + 1;
                 jList1 = new JList(info);
             }

}
      catch(Exception e){

                JOptionPane.showMessageDialog (this, e.getMessage());
}

    }

填充列表class:-
私有void jbutton2actionperformed(java.awt.event.actionevent evt){

fillList();

}

l7wslrjt

l7wslrjt1#

嗯,最基本的问题是 fillList 据我所知,从你张贴的代码是while循环。你创建了一个新的 JList 每个循环。这将使无效(或我应该说肮脏)的 jList1 你的参考资料 proceed 方法。您不应该创建新的 JList 每一次。只需为每个循环保留相同的列表。甚至不要在循环外改变它。保持不变,每次都更新模型。
要更新模型,至少有以下两个选项:
每次创建一个新模型,更新它(即添加你想要的所有元素)并调用 jList1.setModel(your_created_model) .
设置模型 JListDefaultListModel 一次(以施工时间为例),然后每次 fillList 方法调用:
从列表本身获取模型(通过 getModel() ). 你知道这是一个 DefaultListModel 所以你可以放心地把它扔到一个。
从模型中删除所有元素(vis) removeAllElements() ).
从中添加所有新元素 ResultSet 到模型。
以以下代码为例:

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

public class Main {
    public static void fillList(final JList<String> list) {
        final DefaultListModel<String> model = (DefaultListModel<String>) list.getModel(); //Step 1: Get the model and cast it to DefaultListModel.
        model.removeAllElements(); //Step 2: Remove all elements from the model.
        final double rand = Math.random();
        for (int i = 0; i < 10; ++i)
            model.addElement("name " + Math.round((1 + i) * rand * 100)); //Step 3: Fill the model with new elements.
    }

    public static void proceed(final JList<String> list) {
        JOptionPane.showMessageDialog(list, "You selected: \"" + list.getSelectedValue() + "\"!");
        //Here you have the selected value approprietly to do whatever you like with...
    }

    public static void main(final String[] args) {
        final JList<String> names = new JList<>(new DefaultListModel<>()); //Do not forget to set the ListModel of the list to DefaultListModel!

        //List initialization:
        names.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        fillList(names);

        //Creating the buttons:
        final JButton fill = new JButton("Fill"),
                      proceed = new JButton("Proceed");

        //Adding listeners:
        fill.addActionListener(e -> fillList(names));
        proceed.addActionListener(e -> proceed(names));

        //Creating buttons' panel:
        final JPanel buttons = new JPanel();
        buttons.add(fill);
        buttons.add(proceed);

        //Scroll pane of list:
        final JScrollPane scroll = new JScrollPane(names);
        scroll.setPreferredSize(new Dimension(400, 200));

        //Main panel:
        final JPanel contents = new JPanel(new BorderLayout());
        contents.add(scroll, BorderLayout.CENTER);
        contents.add(buttons, BorderLayout.PAGE_END);

        //Frame:
        final JFrame frame = new JFrame("List of MesureTypes.");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(contents);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

它使用第二个选项(每次重新填充模型)。

pgccezyw

pgccezyw2#

private void fillList(){

PreparedStatement stmt = null;
Connection conn = null;

try    {
    Class.forName("java.sql.DriverManager");
    conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/hotel","root","root");
    stmt = conn.prepareStatement("select GuestName from hotelbookings");
    stmt.executeQuery();
    ResultSet rs = stmt.getResultSet();
    int i =0;
    DefaultListModel info = new DefaultListModel();

    while (rs.next()){
        String[] data = new String[100];
        data[i] = rs.getString("GuestName");
        info.addElement(data[i]);
        i = i + 1;
    }
    jList1 = new JList(info);
} catch(Exception e){
    JOptionPane.showMessageDialog (this, e.getMessage());
}

}

相关问题