我正在尝试用数据库中的表填充 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);
最佳答案
尝试以下操作:
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 - 我刚刚在文本编辑器中输入了代码只是为了给您一个想法。因此,如果需要,请更换部件。
关于java - 使用数据库中的所有表填充 jcombobox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23335953/