database - Java Servlet - Showing Column Names -


i've been working on java servlet selects data table input user in index.html. i've encountered problem, because have db numerous different ids , show column names in table - right shows data without column names , i've been seemingly unable find solution or come 1 on own

start.java:

package start; import java.io.ioexception; import java.io.printwriter; import java.sql.connection; import java.sql.drivermanager; import java.sql.resultset; import java.sql.resultsetmetadata; import java.sql.statement; import java.sql.sqlexception; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse;  /**  * servlet implementation class start  */ @webservlet("/start") public class start extends httpservlet {     private static final long serialversionuid = 1l;     public static void getcolumnnames(resultset rs) throws sqlexception {         if (rs == null) {           return;         }         resultsetmetadata rsmetadata = rs.getmetadata();         int numberofcolumns = rsmetadata.getcolumncount();           (int = 1; < numberofcolumns + 1; i++) {           string columnname = rsmetadata.getcolumnname(i);           string tablename = rsmetadata.gettablename(i);           system.out.println("column name=" + columnname + " table=" +     tablename);     }   }  /**  * @see httpservlet#httpservlet()  */ public start() {     super();     // todo auto-generated constructor stub }  /**  * @see httpservlet#doget(httpservletrequest request, httpservletresponse  *      response)  */ protected void doget(httpservletrequest request, httpservletresponse respond) throws servletexception, ioexception {     printwriter pw = respond.getwriter();     respond.setcontenttype("text/html");     string tb = request.getparameter("table");     try {          class.forname("com.mysql.jdbc.driver").newinstance();         connection con = drivermanager.getconnection("jdbc:mysql://localhost/music","root","1234");         statement st = con.createstatement();         system.out.println("connection established successfully!");         resultset rs = st.executequery("select * " + tb);          pw.println("<table border=1>");         while (rs.next()) {             pw.println("<tr><td>" + rs.getint(1) + "</td>" + "<td>" + rs.getstring(2) + "</td>" + "<td>" + rs.getstring(3)                     + "</td></tr>");         }         pw.println("</table>");         pw.close();     } catch (exception e) {         e.printstacktrace();     }     // todo auto-generated method stub     // response.getwriter().append("served at:     // ").append(request.getcontextpath()); }  /**  * @see httpservlet#dopost(httpservletrequest request, httpservletresponse  *      response)  */ protected void dopost(httpservletrequest request, httpservletresponse response)         throws servletexception, ioexception {     // todo auto-generated method stub     doget(request, response);     } } 

this result looks like: 1 appreciated!

you should use resultsetmetadata retrieve meta data result. you'll retrieve column names using resultsetmetadata#getcolumnname(int)

resultsetmetadata rsmd = rs.getmetadata(); pw.println("<table border=1>"); pw.println("<tr>"); (int = 1 ; <= rsmd.getcolumncount() ; i++) {     pw.println("<th>" + rsmd.getcolumnname(i) + "</th>"); } pw.println("</tr>"); while (rs.next()) {     pw.println("<tr><td>" + rs.getint(1) + "</td>" + "<td>" + rs.getstring(2) + "</td>" + "<td>" + rs.getstring(3)             + "</td></tr>"); } pw.println("</table>"); 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -