如何在combo_box中获取数据库值?

时间:2014-04-02 05:48:20

标签: java sql sql-server-2008 combobox jcombobox

我正在开发一个小项目,由我的老师分配给我,我是java的初学者 我在我的表单中使用了组合框来从数据库中获取数据...我能够获取数据 点击组合框中的item1从数据库中..我想摆脱项目
所以我可以直接在这里获取值是我的代码...

import java.sql.*;
Statement stmt=null;
Connection con=null;
try
  {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    con=DriverManager.getConnection("jdbc:odbc:sql_login");
    stmt=con.createStatement();
    String search="select * from tblflight";
    ResultSet rs=stmt.executeQuery(search);
    while(rs.next())
 {
    Date d1 = new Date(rs.getDate("departure_date").getTime());
        combo_box.addItem(d1);//
 }
        rs.close();
        con.close();
}catch(Exception e)
{
        e.printStackTrace();
}
 combox[ITEM1,ITEM2,ITEM3,ITEM4]//When form displays it shows these items
 but when I remove these items from properties I found combobox empty
 !![this is the first img when i open my form][1]
 ![this is the second img when i click any of the item][2]

![1]:http://i.stack.imgur.com/ez5fa.png“隐藏”

![2]:http://i.stack.imgur.com/orlDK.png“隐藏”

1 个答案:

答案 0 :(得分:0)

要获得所选值,您可以按照以下步骤进行操作,

String sSelectedValue = combobox.getSelectedItem().toString();

获取所选项目索引

int iSelectedItemIndex = combobox.getSelectedItemIndex();

删除项目。

combobox.removeItemAt(index); //index is the index of the item you want to remove from the list.