Last active
December 3, 2016 19:58
-
-
Save gaverdugo/5ef68556a59f9ffa5bf4f6c4b7265988 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; | |
import java.sql.ResultSet; | |
import java.sql.ResultSetMetaData; | |
import java.sql.SQLException; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import javax.swing.table.DefaultTableModel; | |
/** | |
* | |
* @author Admin | |
*/ | |
public class JDBCTableAdapter extends DefaultTableModel { | |
private String[] sqlColumnNames; | |
private int[] sqlColumnTypes; | |
private int keyFields; | |
public JDBCTableAdapter(ResultSet res, int keys) { | |
super(); | |
ResultSet rs = res; | |
keyFields = keys; | |
int rows = 0; | |
int columns = 0; | |
//Matriz para guardar los contenidos del ResultSet | |
Object[][] data = null; | |
//Arreglo para los nombres de las columnas | |
Object[] columnNames = null; | |
try { | |
ResultSetMetaData metaData = rs.getMetaData(); | |
columns = metaData.getColumnCount(); | |
columnNames = new Object[columns]; | |
sqlColumnNames = new String[columns]; | |
sqlColumnTypes = new int[columns]; | |
for (int i = 0; i < columns; i++) { | |
sqlColumnNames[i] = metaData.getColumnName(i + 1); | |
sqlColumnTypes[i] = metaData.getColumnType(i + 1); | |
columnNames[i] = metaData.getColumnLabel(i + 1); | |
} | |
//Mover el cursor a la ultima fila | |
rs.last(); | |
//Obtener el numero de fila | |
rows = rs.getRow(); | |
//Instanciar la matriz con el numero de filas y columnas | |
data = new Object[rows][columns]; | |
//Devolver el cursor a la primera fila | |
rs.beforeFirst(); | |
int r = 0; | |
while (rs.next()) { | |
for (int i = 0; i < columns; i++) { | |
data[r][i] = rs.getString(i + 1); | |
} | |
r++; | |
} | |
} catch (SQLException ex) { | |
Logger.getLogger(JDBCTableAdapter.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
this.setDataVector(data, columnNames); | |
} | |
public String getSQLColumnName(int i) { | |
String name = null; | |
if (i >= 0 && i < sqlColumnNames.length) { | |
name = sqlColumnNames[i]; | |
} | |
return name; | |
} | |
public int getSQLColumnType(int i) { | |
int dataType = -1; | |
if (i >= 0 && i < sqlColumnTypes.length) { | |
dataType = sqlColumnTypes[i]; | |
} | |
return dataType; | |
} | |
public int getKeyFields () { | |
return keyFields; | |
} | |
@Override | |
public boolean isCellEditable(int row, int column) { | |
return !(column >= 0 && column < keyFields); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment