Solved

building java object from Dictionary type of input

Posted on 2004-08-16
12
222 Views
Last Modified: 2010-03-31
Hi, guys,

In Java how can i build a class which accepts multiple parameters and returns list type of object.

I need akind of solution where i want to build query from the given arguments.

I need a simple way of sending paramters to the query former which forms the query by using the query pattern and arguments List.

ex:

query pattern :

   select empname,empno from employee where empno = %empno%

arguments :
    empno = 5

resulting query from query Former

  select empname,empno from employee where empno = 5

Here i need a kind of solution where once I gave
empno = 5
 will return th query
how can i provide a solution to this in an easy manner

thanx in advance
regards
Jayaram

0
Comment
Question by:JayaramReddy
  • 7
  • 5
12 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 11809229
Use a prepared statement: http://java.sun.com/docs/books/tutorial/jdbc/basics/prepared.html and read the ResultSet back. Then the only thing you have to do is to read the values from the ResultSet and construct the object you want.
0
 

Author Comment

by:JayaramReddy
ID: 11810315
Hi girionis,
      thnx for ur reply.
This is not the solution I need

Here I want to build the query depending on the database

Query Pattern will come from diffrent sources depending on database. Query with the applied arguments need to be returned.

For ex
  if it is oracle data base  it should return
            select COLUMN_NAME,DATA_TYPE,TABLE_NAME,OWNER OWNER from SYS.ALL_TAB_COLUMNS where TABLE_NAME = 'employee'

  the correspingding entry in oracle bundle will be like this
            select COLUMN_NAME,DATA_TYPE,TABLE_NAME,OWNER OWNER from SYS.ALL_TAB_COLUMNS where TABLE_NAME = '%tabName%'


 if it is db2 database it should return

            SELECT DISTINCT SYSIBM.SYSCOLUMNS.NAME AS COLUMN_NAME, SYSIBM.SYSCOLUMNS.COLTYPE AS DATA_TYPE,SYSIBM.SYSCOLUMNS.TBNAME AS TABLE_NAME,TBCREATOR as TBCREATOR FROM   SYSIBM.SYSCOLUMNS WHERE  TBNAME='employee'

  the correspingding entry in db2 bundle will be like this

            SELECT DISTINCT SYSIBM.SYSCOLUMNS.NAME AS COLUMN_NAME, SYSIBM.SYSCOLUMNS.COLTYPE AS DATA_TYPE,SYSIBM.SYSCOLUMNS.TBNAME AS TABLE_NAME,TBCREATOR as TBCREATOR FROM   SYSIBM.SYSCOLUMNS WHERE  TBNAME='%tabName%'

i will give %tabName% = employee in any javaobject form then it should return one of the above query depending database wihch will be readed while starting the servlet.


so here i need to know what kind of object i need to build for for argument to pass
 the argument here is %tabName% and employee

with regards,
Jayaram
0
 
LVL 35

Expert Comment

by:girionis
ID: 11810653
Well the %tabname% is a String I guess so you need to pass a String to the corresponding entyr in the db. How do you construct the SQL statement? I guess you have some placeholders somewhere and then you just append the name of the table dynamically.
0
 

Author Comment

by:JayaramReddy
ID: 11838097
Hi girionis,
     Here this operation is not regarding the database. Here I need form the query depeinding on database type. I am not executed the query. I just want ot form the query.

In C++ we have Enum class

Enum BoilingPoints
        Celcius = 100
        Fahrenheit = 212
    End Enum 'BoilingPoints

here the input is of dictionary type  then it will create Enum Object later on we can use this values for any purpose.

     Here I want the same type of class which will accept this type of input and gives me javaobject for Ex : HashMap. Then i will substitute this values for the corresponding variable in the query like

     select * from temprature where maxTemp = %Celcius% and maxFar = %Fahrenheit%

the substitution will happen in seperate class which will return me the query

     select * from temprature where maxTemp = 100 and maxFar = 212

if I gave BoilingPoints as input

Like this I have Plenty of requirements at lot of places in my code

  I don't want to give complicated input for froming the query. A simple way I need to form the query.

      If it is C++ I will use ENUM class but in Java I need to find the corresponding class.

      If you gave me the solution for this I can Minimize My coding

