CRUD utilizando Java Swing

PostgreSQL

Java

JDBC

POO Java

JDK

07/12/2020

Pessoal, estou com um problema no meu código. Estou construindo um CRUD orientado a objetos e estou preso no método UPDATE.
Segue o meu código:

BD_conexao x = new BD_conexao();
ResultSet rs = null;
x.conectar();

DefaultTableModel dtm = (DefaultTableModel) table.getModel();
int column = 0;
int row = table.getSelectedRow();

if (row == -1) {
JOptionPane.showMessageDialog(this, "Nenhuma linha selecionada!");
return;
}

String cod = dtm.getValueAt(row, 0).toString();
String title = dtm.getValueAt(row, 1).toString();
String aut = dtm.getValueAt(row, 2).toString();
String gen = dtm.getValueAt(row, 3).toString();
String price = dtm.getValueAt(row, 4).toString();

String newcod = JOptionPane.showInputDialog(null, "Digite o código: ", cod);
String newtitle = JOptionPane.showInputDialog(null, "Informe o título: ", title);
String newaut = JOptionPane.showInputDialog(null, "Informe o autor: ", aut);
String newgen = JOptionPane.showInputDialog(null, "Informe o gênero: ", gen);
String newprice = JOptionPane.showInputDialog(null, "Digite o preço: ", price);

String sql = "UPDATE livros SET codigo=1263";
rs = x.buscaLivro(sql);

try {
if (table.getSelectedRowCount() >= 1) {
dtm.setValueAt(newcod, row, 0);
dtm.setValueAt(newtitle, row, 1);
dtm.setValueAt(newaut, row, 2);
dtm.setValueAt(newgen, row, 3);
dtm.setValueAt(newprice, row, 4);
}
} catch (Exception ex) {
Logger.getLogger(IG_consulta.class.getName()).log(Level.SEVERE, null, ex);
}

x.desconectar();
}
Visualmente com as caixas de diálogo do JOptionPane eu consigo editar as linhas do JTable como pretendia, mas não estou conseguindo salvá-los na minha tabela postgresql.


Minha dificuldade está em salvar as edições que faço no JTable via JOptionPane na tabela do Postgresql para quando eu recarregar as telas do java swing, os dados atualizados estejam salvos, alguém pode me dar uma luz?
Cristian Guilherme

Cristian Guilherme

Curtidas 0
POSTAR