Eu to tentando fazer um programa com banco de dados MYSQL em que ao clicar numa linha da tabela, ele pegue o Usuario e ID dela, faça uma verificação com o ResultSet e depois delete a linha.
public class Lista extends JFrame {
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 final String Tipos[]= {"VarChar","Int",""};
private String sql = "SELECT * FROM information";
private String sql2 = "delete from information where id = ?";
private static Connection pegarConexao() throws Exception {
Class.forName(DRIVER);
Connection con = DriverManager.getConnection(URL,USUARIO,SENHA);
return con;
}
public Lista() throws HeadlessException {
super("Biblioteca Virtual");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
pack();
setVisible(true);
setSize(600,340);
initComponents();
}
private void componetes() {
Cadastro c1 = new Cadastro();
JTable table = new JTable();
table.setModel(new ModeloTabela(getCadastros()));
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setPreferredSize(new Dimension(500, 300));
add(scrollPane);
try {
Connection con = pegarConexao();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql2);
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent click) {
if(click.getClickCount() > 0) {
int coluna = table.getSelectedColumn();
if(coluna == 2) {
try {
while(rs.next()) {
c1.getID();
}
} catch (SQLException ex) {
System.out.println("aaaa");
}
}
}
}
});
} catch(Exception e) {
System.out.println("Ta deu merda");
}
}
private void initComponents() {
componetes();
}
private List<Cadastro> getCadastros() {
List<Cadastro> pessoas = new ArrayList<>();
try {
Connection con = pegarConexao();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
pessoas.add(new Cadastro(rs.getString("user"),rs.getString("pass"),rs.getInt("id")));
}
} catch(Exception ex) {
JOptionPane.showMessageDialog(null,"Deu merda");
}
return pessoas;
}
public static void main(String[] args) {
new Lista();
}
}