0

I have this below code in which a request goes from browser through rest web service to the controller and then database connection get established through jdbc , my query is that i want to change my below code in such a way so that it can retrieve all the columns of the table , please advise how can i achieve the same

@GET
    @Produces(MediaType.TEXT_HTML)

    public String retriveData(@QueryParam("tablename") String tablename) throws SQLException
    {
        Connection con=null;
        PreparedStatement ps=null;
        String statement="";
        String retString="";

        try {
            //Class.forName("com.mysql.jdbc.Driver");
            //put sql jdbc jar in tomcat lib
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  

            System.out.println(" ******Driver Loaded *******");

            con=DriverManager.getConnection("jdbc:sqlserver://sv53720.hk.sunlife:1433; databaseName=pin_uat", "pinread","pinread123");
            con.setAutoCommit(false);

            System.out.println(" ******Connected To MSSql ******");

            System.out.println("FROM TABLE NAME : "+tablename);
            statement="SELECT * FROM "+tablename+";";

            System.out.println("STATEMENT : "+statement);
            ps=con.prepareStatement(statement);
            // Turn use of the cursor on.
            //ps.setFetchSize(50);
            ps.setMaxRows(10);
            ResultSet rs=ps.executeQuery();
            ResultSetMetaData rsmd=rs.getMetaData();
            String name=rsmd.getColumnName(1);
            while(rs.next())
            {

                retString=retString+name+" : "+rs.getString(name)+"<br>";
                System.out.println(retString);

            }

            System.out.println("Table FOUND!!!"); 
            ps.close();         
            rs.close();
            con.close();
            return retString;

        }catch(Exception e) {
            e.printStackTrace();

        }
        finally {
            if(con!=null)
                con.close();
        }

        return "Unable To Read Table :(";

    }
  • you need to query the information schema – lakshman Apr 06 '18 at 11:19
  • Possible duplicate of [Retrieve column names from java.sql.ResultSet](https://stackoverflow.com/questions/696782/retrieve-column-names-from-java-sql-resultset) – AxelH Apr 06 '18 at 11:21
  • 1
    You had a good start with `rsmd.getColumnName` but you can also know how many columns there is to get all the columns that way. Check [this answer](https://stackoverflow.com/a/3818712/4391450) from the duplicate. – AxelH Apr 06 '18 at 11:21

1 Answers1

0

Try this

   statement="SELECT * FROM "+tablename+";";
        ps=obj.getConnection().prepareStatement(statement);            
        ps.setMaxRows(10);

        ResultSet rs=ps.executeQuery();
        ResultSetMetaData rsmd=rs.getMetaData();
        int columnCount = rsmd.getColumnCount();

     // The column count starts from 1
     for (int i = 1; i <= columnCount; i++ ) {
       String name = rsmd.getColumnName(i);
       retString=retString+i+"- coloum name="+name+",";

     }          
     System.out.println(retString);
Gayathri Rajan
  • 369
  • 1
  • 6
  • 18