thanx
with regards
Jayaram
0
 
LVL 35

Expert Comment

by:girionis
ID: 11838603
Java does not have an enum in the same sense as C++ does. In Java the closest you can get to an enum as a java class. So your BoilingPoints can be represented:


public class BoilingPoints
{
    private int celcius = 100;
    private int fahrenheit = 212;

    // Default constructor
    public BoilingPoints()
    {
    }

    // Constructor used to initialize the celcius and fahreneit variables
    public BoilingPoints(int celcius, int fahrenheit)
    {
        this.celcius = celcius;
        this.fahrenheit = fahrenheit;
    }

    public void setCelcius(int celcius)
    {
        this.celcius = celcius;
    }

    public int getCelcius()
    {
        return celcius;
    }

    public void setFahrenheit(int fahrenheit)
    {
        this.fahrenheit = fahrenheit;
    }

    public int getFahrenheit()
    {
        return fahreneheit;
    }
}

Now you can create objects of the above class like

BoilingPoints bp = new BoilingPoints();  // will default to 100 celcius and 120 fahreneheit
BoilingPoints bp2 = new BolingPoints(90, 110); // will set celcius to 90 and fahreneheit to 110

If you want to get the values simple do:

fahrenheit = bp.getFahrenheit();
celcius = bp.getCelcius();

I am not sure if this helps, if you need more clarification please let me know.
0
 

Author Comment

by:JayaramReddy
ID: 11841265
Hi girionis,
   This solution will not suite me becoz if I have one requirement then I can go for it. But through out my product I need to use it. Each time I can't create a seperate class with getter and setter methods.

   I have one solution like this.

    String[] paramArr = new String[] {'celcius ','farenheit'};
    String[] valueArray =  new String[]{'100','212'};
    queryName = "tempQuery"

for query of oracle database it will give me

if the pattern in query bundle for oracle
     tempQuery=select * from temprature where maxTemp = %celcius% and maxFar = %farenheit%

     select * from temprature where maxTemp = 100 and maxFar = 212


for query of db2 database it will give me

if the pattern in query bundle for db2
     tempQuery=select * from temprature1 where maxTemp1 = %celcius% and maxFar1 = %farenheit%

     select * from temprature1 where maxTemp1 = 100 and maxFar1 = 212
   

If I gave some data like this
        String[] paramArr = new String[] {'tabName ','colName','colType'};
        String[] valueArray =  new String[]{'SYSTEM','EMPNAME','varchar2'};
        queryName = 'tableQuery'

For Oracle Data base query former will give me

if the pattern in query bundle for oracle

           tableQuery=select COLUMN_NAME,DATA_TYPE,TABLE_NAME,OWNER OWNER from SYS.ALL_TAB_COLUMNS where TABLE_NAME = '%tabName%' and column_name = %coluName% and datatype = %dataType%

           select COLUMN_NAME,DATA_TYPE,TABLE_NAME,OWNER OWNER from SYS.ALL_TAB_COLUMNS where TABLE_NAME = 'SYSTEM' and column_name = 'EMPNAME'  and datatype = 'varchar2'


For Db2 Database the query former will give me  

if the pattern in query bundle for db2

       tableQuery=SELECT DISTINCT SYSIBM.SYSCOLUMNS.NAME AS COLUMN_NAME, SYSIBM.SYSCOLUMNS.COLTYPE AS DATA_TYPE,SYSIBM.SYSCOLUMNS.TBNAME AS TABLE_NAME,TBCREATOR as TBCREATOR FROM   SYSIBM.SYSCOLUMNS WHERE  TABLE_NAME = '%tabName%' and column_name = %coluName% and datatype = %dataType%


       SELECT DISTINCT SYSIBM.SYSCOLUMNS.NAME AS COLUMN_NAME, SYSIBM.SYSCOLUMNS.COLTYPE AS DATA_TYPE,SYSIBM.SYSCOLUMNS.TBNAME AS TABLE_NAME,TBCREATOR as TBCREATOR FROM   SYSIBM.SYSCOLUMNS WHERE TABLE_NAME = 'SYSTEM' and column_name = 'EMPNAME'  and datatype = 'varchar2'


