Pessoal, estou tentando criar um programa de cadastro com o banco de dados Mysql, to usando o Assistente SWING do netbeans para fazer a tabela padrão, e implementando classes para ficar com o modelo AbstractTableModel, mas eu não sei como ligar ela ao meu banco de dados, eu to ficando louco ja com essa desgraça
Codigo
public class ModeloTabela extends AbstractTableModel {
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String USUARIO = "root";
private static final String SENHA = "";
private static final String URL = "jdbc:mysql://localhost:3306/cadastro";
private static Connection pegarConexao() throws Exception {
Class.forName(DRIVER);
Connection con = DriverManager.getConnection(URL, USUARIO, SENHA);
return con;
}
private String[] colunas = {"Usuario", "Senha", "ID"};
private ArrayList<Cadastro> dados = new ArrayList<>();
//Show de bola
public ArrayList<Cadastro> contatosLista() {
try {
Connection con = pegarConexao();
String sql = "SELECT * FROM information";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
Cadastro c1;
while(rs.next()) {
c1 = new Cadastro(rs.getString("user"), rs.getString("pass"), rs.getInt("id"));
dados.add(c1);
}
} catch(Exception ex) {
System.err.println("Error: " + ex);
}
return dados;
}
public String getColumnName(int column) {
return colunas[column];
}
public int getRowCount() {
return dados.size();
}
public int getColumnCount() {
return colunas.length;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public void addRow() {
Cadastro c1 = new Cadastro();
ArrayList<Cadastro> lista = contatosLista();
Object[] role = new Object [6];
for (int i = 0; i < lista.size(); i++) {
role[0] = lista.get(i).getUsuario();
role[1] = lista.get(i).getSenha();
role[2] = lista.get(i).getId();
this.dados.add(c1);
}
}
}
