[英]Fill a jcombobox with the all tables from a data base
我正在嘗試用數據庫中的表填充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++;
}
讓我們知道您遇到的錯誤是什么。 連同堆棧跟蹤(如果有)。 PS-我剛剛在文本編輯器中輸入了代碼,只是為了給您一個想法。 因此,如有必要,請更換零件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.