Convert tuples from different databases to a given Java object

2

Well folks, I do not know if the title of my question is correct, if it is not, someone edits please. I'll explain what my problem is.

I developed a tool that checks for conflicts between access control policies in Software as a course completion work. For example, it checks if the software has two policies that can not be executed by the user at the same time, such as the two policies:

  

- > It is allowed for user John to open a form.

     

- > It is prohibited for user John to open a form.

With this, my tool compares the Java objects called Politica, which has the second structure:

  

Policy (type, organization, user, action, object, date,   dataFim).

But I look for these policies in a database, such as my university, which is DB2. What I need to do and I have no notion of how to do is:

  

- > How to connect to the database and get its structure.

     

- > After getting its structure, define which columns in the database, refer to the attributes of my Policy object?

If the table has the following structure:

ID - Type - Application - Action - Date Validity - Date Insertion - Unit

How to make an equation and say that: Type = Type, Application = Object, Action = Action, Date Validity = End Date, and so on ...

And get a query of this? And how to do this even if the tables are different, let Joins?

Summarizing and trying to explain again: How to make any database that has policies return a Query with objects equal to my Policy object.

I need a north to know where it's going and if it's possible. Many thanks.

    
asked by anonymous 14.06.2016 / 23:25

1 answer

2
  

How to connect to the database and get its structure.

Each database has at least one client software for you to access. If you have DB in your college, ask for access and tools for the service maintainer.

On the Java side, all access to traditional banks is done using JDBC. The API is the same, you just need to find the specific driver for your bank.

For IBM DB2, see the DB2 JDBC Driver Versions and Downloads . Give preference to JDBC version 4.0 because it is more efficient.

  

After getting your structure, define which columns in the database, refer to the attributes of my Policy object?

When you are retrieving data from the database after executing the query, you inform the name the field and do whatever you want with the value. Example:

    ResultSet rs = stmt.executeQuery(query);
    while (rs.next()) {
        String valor = rs.getString("NOME_CAMPO");
        ...
    }
  

And get a query of this? And how to do this even if the tables are different, let Joins?

Basically you:

  • Do a different query for each type of bank you want.
  • For each query you retrieve the values in variables, as in the example above.
  • With these variables, you then create an instance of your Politica class by passing the values as you see fit and placing them in the class's attributes.
  • Once you have all the data in a uniform list of objects of type Politica , then you can create a method to go through this list and detect conflicts by making simple comparisons of the attributes of those objects.

    Note that there are several other ways to solve the problem, but I tried to synthesize it in a simple and straightforward way, without technical sophistication.

        
    15.06.2016 / 05:31