
MySQL
Publicado por Jemel (15 intervenciones) el 27/05/2011 19:13:53
Saludos. Estoy trabajando con java y MySQL. Mi problema esta en que muestro datos de una tabla de mi BD en un JTable y quiero agregar datos adicionales a cada registro mostrado en el JTable. Lo que quiero hacer es que los datos agregados se guarden en la tabla que estoy mostrando.
El código que estoy usando es este. No guarda los datos sino que me da el mensaje de error del catch "Error al guardar los datos".
Gracias de antemano
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
int filesel = tblistado.getSelectedRow();
int reply;
DefaultTableModel l = (DefaultTableModel) tblistado.getModel();
int filas = l.getRowCount();
String cedula = "", p1l1 ="", p2l1 = "", proml1 ="", p3l1="", pp3l1="",defl1="", p1l2 ="", p2l2= "", proml2 ="", p3l2="", pp3l2="",defl2="", p1l3 ="", p2l3 = "", proml3 ="", p3l3="", pp3l3="",defl3="";
String tabla = cmbnp.getSelectedItem() + "_" + cmbmp.getSelectedItem() + "_" + cmbsp.getSelectedItem();
for (int j = 0; j<filas ; j++)
{
cedula = (String) tblistado.getValueAt(filesel, 0);
p1l1 = (String) tblistado.getValueAt(filesel, 3);
p2l1 = (String) tblistado.getValueAt(filesel, 4);
proml1 = (String) tblistado.getValueAt(filesel, 5);
p3l1 = (String) tblistado.getValueAt(filesel, 6);
pp3l1 = (String) tblistado.getValueAt(filesel, 7);
defl1 = (String) tblistado.getValueAt(filesel, 8);
p1l2 = (String) tblistado.getValueAt(filesel, 9);
p2l2 = (String) tblistado.getValueAt(filesel, 10);
proml2 = (String) tblistado.getValueAt(filesel, 11);
p3l2 = (String) tblistado.getValueAt(filesel, 12);
pp3l2 = (String) tblistado.getValueAt(filesel, 13);
defl2 = (String) tblistado.getValueAt(filesel, 14);
p1l3 = (String) tblistado.getValueAt(filesel, 15);
p2l3 = (String) tblistado.getValueAt(filesel, 16);
proml3 = (String) tblistado.getValueAt(filesel, 17);
p3l3 = (String) tblistado.getValueAt(filesel, 18);
pp3l3 = (String) tblistado.getValueAt(filesel, 19);
defl3 = (String) tblistado.getValueAt(filesel, 20);
}
try
{
String msql = "select *from `" + tabla + "` where cedula = '" + cedula + "'";
try
{
if (filesel == -1)
JOptionPane.showMessageDialog(this, "Debe seleccionar todo");
else
{
ms = "update `" + tabla + "` set p1l1 = '" + p1l1 + "', p2l1 = '" + p2l1 + "', proml1 ='" + proml1 + "', p3l1 ='" + p3l1 + "', pp3l1 = '" + pp3l1 + "', defl1 = '" + defl1 + "', p1l2 = '" + p1l2 + "', p2l2 = '" + p2l2 + "', proml2 ='" + proml2 + "', p3l2 ='" + p3l2 + "', pp3l2 = '" + pp3l2 + "', defl2 = '" + defl2 + "', p1l3 = '" + p1l3 + "', p2l3 = '" + p2l3 + "', proml3 ='" + proml3 + "', p3l3 ='" + p3l3 + "', pp3l3 = '" + pp3l3 + "', defl3 = '" + defl3 + "' where cedula = '" + cedula + "'";
sql = conexion.createStatement();
sql.executeUpdate(ms);
sql.close();
JOptionPane.showMessageDialog(this, "Notas Registradas");
}
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(this, "Error al guardar los datos");
}
}
catch (Exception e)
{
JOptionPane.showMessageDialog(this, "Error de conexion");
}
}//GEN-LAST:event_jButton1ActionPerformed
El código que estoy usando es este. No guarda los datos sino que me da el mensaje de error del catch "Error al guardar los datos".
Gracias de antemano
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
int filesel = tblistado.getSelectedRow();
int reply;
DefaultTableModel l = (DefaultTableModel) tblistado.getModel();
int filas = l.getRowCount();
String cedula = "", p1l1 ="", p2l1 = "", proml1 ="", p3l1="", pp3l1="",defl1="", p1l2 ="", p2l2= "", proml2 ="", p3l2="", pp3l2="",defl2="", p1l3 ="", p2l3 = "", proml3 ="", p3l3="", pp3l3="",defl3="";
String tabla = cmbnp.getSelectedItem() + "_" + cmbmp.getSelectedItem() + "_" + cmbsp.getSelectedItem();
for (int j = 0; j<filas ; j++)
{
cedula = (String) tblistado.getValueAt(filesel, 0);
p1l1 = (String) tblistado.getValueAt(filesel, 3);
p2l1 = (String) tblistado.getValueAt(filesel, 4);
proml1 = (String) tblistado.getValueAt(filesel, 5);
p3l1 = (String) tblistado.getValueAt(filesel, 6);
pp3l1 = (String) tblistado.getValueAt(filesel, 7);
defl1 = (String) tblistado.getValueAt(filesel, 8);
p1l2 = (String) tblistado.getValueAt(filesel, 9);
p2l2 = (String) tblistado.getValueAt(filesel, 10);
proml2 = (String) tblistado.getValueAt(filesel, 11);
p3l2 = (String) tblistado.getValueAt(filesel, 12);
pp3l2 = (String) tblistado.getValueAt(filesel, 13);
defl2 = (String) tblistado.getValueAt(filesel, 14);
p1l3 = (String) tblistado.getValueAt(filesel, 15);
p2l3 = (String) tblistado.getValueAt(filesel, 16);
proml3 = (String) tblistado.getValueAt(filesel, 17);
p3l3 = (String) tblistado.getValueAt(filesel, 18);
pp3l3 = (String) tblistado.getValueAt(filesel, 19);
defl3 = (String) tblistado.getValueAt(filesel, 20);
}
try
{
String msql = "select *from `" + tabla + "` where cedula = '" + cedula + "'";
try
{
if (filesel == -1)
JOptionPane.showMessageDialog(this, "Debe seleccionar todo");
else
{
ms = "update `" + tabla + "` set p1l1 = '" + p1l1 + "', p2l1 = '" + p2l1 + "', proml1 ='" + proml1 + "', p3l1 ='" + p3l1 + "', pp3l1 = '" + pp3l1 + "', defl1 = '" + defl1 + "', p1l2 = '" + p1l2 + "', p2l2 = '" + p2l2 + "', proml2 ='" + proml2 + "', p3l2 ='" + p3l2 + "', pp3l2 = '" + pp3l2 + "', defl2 = '" + defl2 + "', p1l3 = '" + p1l3 + "', p2l3 = '" + p2l3 + "', proml3 ='" + proml3 + "', p3l3 ='" + p3l3 + "', pp3l3 = '" + pp3l3 + "', defl3 = '" + defl3 + "' where cedula = '" + cedula + "'";
sql = conexion.createStatement();
sql.executeUpdate(ms);
sql.close();
JOptionPane.showMessageDialog(this, "Notas Registradas");
}
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(this, "Error al guardar los datos");
}
}
catch (Exception e)
{
JOptionPane.showMessageDialog(this, "Error de conexion");
}
}//GEN-LAST:event_jButton1ActionPerformed
Valora esta pregunta


0