Generating HTML table output of database table

import java.sql.*;
import java.io.*;
public class JavaDemo124{
    public static void main(String ar[])throws Exception{
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:test","system","123456");
        Statement st=con.createStatement();
        String q="select *from users";
        ResultSet rs=st.executeQuery(q);
        ResultSetMetaData rsmd=rs.getMetaData();
        int cols=rsmd.getColumnCount();
        String tr="";
        while(rs.next()){
            String td="";
            for(int i=1;i<=cols;i++){
                String s1=rs.getString(i);
                td=td+"<td>"+s1+"</td>";
                System.out.print(s1+"\t");
            }            
            tr=tr+"<tr>"+td+"</tr>";
            System.out.println("");
        }
        String table="<table border='1'>"+tr+"</table>";
        FileWriter fw=new FileWriter("data.html");
        fw.write(table);
        fw.close();
        con.close();
    }
}

No comments:

Popular Posts