CONSULTAS CON JTABLE
package jtable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
public class form2 extends javax.swing.JInternalFrame {
DefaultTableModel modelo = new DefaultTableModel();
public form2() {
initComponents();
setTitle("Consulta de Cargos");
//jtable
cabecera_tabla();
}
private void cabecera_tabla(){
modelo.addColumn("Cargo");
modelo.addColumn("Nombre");
jTable1.setModel(modelo);
}
private void buscar(){
try{
Conexion obj = new Conexion();
Connection cone = obj.getConnection();
PreparedStatement ps = cone.prepareStatement("Select cargo,nombre from cargo where nombre=?");
ps.setString(1, this.txtnom.getText().trim());
ResultSet rs = ps.executeQuery();
while(rs.next()){
Object [] fila = new Object[2];
fila[0] = rs.getString("cargo");
fila[1] = rs.getString("nombre");
modelo.addRow(fila);
fila=null;
}
rs.close();
cone.close();
}catch(SQLException ex){
JOptionPane.showMessageDialog(null, "Error Buscar Tabla: " + ex.getMessage());
}
}
private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {
buscar();
}
private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
}
by. Ing. CIP. Jhonatan Abal Mejia
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
public class form2 extends javax.swing.JInternalFrame {
DefaultTableModel modelo = new DefaultTableModel();
public form2() {
initComponents();
setTitle("Consulta de Cargos");
//jtable
cabecera_tabla();
}
private void cabecera_tabla(){
modelo.addColumn("Cargo");
modelo.addColumn("Nombre");
jTable1.setModel(modelo);
}
private void buscar(){
try{
Conexion obj = new Conexion();
Connection cone = obj.getConnection();
PreparedStatement ps = cone.prepareStatement("Select cargo,nombre from cargo where nombre=?");
ps.setString(1, this.txtnom.getText().trim());
ResultSet rs = ps.executeQuery();
while(rs.next()){
Object [] fila = new Object[2];
fila[0] = rs.getString("cargo");
fila[1] = rs.getString("nombre");
modelo.addRow(fila);
fila=null;
}
rs.close();
cone.close();
}catch(SQLException ex){
JOptionPane.showMessageDialog(null, "Error Buscar Tabla: " + ex.getMessage());
}
}
private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {
buscar();
}
private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
}
by. Ing. CIP. Jhonatan Abal Mejia
Comentarios