Bem, aqui estou eu novamente.
Estou tentando fazer com que assim que um programa é iniciado ele preencha uma lista buscando no banco de dados. O problema é que não sei como fazer isso! Pelo que vi uma lista é composta de uma Array de String, então pensei em montar algo que retornarse essa array pra mim (para maior organização), mas o grande problema é:
Não sei como fazer isso. Segue o código:
package MenuPrincipal;
import javax.swing.*;
import java.awt.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
@SuppressWarnings("serial")
public class Formas_de_Pagamento extends JFrame {
@SuppressWarnings("deprecation")
public Formas_de_Pagamento() {
super("Formas de Pagamento");
// Este trecho insere o logo da empresa no canto superior esquerdo da
// barra de titulos.
ImageIcon Clinica = new ImageIcon("src/Clinica.GIF");
setIconImage(Clinica.getImage());
JLabel lblCod = new JLabel("Código:");
JLabel lblForma = new JLabel("Forma de pagamento:");
final JTextField txtCod = new JTextField();
final JTextField txtForma = new JTextField();
final JList lstCod = new JList();
final JList lstForma = new JList();
JButton btnAdd = new JButton("Adicionar");
JButton btnExc = new JButton("Excluir");
Container Cont = getContentPane();
Color greenc = new Color(144, 255, 227);
Cont.setBackground(greenc);
setLayout(null);
Cont.add(lblCod);
Cont.add(lblForma);
Cont.add(txtCod);
Cont.add(txtForma);
Cont.add(lstCod);
Cont.add(lstForma);
Cont.add(btnAdd);
Cont.add(btnExc);
lblCod.reshape(90, 20, 150, 25);
lblForma.reshape(355, 20, 150, 25);
txtCod.reshape(40, 50, 150, 25);
txtForma.reshape(210, 50, 400, 25);
lstCod.reshape(40, 80, 150, 120);
lstForma.reshape(210, 80, 400, 120);
btnAdd.reshape(650, 50, 120, 40);
btnExc.reshape(650, 130, 120, 40);
Rectangle r = this.getGraphicsConfiguration().getBounds();
Double altura = new Double(r.getHeight());
Double largura = new Double(r.getWidth());
this.setBounds((largura.intValue() - 620) / 2,
(altura.intValue() - 300) / 2, 800, 600);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setResizable(false);
setSize(850, 300);
setVisible(true);
}
public String[] getCodigos() {
String sql = null;
String[] Codigos = null;
int i = 0;
sql = "SELECT * FROM formas_pag";
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/sgoca",
"root", "");
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Erro: (" + ex
+ ").\nEntre em contato com o administrador.");
}
if (con != null) {
try {
Statement stm = con.createStatement();
ResultSet rs = stm.executeQuery(sql);
while (rs.next()) {
Codigos[i] = rs.getString("Cod_Formas_Pag");
i++;
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Erro: (" + ex
+ ").\nEntre em contato com o administrador.");
} finally {
try {
con.close();
} catch (SQLException onConClose) {
JOptionPane.showMessageDialog(null,
"Houve erro no fechamento da conexão.\nErro: "
+ onConClose);
}
}
}
return Codigos;
}
}
Obrigado.