【问题标题】:Fill a jcombobox with the all tables from a data base用数据库中的所有表填充 jcombobox
【发布时间】:2014-06-13 16:42:48
【问题描述】:

我正在尝试用数据库中的表填充 JComboBox,我有这段代码执行查询,结果将他发送到另一个类,在其中我用数据修复填充 JComboBox,问题是我只收到垃圾。

查询和连接的代码

public ArrayList Tablas()
{    
    ArrayList tabla = new ArrayList(); 
    int i=0;
    try 
    {
        this.conectar("127.0.0.1", "mydb", "root", "root");
        this.consulta=this.conn.prepareStatement("show tables;");
        this.datos=this.consulta.executeQuery();
        while(datos.next())
        {
            tabla.add(datos);
            i++;
        }
        return tabla;

    } catch (ClassNotFoundException | SQLException ex) {
        Logger.getLogger(Servicio.class.getName()).log(Level.SEVERE, null, ex);
        return tabla;
    }
}

感谢您的帮助

以及我尝试设置 Jcombobox 的部分

    Servicio service = new Servicio();  
    ArrayList<String> tabla = new ArrayList<String>(); 
    tabla = service.Tablas();
    DefaultComboBoxModel model = new DefaultComboBoxModel(tabla.toArray());
    cTablas.setModel(model);

【问题讨论】:

  • “问题是我只收到垃圾” - 这如何告诉我们问题是什么?
  • 嗨,对不起,不能更具体,我已经解决了问题,我添加了 tabla.add(datos.getString(1));到代码,它的工作原理。

标签: java mysql arrays swing jcombobox


【解决方案1】:

尝试以下方法:

 ArrayList tabla = new ArrayList(); 

改成

List<String> tabla = new ArrayList();

改变

while(datos.next())
        {
            tabla.add(datos);
            i++;
        }

while(datos.next())
        {
            tabla.add(datos.get...(i);
            i++;
        }

让我们知道您遇到的错误是什么。连同堆栈跟踪(如果有)。 P.S - 我刚刚在文本编辑器中输入代码只是为了给你一个想法。因此,如果需要,请更换零件。

【讨论】:

  • 嗨 Hirak,谢谢你的回答,我已经解决了我的问题,只需添加“tabla.add(datos.getString(1));”现在可以工作了,很抱歉给我的帖子带来不便,并且我对问题的描述不佳,我希望改进,问候
  • 没问题....如果您看到我的回答,我建议您使用相同的“ tabla.add(datos.get...(i);”
  • 是的,这很有趣,因为我花了大约 2 个小时来解决这个问题,而答案很简单
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-15
相关资源
最近更新 更多