Tema: JSP+ MySql
Ver Mensaje Individual
  #5 (permalink)  
Antiguo 28/08/2009, 10:26
Avatar de Rod_Man_mx
Rod_Man_mx
 
Fecha de Ingreso: agosto-2009
Mensajes: 69
Antigüedad: 14 años, 9 meses
Puntos: 4
Respuesta: JSP+ MySql

Muchas gracias, me ha sido de bastante utilidad todo esto, les dejo el codigo que salio despues de todo esto por si me pueden sugerir algo, o si alguien se enfrenta al mismo problema que yo:

Código:
public class ConexionDB {
    public String error = " ";
    public Connection con = null;
    public Statement stt = null;
    public ResultSet rSet = null;
   
    public ConexionDB(){
        super();
    }

    public boolean ConexionExitosa(){
        boolean resp = false;
        try{
            Class.forName("org.gjt.mm.mysql.Driver").newInstance();
            this.con = DriverManager.getConnection("jdbc:mysql://localhost/catalogo","root","root");
            resp = true;
            this.error = "Conexion exitosa!";
            
        } catch(Exception e){
            this.error = "Error al conectar debido a: \n" + e ;
        }
        return resp;
    }

    public DefaultTableModel busca(String query){
         int numCols = 0;
         DefaultTableModel modelo = new DefaultTableModel();
         ResultSetMetaData metaData = null;
         int i;

        if(ConexionExitosa()){
            try{
                this.stt = con.createStatement();
                rSet = stt.executeQuery(query);
                metaData = rSet.getMetaData();
                numCols = metaData.getColumnCount();
                Object [] etiquetas = new Object[numCols];
                Object [] fila = new Object[numCols];
                for( i = 0; i < numCols; i++){
                    etiquetas[i] = metaData.getColumnLabel(i+1);
                }
                modelo.setColumnIdentifiers(etiquetas);
                while(rSet.next()){
                    for(i = 0; i < numCols ; i++){
                         fila[i] = rSet.getObject(i+1);
                    }
                    modelo.addRow(fila);
                }
           } catch(Exception e){
                this.error = "no se pudo realizar la busqeda debido a " + e;
            }

        }
        return modelo;
    }
}