Advertisement
Draconk

polladas

Nov 14th, 2014
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package enric.mitabla;
  7.  
  8.  
  9. import java.sql.*;
  10.  
  11.  
  12.  
  13. /**
  14.  *
  15.  * @author Enric
  16.  */
  17. public class AccesoTabla {
  18.    
  19.     private Connection con = null;
  20.     private Statement stmn = null;
  21.     private ResultSet rs = null;
  22.     private StringBuilder sb = null;
  23.     private ResultSetMetaData rsmd = null;
  24.     private int n = 0;
  25.  
  26.    
  27.     public void AccesoTabla(){
  28.        
  29.     }
  30.    
  31.     public String Con_Tabla(String sql) throws SQLException{
  32.  
  33.         try {
  34.             con = DriverManager.getConnection("jdbc:mysql://localhost:33331","DAF", "DTI");
  35.             this.stmn = this.con.createStatement();
  36.             this.sb = new StringBuilder();
  37.             this.rs = stmn.executeQuery(sql);
  38.             this.rsmd = rs.getMetaData(); //coge la metadata de la query
  39.             this.n = rsmd.getColumnCount(); //da el numero de columnas
  40.             this.sb.append("<table id='mitable' class = 'table mitabla table-bordered table-striped table-condensed'>");
  41.                this.sb.append("<thead>");
  42.                     this.sb.append("<tr class='cabeceras'>");
  43.                     for (int i = 1; i <= n; i++){ //crea automaticamente los table headers
  44.                         this.sb.append("<th>" + rsmd.getColumnName(i) + "</th>");
  45.                     }
  46.                     this.sb.append("<tr>");
  47.                 this.sb.append("</thead>");
  48.                 this.sb.append("<tbody>");
  49.                     while(rs.next()){//llena automaticamente la tabla con el resultset sin tener que poner nada hardcodeado
  50.                         this.sb.append("<tr class='filas' >");
  51.                         for (int m = 1; m <= n; m++){
  52.                             this.sb.append("<td>" + rs.getString(m) + "</td>");
  53.                         }
  54.                         this.sb.append("</tr>");
  55.                     }
  56.                 this.sb.append("</tbody>");
  57.             this.sb.append("</table>");
  58.            
  59.            
  60.         }catch(Exception ex){
  61.             System.out.println(ex.getMessage());
  62.             ex.printStackTrace();
  63.         }
  64.        
  65.         if (sb != null){
  66.             return this.sb.toString();
  67.         }else{
  68.             return "error";
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement