JavaGambiara 14 de mai. de 2017
String sql = “update produto set estoque = ? WHERE NOME = ?”;
Coloque assim!
String sql = “update produto set estoque = ? WHERE NOME = " +produto.getNome()+”";
Agora pode ser que retorne algum erro!
Diego_Gomes_Dias 14 de mai. de 2017
Ele retorna este erro.
Conectando ao Banco de Dados
Exception in thread “ AWT - EventQueue - 0 ” java . lang . NullPointerException
at CadCompraVenda . atualizaEstoque ( CadCompraVenda . java : 99 )
at CadCompraVenda . access$1 ( CadCompraVenda . java : 96 )
at CadCompraVenda$10 . actionPerformed ( CadCompraVenda . java : 343 )
at javax . swing . AbstractButton . fireActionPerformed ( Unknown Source )
at javax . swing . AbstractButton$Handler . actionPerformed ( Unknown Source )
at javax . swing . DefaultButtonModel . fireActionPerformed ( Unknown Source )
at javax . swing . DefaultButtonModel . setPressed ( Unknown Source )
at javax . swing . plaf . basic . BasicButtonListener . mouseReleased ( Unknown Source )
at java . awt . Component . processMouseEvent ( Unknown Source )
at javax . swing . JComponent . processMouseEvent ( Unknown Source )
at java . awt . Component . processEvent ( Unknown Source )
at java . awt . Container . processEvent ( Unknown Source )
at java . awt . Component . dispatchEventImpl ( Unknown Source )
at java . awt . Container . dispatchEventImpl ( Unknown Source )
at java . awt . Component . dispatchEvent ( Unknown Source )
at java . awt . LightweightDispatcher . retargetMouseEvent ( Unknown Source )
at java . awt . LightweightDispatcher . processMouseEvent ( Unknown Source )
at java . awt . LightweightDispatcher . dispatchEvent ( Unknown Source )
at java . awt . Container . dispatchEventImpl ( Unknown Source )
at java . awt . Window . dispatchEventImpl ( Unknown Source )
at java . awt . Component . dispatchEvent ( Unknown Source )
at java . awt . EventQueue . dispatchEventImpl ( Unknown Source )
at java . awt . EventQueue . access$500 ( Unknown Source )
at java . awt . EventQueue$3 . run ( Unknown Source )
at java . awt . EventQueue$3 . run ( Unknown Source )
at java . security . AccessController . doPrivileged ( Native Method )
at java . security . ProtectionDomain$JavaSecurityAccessImpl . doIntersectionPrivilege ( Unknown Source )
at java . security . ProtectionDomain$JavaSecurityAccessImpl . doIntersectionPrivilege ( Unknown Source )
at java . awt . EventQueue$4 . run ( Unknown Source )
at java . awt . EventQueue$4 . run ( Unknown Source )
at java . security . AccessController . doPrivileged ( Native Method )
at java . security . ProtectionDomain$JavaSecurityAccessImpl . doIntersectionPrivilege ( Unknown Source )
at java . awt . EventQueue . dispatchEvent ( Unknown Source )
at java . awt . EventDispatchThread . pumpOneEventForFilters ( Unknown Source )
at java . awt . EventDispatchThread . pumpEventsForFilter ( Unknown Source )
at java . awt . EventDispatchThread . pumpEventsForHierarchy ( Unknown Source )
at java . awt . EventDispatchThread . pumpEvents ( Unknown Source )
at java . awt . EventDispatchThread . pumpEvents ( Unknown Source )
at java . awt . EventDispatchThread . run ( Unknown Source )
JavaGambiara 14 de mai. de 2017
Comente essa linha!
//stmt.setString(2,produto.getNome());
Diego_Gomes_Dias 14 de mai. de 2017
Eu ja tinha feuti mas não funcionou, eu alterei para dessa forma e mesmo assim nao deu cero
CompraVendaDao
public void AtualizaEstoque ( Produto produto ) throws SQLException {
String sql = "update produto set estoque = ? WHERE NOME = ?" ;
try {
PreparedStatement stmt = getConnection (). prepareStatement ( sql );
stmt . setInt ( 1 , produto . getEstoque ());
stmt . setString ( 2 , produto . getNome ());
stmt . close ();
} catch ( SQLException e ) {
throw new RuntimeException ( e );
}
}
TelaPrincipal
private void atualizaEstoque () throws ParseException , SQLException {
Produto produto = new Produto ();
produto . setNome ( NomeProduto );
produto . setEstoque ( ValorEstoque + Integer . parseInt ( textEstoque . getText ()));
CompraVendaDao dao = new CompraVendaDao ();
dao . AtualizaEstoque ( produto );
}
Solucao aceita
shuttner 14 de mai. de 2017 1 like
Ficou faltando vc mandar executar.
public void AtualizaEstoque(Produto produto) throws SQLException{
String sql = "update produto set estoque = ? WHERE NOME = ?" ;
try {
PreparedStatement stmt = getConnection (). prepareStatement ( sql );
stmt . setInt ( 1 , produto . getEstoque ());
stmt . setString ( 2 , produto . getNome ());
stnt . execute ();
stmt . close ();
} catch ( SQLException e ) {
throw new RuntimeException ( e );
}
}