[英]java - retrieving data from database and display using JcomboBox
I would like to be able to select a data from database and display it using the comboBox. 我希望能够从数据库中选择一个数据并使用comboBox显示它。
I have the following code but it does not display the data in the comboBox. 我有以下代码,但它不会在comboBox中显示数据。 I do realize there is codes missing to display the data and my SQL statement is not correct.
我确实意识到缺少代码来显示数据,并且我的SQL语句不正确。 I am just not sure what I can do any advise is appreciated.
我只是不确定我能做些什么建议受到赞赏。
try { Statement st = db.con.createStatement(); 尝试{语句st = db.con.createStatement(); ResultSet rs = st.executeQuery("SELECT Name, Size, Price FROM item WHERE Name=" + comboBox_1.getToolkit());
ResultSet rs = st.executeQuery(“ SELECT名称,大小,价格从项目WHERE Name =” + comboBox_1.getToolkit());
JOptionPane.showMessageDialog(frame, "displayed");
while (rs.next()) {
String name = rs.getString("Name");
String size = rs.getString("size");
String price = rs.getString("price");
textArea_Name.append(name);
textArea_size.append(size);
textArea_price.append(price);
comboBox_1.addItem(rs.getString("Name"));
comboBox_1.getSelectedItem();
}}
catch (SQLException e ) {
System.out.println("user not added");
e.printStackTrace();
}
}
});
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn= DriverManager.getConnection("jdbc:odbc:driverName");
Statement st = conn.createStatement();
ResultSet r=st.executeQuery("select * from tableName");
while (r.next()) {
JComboBox.addItem(r.getString("Key_Coloumn_Name"));
}
conn.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(null,"Failed to Connect to Database","Error Connection", JOptionPane.WARNING_MESSAGE);
System.exit(0);
}
try {
Statement stmt = db.con.createStatement();
ResultSet rs = stmt5.executeQuery("select * from tbl_your_table");
while (rs.next()) {
String pat = rs.getString("name");
jComboBox.addItem(pat);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
Try this tutorial. 试试本教程。 It does exactly that.
它确实做到了。
http://www.roseindia.net/tutorial/java/swing/comboboxwithdatabaseValues.html http://www.roseindia.net/tutorial/java/swing/comboboxwithdatabaseValues.html
con = DriverManager.getConnection("jdbc:mysql://:3306/database","user","password");
stat = con.createStatement();
ResultSet rs = stat.executeQuery("select name from student;");
while(rs.next()){
jComboBox1.addItem(rs.getString("name"));
}
rs.close();
stat.close();
con.close();
comb = new JComboBox();
DefaultComboBoxModel dftm = (DefaultComboBoxModel) comb.getModel();
ArrayList<stock_data> list = userLists();
row = new Object[1];
for (int i = 0; i < list.size(); i++) {
row[0] = list.get(i).get_item();
dftm.addElement(row);
// comb.setModel(dftm);
}
Then create your method and also remember you need a stock_data Class 然后创建您的方法,还要记住您需要一个stock_data类
private ArrayList<stock_data> userLists() {
try {
connection = localhost.dbConnector();
ArrayList<stock_data> usersList = new ArrayList();
try {
String query = "Select * from itemlist";
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery(query);
stock_data users;
while (rs.next()) {
// users = new stock_data(rs.getString("name"));
// usersList.add(users);
comb.addItem(rs.getString("name"));
}
rs.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "blah blah");
e.printStackTrace();
}
return usersList;
} catch (Exception e1) {
e1.printStackTrace();
}
return null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.