How to Loop all tables in Sql Server using Java and extract the data
<p>The SQL DB has 110 tables and each table is having different column names. I have to loop through each table and fetch the data, so that I can write into a XML file.</p>

<p>For this, I created a new table called "MasterList" which holds all the 110 table names.</p>

<pre><code>try{
        // Connection for SQL Server.
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
         String url = "jdbc:sqlserver://"+strDBServer+":1433;DatabaseName="
                 + strDBName + ";" +
               "User="+strDBUser+";Password="+strDBPassword+";";
        Connection conn = DriverManager.getConnection(url);

        if (conn != null) {
            System.out.println("Connection Successful!");
        }

         //XML Transform
        TransformerFactory tFactory = TransformerFactory.newInstance();

        //Create a Statement object
        Statement sql_stmt = conn.createStatement();

        //Get List of all the tables present from Master table.
        ResultSet rs  = sql_stmt.executeQuery("SELECT TableName, Order FROM "
                + strDBName + ".[dbo].Master");


        while (rs.next())
        {             
            //Create a Statement object
            Statement sql_stmt_1 = conn.createStatement();

            String strTableName=rs.getString(1).trim();
            int intOrder = rs.getInt(2);

            hsMapTablesFromDB.put(strTableName,intOrder);


            System.out.println("Hashmap --&gt; " + hsMapTablesFromDB);

            ResultSet rs_1  = sql_stmt_1.executeQuery("SELECT Name, LevelOfExistence, UniqueId FROM "
                + strDBName + ".[dbo]." + strTableName);

            String strName = rs_1.getString(1).trim();
            String strUnique = rs_1.getString(3).trim();
            hsMapDataFromIndTable.put(strName,strUnique);

            System.out.println("hsMapDataFromIndTable" + hsMapDataFromIndTable);

        }
    }
    catch(Exception e){
        e.printStackTrace();
    }
</code></pre>

<p>As the column names in each table will be different, how to get the corresponding column names in "sql_stmt_1.executeQuery", so that once I get each record from the table, I have to insert it.</p>

<p>Like wise all the 110 tables data has to be inserted into XML file.</p>

<p>Please help me.</p>

<p>Thanks
Ramm</p>
