将数据库字段值加载到JCombobox

时间:2022-12-05 10:05:07

How to load values from an Oracle db to a JComboBox to make it easier for the user To Choose from I Have tried this:

如何将值从Oracle数据库加载到JComboBox以使用户更容易从I中选择我试过这个:

    Connection dbcon = null;
    try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        dbcon = DriverManager.getConnection("
             jdbc:oracle:thin:@localhost:1521:XE", "USERNAME", "PASSWORD");
        Statement st = dbcon.createStatement();
        String combo = "Select EMP_IDNUM from employees";
        ResultSet res = st.executeQuery(combo);
        while(res.next()){
            String ids = res.getString("EMP_IDNUM");
            String [] str = new String[]{ids};
            cboId = new JComboBox(str);
        }
    } catch (Exception d) {
        System.out.println(d);
    }

This is Only getting me the first Value Into the JComboBox cboID. What Is the Best way to Load the entire Field Data (EMP_IDNUM) into the Jcombobox??

这只是让我第一个进入JComboBox cboID的值。将整个现场数据(EMP_IDNUM)加载到Jcombobox中的最佳方法是什么?

3 个解决方案

#1


2  

String [] str = new String[]{ids};

It means your String array is having only one ids value which you have loaded String ids = res.getString("EMP_IDNUM");

这意味着你的String数组只有一个你已经加载的id值String ids = res.getString(“EMP_IDNUM”);

if(rs.getRow()>0){
 String [] str = new String[res.getRow()];
 int i=0;
 while(res.next()){
   str[i++] = res.getString("EMP_IDNUM");
 }
}
JComboBox jcb = new JComboBox(str);

Instead of array you can use Vector also to create JComboBox.

您也可以使用Vector来创建JComboBox,而不是数组。

#2


2  

you can use this code:

你可以使用这段代码:

Vector v = new Vector();
while(res.next()){
    String ids = res.getString("EMP_IDNUM");
    v.add(ids)
}
JComboBox jcb = new JComboBox(v);

In this code you create a Vector with your Strings, and then invoke directly the JComboBox(Vector items) constructor of JComboBox

在此代码中,您使用您的字符串创建一个Vector,然后直接调用JComboBox的JComboBox(Vector项)构造函数

#3


2  

there are three important areas

有三个重要方面

a) close all JDBC Objects in the finally block, because these Object aren't, never GC'ed

a)关闭finally块中的所有JDBC对象,因为这些Object不是,而是从不GC

try {

} catch (Exception d) {
    System.out.println(d);
} finally {
    try {
        st.close()
        res.close() 
        dbcon.close()
    } catch (Exception d) {
        //not important
    }
}

b) don't to create any Objects inside try - catch - finally, prepare that before

b)不要在try - catch中创建任何对象 - 最后,准备之前

meaning cboId = new JComboBox(str);

意思是cboId = new JComboBox(str);

c) put all data from JDBC to the ComboBoxModel, prepare that before

c)将所有数据从JDBC放到ComboBoxModel中,然后再准备

#1


2  

String [] str = new String[]{ids};

It means your String array is having only one ids value which you have loaded String ids = res.getString("EMP_IDNUM");

这意味着你的String数组只有一个你已经加载的id值String ids = res.getString(“EMP_IDNUM”);

if(rs.getRow()>0){
 String [] str = new String[res.getRow()];
 int i=0;
 while(res.next()){
   str[i++] = res.getString("EMP_IDNUM");
 }
}
JComboBox jcb = new JComboBox(str);

Instead of array you can use Vector also to create JComboBox.

您也可以使用Vector来创建JComboBox,而不是数组。

#2


2  

you can use this code:

你可以使用这段代码:

Vector v = new Vector();
while(res.next()){
    String ids = res.getString("EMP_IDNUM");
    v.add(ids)
}
JComboBox jcb = new JComboBox(v);

In this code you create a Vector with your Strings, and then invoke directly the JComboBox(Vector items) constructor of JComboBox

在此代码中,您使用您的字符串创建一个Vector,然后直接调用JComboBox的JComboBox(Vector项)构造函数

#3


2  

there are three important areas

有三个重要方面

a) close all JDBC Objects in the finally block, because these Object aren't, never GC'ed

a)关闭finally块中的所有JDBC对象,因为这些Object不是,而是从不GC

try {

} catch (Exception d) {
    System.out.println(d);
} finally {
    try {
        st.close()
        res.close() 
        dbcon.close()
    } catch (Exception d) {
        //not important
    }
}

b) don't to create any Objects inside try - catch - finally, prepare that before

b)不要在try - catch中创建任何对象 - 最后,准备之前

meaning cboId = new JComboBox(str);

意思是cboId = new JComboBox(str);

c) put all data from JDBC to the ComboBoxModel, prepare that before

c)将所有数据从JDBC放到ComboBoxModel中,然后再准备