Send this params to any java oject which substitutes the values for patterns and returns the query.

   But this too I don't want to do becasue my code will become huge if I use this one.

   My Problem will be realy solved if I can able to send multiple paramters to any class. I think this is also not possible in Java.

  throught my code I need to use this one so I need Efficient Solution for this

I will have queries for oracle sperately, query for db2 seperately, query for postgre seperately so I need simple way of passing parameters to the query patterns


thanx
with Regards
Jayaram
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 35

Expert Comment

by:girionis
ID: 11842016
Do you know before hand how the query will look, how many parameters you will be passing and what type they are or are they dynamic?

> Each time I can't create a seperate class with getter and setter methods.

You do not. Just create the class by passing the parameters once. Then in the class where you create the query simply do:

tempQuery= "select * from temprature where maxTemp = " + object.getCelcius() + "and maxFar = " object.getFahrenheit(); and you are set.
0
 

Author Comment

by:JayaramReddy
ID: 11849231
yes here everything is dynamic

  Query Pattern is two dimensional dynamic dynamic it will be from one of Query Bundles

  here oracle will have one bundle, db2 will have on bundle, SQL server will have one bundle like that each db will have one bundle of query patterns.

 Each query inside each bundle may have diffrent no of params and diffrent no of param names.

 I will just pass the query pattern id and parameters and theier values.
 depending on these inputs it should return the query for correct database and correct values

 
0
 
LVL 35

Expert Comment

by:girionis
ID: 11849780
Then I would suggest create different classes for different ids and put them in a hashmap. Then get each relevant class based on the id, something like:

Parameters1 param1 = new Parameters1(...);
Parameters2 param2 = new Parameters2(...);
Hashtable table = new Hashtable();
table.put(new Integer(1), param1);
table.put(new Integer(2), param2);
...

Then in your method do something like:

Parameter param = (Parameter) table.get(new Integer(id));
//construct querry
"SELECT * FROM mytable WHERE p1 = " + param.getPar() ...

Also make all your classes have a parent class/interface so they have the same type.
0
 

Author Comment

by:JayaramReddy
ID: 11870447
Here you specifed
     Then I would suggest create different classes for different ids and put them in a hashmap.

but i want something where i don't want to create diffrent classes. but instead i want create diffrent objects of same claass for each id. if I follow this then my usability of the class will be easy and writing the class will be more diffuclt. I want the usability of the class to be simple. if i gave some parameter and the query id in simple way it has to construct the query from the given query pattern which will be inside any repositary

regards,
Jayaram
0
 
LVL 35

Accepted Solution

by:
girionis earned 350 total points
ID: 11880836
If you care about reusability and extensibility you could create an interface, or even better an abstract class, and make all the subclasses implement/extend the interface/subclass.

If you want to send multiple parameters to only one class and return the appropriate query then I suggest you have several methods and call the appropriate method by passing a the id. You will need to pass all your parameters in a vestor or another collectioon object:

public class Queries
{
    private Vector parameters = null;

    public Queries(Vector parameters)
    {
        this.parameters = parameters;
    }

    public void setVector(Vector parameters)
    {
        this.parameters = parameters;
    }

    public Vector getVector()
    {
        return parameters;
    }

    public String getQuery(int id)
    {
        String query = null;
        switch (id)
        {
            case 1: query = getQuery1(); break;
            case 2: query = getQuery2(); break;
            case 3: query = getQuery3(); break;
        }

        return query;
    }

    public String getQuery1()
    {
        String Buffer sb =  new StringBuffer("select empname,empno from employee where empno = ");
        sb.append(append( ((Integer)parameters.elementAt(0)).intValue() );
        return sb.toString();
    }
}

Do likewise for the rest of the methods. Then you can us it like:

Vector v = new Vector(1);
int empno = 10;
v.addElement(new Integer(empno));
Queries queries = new Queries(v);
String query1 = queries.getQuery(1);

v.addElement("parameter2");
queries.setParameters(v);

String query2 = queries.getQuery(2);
...
...
0
 
LVL 35

Expert Comment

by:girionis
ID: 11930517
:)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now