我一直在尝试从list
中的mysql检索数据,并且工作正常,但是问题是我无法选择name
中的list
。
我想选择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);
}
}
这里是“显示列表按钮”的代码:-
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());
}
}
fillList类:-
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt){
fillList();
}
答案 0 :(得分:0)
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());
}
}
答案 1 :(得分:0)
好吧,据我从您发布的代码中得知,fillList
的基本问题是while循环。您为每个循环创建一个新的JList
。这将使您在jList1
方法中拥有的proceed
引用无效(或者我应该说 dirty )。您不应该每次都创建一个新的JList
。只需为每个循环保留相同的列表。甚至不要在循环外更改它。保持不变并每次更新其模型。
要更新模型,您至少具有以下两个选项:
jList1.setModel(your_created_model)
。JList
的模型一次设置为DefaultListModel
(例如,在构建时),然后在每个fillList
方法调用中:
getModel()
)。您知道它是DefaultListModel
,因此可以安全地将其转换为一个。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);
}
}
它利用第二个选项(每次重新填充模型)。