Last active
December 3, 2016 19:57
-
-
Save gaverdugo/f2bc09b83e888ae20801e7c6a52c2c3e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package rentasvehiculos.TableListeners; | |
import java.sql.SQLException; | |
import javax.swing.JOptionPane; | |
import javax.swing.event.TableModelEvent; | |
import javax.swing.event.TableModelListener; | |
import rentasvehiculos.Database; | |
import rentasvehiculos.JDBCTableAdapter; | |
/** | |
* | |
* @author Admin | |
*/ | |
public class VehiculosTableListener implements TableModelListener { | |
private final Database database; | |
private final String table, key; | |
public VehiculosTableListener(Database db, String table, String key) { | |
super(); | |
database = db; | |
this.table = table; | |
this.key = key; | |
} | |
@Override | |
public void tableChanged(TableModelEvent event) { | |
JDBCTableAdapter modelo = (JDBCTableAdapter) event.getSource(); | |
int row = event.getFirstRow(); | |
int column = event.getColumn(); | |
if (column < modelo.getKeyFields()) { | |
return; | |
} | |
String colSQLName = modelo.getSQLColumnName(column); | |
int columnType = modelo.getSQLColumnType(column); | |
String sql; | |
if (columnType == 5) { | |
//SMALLINT | |
sql = String.format( | |
"UPDATE %s SET %s = %s WHERE %s = \'%s\'", | |
table, colSQLName, modelo.getValueAt(row, column), modelo.getSQLColumnName(0), modelo.getValueAt(row, 0)); | |
} else if (columnType == 3) { | |
//DECIMAl | |
sql = String.format( | |
"UPDATE %s SET %s = %s WHERE %s = \'%s\'", | |
table, colSQLName, modelo.getValueAt(row, column), modelo.getSQLColumnName(0), modelo.getValueAt(row, 0)); | |
} else { | |
//VARCHAR O DATE O ENUM | |
sql = String.format( | |
"UPDATE %s SET %s = \'%s\' WHERE %s = \'%s\'", | |
table, colSQLName, modelo.getValueAt(row, column), modelo.getSQLColumnName(0), modelo.getValueAt(row, 0)); | |
} | |
try { | |
database.doUpdate(sql); | |
} catch (SQLException ex) { | |
ex.printStackTrace(System.err); | |
if (ex.getSQLState().equals("HY000")) { | |
JOptionPane.showMessageDialog(null, "Tipo de dato incorrecto en " + colSQLName); | |
} else if (ex.getSQLState().equals("01000")) { | |
JOptionPane.showMessageDialog(null, "Opción no valida en " + colSQLName); | |
} else { | |
JOptionPane.showMessageDialog(null, ex.getMessage()); | |
} | |
} | |
System.out.println(sql); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment