Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6216185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:11:22+00:00 2026-05-24T07:11:22+00:00

I have a groovy script that pulls a number of fields from a MYSQL

  • 0

I have a groovy script that pulls a number of fields from a MYSQL database and inserts the data into an HTML table, along with this I find the lastModified() date of the files that are pulled into the database.

I found a fairly similar topic: “Sorting files by lastModified()”, however my problem is that I never actually get the data into a map or list, instead I build the xml and call the query using sql.eachRow(query) to insert the proper fields into the table and then find the lastModified() date within the xml itself.

I can’t do the sort in my query because I have to get the lastModified() date from the groovy script. So I have two questions, is it possible to do a sort by last modified date using the set up in my code that I posted? And if so where would I need to actually perform the sort so that it is sorted correctly on the HTML table? I should say I am very new to Groovy and Java and am terrible when it comes to trying to figure out these kinds of things, so any hints as to where you found this information would be great.

Similar topics but ones that are not exactly what I am looking for:

  • How to sort rows of HTML table that are called from MySQL <– really looking at this but it is in php
  • http://groovy.329449.n5.nabble.com/Sorting-files-by-lastModified-td362691.html <–this but trying to sort a table
  • http://www.kryogenix.org/code/browser/sorttable/#totalsrows <–this would be nice but is it possible in groovy?

My code, I had to change some things:

sql = Sql.newInstance("location of database", "username", "password", "driver")
writer = new StringWriter()
def xml = new MarkupBuilder(writer)
rptDate = new java.util.Date()

query = 
"""
query stuff
"""
xml.html(){
xml.head(){
xml.title("Title")
xml.body(){
    xml.h1("Title")
    xml.table(border:1, cellpadding:5){
        xml.tr(){
            xml.th("ID")
            xml.th("Date Added")
            xml.th("Hospital Name")
            xml.th("Total Daily Clients")
            xml.th("Total Daily Pets")
            xml.th("Last Upload Date")//lastModified() date

            }//end headings

//insert data from query into each row of the table
    sql.eachRow(query)
      {row ->
         xml.tr(align:'center'){
            xml.td("${row.ID}")
            xml.td("${row.DateAdded}")
            xml.td("${row.HospitalName}")
            xml.td("${row.TotalDailyClients}")
            xml.td("${row.TotalDailyPets}")
                        //find lastModified() dates for incoming files and format them
            mod = new File("/home/me/folderforfiles/${row.ID}.zip").lastModified()
            fd = new Date(mod).format("EEE MMM dd hh:mm:ss a yyyy")
                       //insert into table
            xml.td(fd)
        }//end table data
    }//end loop
}//end table
}//end body
}//end title
}//end html
println writer.toString()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:11:24+00:00Added an answer on May 24, 2026 at 7:11 am

    If you load the SQL results into a List first, then you can sort this map, and iterate through it to generate your XML:

    sql = Sql.newInstance("location of database", "username", "password", "driver")
    writer = new StringWriter()
    def xml = new MarkupBuilder(writer)
    rptDate = new java.util.Date()
    
    query = 
    """
    query stuff
    """
    
    // Load the results into a list
    List rows = sql.rows( query )
    
    // Then manipulate the list to add the mod and fd fields
    rows.collect { 
      mod = new File("/home/me/folderforfiles/${row.ID}.zip").lastModified()
      fd  = new Date(mod).format("EEE MMM dd hh:mm:ss a yyyy")
      it << [ mod:mod, fd:fd ]
    }
    
    // Then sort it based on this field
    rows = rows.sort { it.mod }
    
    xml.html(){
      head {
        title "Title"
      }
      body {
        h1 "Title"
        table(border:1, cellpadding:5) {
          tr {
            th "ID"
            th "Date Added"
            th "Hospital Name"
            th "Total Daily Clients"
            th "Total Daily Pets"
            th "Last Upload Date"
          } //end headings
          rows.each { row ->
            tr( align:'center' ) {
              td row.ID
              td row.DateAdded
              td row.HospitalName
              td row.TotalDailyClients
              td row.TotalDailyPets
              td row.fd
            }//end table data
          }//end loop
        }//end table
      }//end body
    }//end html
    println writer.toString()
    

    I haven’t tried that, but it should do what you want to do… Let me know if you get any errors, and I’ll sort them out…

    Of course, loading all the rows into memory will not work if you have thousands and thousands of rows… If that is the case, then you should store the lastModified Date in the database, and sort the results using the sql query

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a very complex database migration script in Groovy, that runs just
I have a groovy script that needs a library in a jar. How do
I have a groovy script with an unknown number of variables in context at
Similar to this question I have a small groovy test script that basically uses
I have hierchical data stored in a spreadsheet that I am converting into an
I have a simple groovy script that generates xml def builder = new groovy.xml.StreamingMarkupBuilder()
Hey I have created a Groovy script that will extract the version numbers of
I have a groovy/grails application that needs to serve images It works fine on
I have to ship some groovy code to some users that have only java
I've got some soapUI tests, that use groovy scripts to first insert some data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.