Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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 enric.mitabla;
- import java.sql.*;
- /**
- *
- * @author Enric
- */
- public class AccesoTabla {
- private Connection con = null;
- private Statement stmn = null;
- private ResultSet rs = null;
- private StringBuilder sb = null;
- private ResultSetMetaData rsmd = null;
- private int n = 0;
- public void AccesoTabla(){
- }
- public String Con_Tabla(String sql) throws SQLException{
- try {
- con = DriverManager.getConnection("jdbc:mysql://localhost:33331","DAF", "DTI");
- this.stmn = this.con.createStatement();
- this.sb = new StringBuilder();
- this.rs = stmn.executeQuery(sql);
- this.rsmd = rs.getMetaData(); //coge la metadata de la query
- this.n = rsmd.getColumnCount(); //da el numero de columnas
- this.sb.append("<table id='mitable' class = 'table mitabla table-bordered table-striped table-condensed'>");
- this.sb.append("<thead>");
- this.sb.append("<tr class='cabeceras'>");
- for (int i = 1; i <= n; i++){ //crea automaticamente los table headers
- this.sb.append("<th>" + rsmd.getColumnName(i) + "</th>");
- }
- this.sb.append("<tr>");
- this.sb.append("</thead>");
- this.sb.append("<tbody>");
- while(rs.next()){//llena automaticamente la tabla con el resultset sin tener que poner nada hardcodeado
- this.sb.append("<tr class='filas' >");
- for (int m = 1; m <= n; m++){
- this.sb.append("<td>" + rs.getString(m) + "</td>");
- }
- this.sb.append("</tr>");
- }
- this.sb.append("</tbody>");
- this.sb.append("</table>");
- }catch(Exception ex){
- System.out.println(ex.getMessage());
- ex.printStackTrace();
- }
- if (sb != null){
- return this.sb.toString();
- }else{
- return "error";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement