Bom dia, estou com um problema que não estou conseguindo resolver. O problema é o seguinte: Eu quero pegar o valor da linha que eu selecionei na coluna 2, somar com o campo 3 do meu banco de dados e fazer um update no mesmo campo 3. Sendo assim valorselecionado+valorcampo3 = o que eu quero, que é o que vai voltar pro campo 3. Segue o código. Gostaria que me alertassem do que estou fazendo de errado.
public void atualizabanco(){
String sql = "Select * from estoque where id=?";
String sql1 = "Update estoque set qtd=? where id=?";
try {
pst = conexao.prepareStatement(sql);
pst.setString(1, campoid.getText());
rs = pst.executeQuery();
if (rs.next()) {
}
int soma;
int qtd = Integer.parseInt(rs.getString(3));
int linhaSelecionada = jTable1.getSelectedRow();
int coluna = 2;
int valorSelecionado = (Integer) jTable1.getValueAt(linhaSelecionada, coluna);
soma = qtd + valorSelecionado;
pst = conexao.prepareStatement(sql1);
pst.setString(1, Integer.toString(soma));
pst.setString(2, campoid.getText());
pst.executeUpdate();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Pode até não ter, mas que if vazio tá errado, isso eu te garanto kk não faz sentido algum ele ser vazio. É como se lêssemos: " se rs.next() resultar true, não faça nada ". Qual a lógica de você ver se existe algo no ResultSet e não fazer nada com ele?
Exception in thread “AWT-EventQueue-0” java.lang.RuntimeException: java.lang.RuntimeException: java.lang.ArrayIndexOutOfBoundsException: -1
at GUI.VendaProd.remove(VendaProd.java:135)
at GUI.VendaProd.jButton2ActionPerformed(VendaProd.java:566)
at GUI.VendaProd.access$1700(VendaProd.java:11)
at GUI.VendaProd$14.actionPerformed(VendaProd.java:459)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6539)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6304)
at java.awt.Container.processEvent(Container.java:2239)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2297)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
at java.awt.Container.dispatchEventImpl(Container.java:2283)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
at java.awt.EventQueue$4.run(EventQueue.java:733)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.lang.RuntimeException: java.lang.ArrayIndexOutOfBoundsException: -1
at GUI.VendaProd.atualizabanco(VendaProd.java:159)
at GUI.VendaProd.remove(VendaProd.java:133)
… 39 more
Caused by: java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.Vector.elementData(Vector.java:737)
at java.util.Vector.elementAt(Vector.java:480)
at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:648)
at javax.swing.JTable.getValueAt(JTable.java:2720)
at GUI.VendaProd.atualizabanco(VendaProd.java:150)
… 40 more
Faz o teste básico pra ver se ta pegando o valor da tabela mesmo, pode colocar esse código isolado mesmo, sem conexão. Você vai clicar na tabela e apertar em um botão e ele tem que mostrar o resultado
Coloca isso em um botão
String linha = "" + tabela.getValueAt(tabela.getSelectedRow(), 1);
System.out.println(linha);
Agora vai fazendo os passos, tenta fazer a soma com alguma variável padrão mesmo, pode ser tudo via console.
Tipo:
private int get_table_result(){
int resultado;
String linha = "" + tabela.getValueAt(tabela.getSelectedRow(), 1);
int valor = Integer.parseInt(linha);
resultado = valor + get_result();
return resultado;
}
O resultado acima é CERTEZA que funciona, depois é só você pegar o resultado e fazer o SELECT com o banco.
private int get_result(){
int qtd;
try {
conecta.executaSQL("select * from tabela where id='" + campoid.getText() + "'");
conecta.rs.first();
qtd = conecta.rs.getInt("qtd");
} catch (SQLException s) {
s.printStackTrace();
}
return qtd;
}
public update_database(){
.......
}