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

  • Home
  • SEARCH
  • 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 6606049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:21:17+00:00 2026-05-25T19:21:17+00:00

Hi I want to design a class using Groovy Sql which I will be

  • 0

Hi I want to design a class using Groovy Sql which I will be able to use in any of my future db related projects. At present I came up with the below class, which has separate functions for individual queries, I want to learn how can I make it generic! So that I just need to pass 1 query and some bind parameters (if not pass null) Please help me get a solution

My code

import java.sql.*;
import java.util.List;
import groovy.sql.Sql

public class ProductInfo {

  ReadProperty prop

  def sql
  public ProductInfo()
  {
    prop=ReadProperty.getInstance("db")
    sql = Sql.newInstance("jdbc:oracle:thin:@"+prop.getProperty("hostname")+":"+prop.getProperty("port")+":"+prop.getProperty("service"), "asimonc", "asimon","oracle.jdbc.OracleDriver")
  }

  public List executeSelection(String query)
  {
    List result=new ArrayList()
    sql.eachRow(query)
    { 
      String[] rows=new String[5]
      rows[0]=(String)it.id
      rows[1]=(String)it.name
      rows[2]=(String)it.description
      rows[3]=(String)it.active
      rows[4]=(String)it.release_date

      result.add(rows)
    }
    return result
  }

  public executeInsert(String query,Object[] paramValues)
  {
    sql.execute(query,[paramValues[0], paramValues[1],paramValues[2], paramValues[3], paramValues[4]])
  }

  public executeUpdation(String query,Object[] paramValues)
  {
    sql.executeUpdate(query,[paramValues[1], paramValues[2],paramValues[4],paramValues[5], paramValues[0]])
  }

  public int executeSelectMax(String query)
  {
    int max     
    sql.eachRow(query) 
    {
      max=it.max
    }
    if(max==null)
      return 0
    else
      return max
  }
}

My oracle table

CREATE TABLE PRODUCTINFO
(     "ID" NUMBER NOT NULL ENABLE, 
      "NAME" NVARCHAR2(200), 
      "DESCRIPTION" NVARCHAR2(200), 
      "ACTIVE" NVARCHAR2(2), 
      "RELEASE_DATE" DATE, 
       PRIMARY KEY ("ID")
)

for finding out max id (selection)

ProductInfo pinfo= new ProductInfo();
int max=pinfo.executeSelectMax("select max(id) as max from productinfo");

for updation

Object[] paramValues={iid,name,desc,"A",date,active};
pinfo.executeUpdation("update productinfo set name=?, description=?, release_date=?, active=? where id=?",paramValues);

for insertion

Object[] paramValues={max+1,name,desc,"A",date};
pinfo.executeInsert("insert into productinfo(ID,NAME,DESCRIPTION,ACTIVE,RELEASE_DATE) values (?,?,?,?,?)",paramValues);

for yet another selection

List result=pinfo.executeSelection("select ID,NAME,DESCRIPTION,ACTIVE,RELEASE_DATE from productinfo where ACTIVE='A'");
  • 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-25T19:21:17+00:00Added an answer on May 25, 2026 at 7:21 pm

    How about:

    import groovy.sql.Sql
    
    public class ProductInfo {
    
      ReadProperty prop
    
      def sql
      public ProductInfo() {
        prop=ReadProperty.getInstance("db")
        sql = Sql.newInstance( "jdbc:oracle:thin:@"+prop.getProperty("hostname")+":"+prop.getProperty("port")+":"+prop.getProperty("service"), "asimonc", "asimon","oracle.jdbc.OracleDriver")
      }
    
      public List executeSelection(String query) {
        List result = []
        sql.eachRow(query) { 
          result << [ it.id, it.name, it.description, it.active, it.release_date ]
        }
        result
      }
    
      public void executeInsert( GString query ) {
        sql.execute( query )
      }
    
      public void executeUpdation( GString query ) {
        sql.executeUpdate( query )
      }
    
      public int executeSelectMax( String query ) {
        sql.firstRow(query)?.max ?: 0
      }
    }
    

    Then, your update and insert examples become:

    update:

    pinfo.executeUpdation( "update productinfo set name=$name, description=$desc, release_date=$date, active=${'A'} where id=$iid" )
    

    insert:

    pinfo.executeInsert( "insert into productinfo(ID,NAME,DESCRIPTION,ACTIVE,RELEASE_DATE) values (${max+1}, $name, $desc, ${'A'}, $date )" )
    

    As you can see… a lot of your code is simply wrapping things that already exist in the groovy.sql.Sql class

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

Sidebar

Related Questions

I want to see if anyone has a better design for a class (class
I want to design a c# program which can run program a 3rd exe
Basic question: what design principles should one follow when choosing between using a class
If I want to work with XML, I usually design a class/bunch of classes
I am using CPropertySheet class for my design in MFC application, normally in CPropertySheet
I am writing a web application using PHP. I want to use the MVC
I want to create a JTabbedPane in a Visual Class using the Visual Editor
Often, when I want a class which is thread-safe, I do something like the
I'm building an application using the MVVM design pattern and I want to make
I have a design question. I'm using the HttpURLConnection class to navigate a website.

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.