【发布时间】: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