Convert tuples from different databases into a particular Java object

Asked

Viewed 108 times

2

Well folks, I don’t know if the title of my question is quite correct, if not, someone please edit. I’ll explain my problem.

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

-> You are allowed to open a form.

-> It is forbidden for the user 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, dateInicio, dataFim).

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

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

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

If a table has the following structure:

ID - Type - Application - Action - Expiration date - Insertion date - Unit

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

And get a query of this? And how to do this even if the tables are different, there are 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 goes and if it is possible to do. Thank you very much.

  • 1

    Does this search and structure check need to be automated? I think it would add too much to the complexity of your program.

  • I deleted the other comment because I read it wrong. Of the structure not necessarily, for example, if I looked in the database, we identified which fields were equated, and only informed in the program I think would be of great value. In text form, I don’t know... Because what I want to do is to exempt.

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 at 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 driver specific to your bank.

For IBM DB2, see the page 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’re retrieving data from the database after executing the query, you enter the field name 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, there are Joins?

Basically you:

  1. Makes a different query for each type of bank you want.
  2. For each query you recover the values in variables, as in the above example.
  3. With these variables, you then create an instance of your class Politica passing the values as deemed appropriate and placing in the class 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 on the attributes of these objects.

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

  • Hi @utluiz, thank you so much for the answer. This part of JDBC really helps me a lot, and I think it can be that way. However, I wanted to abstract the user having to have the database manager to visualize the database structure, wouldn’t I have an SQL command or something so I could display it to him in the tool? Thank you!

  • @Joãoneto It is not easy to make a system that allows the user to enter any type of query. However, it is possible through the API DatabaseMetaData. Here are examples of how to use it: http://tutorials.jenkov.com/jdbc/databasemetadata.html

  • @Joãoneto You can even create a "query Wizard" so that the user can assemble queries through a visual interface and then you store the query created and execute it later. However, the complexity of doing this is very high and I would say that you would end up not delivering your TCC. Focus on what matters: first do the conflict detection logic, then do an implementation that reads this from any bank, then try to do something to make the interface simpler.

  • in case I’ve finished the tool and it’s already functional, I’ll present it this way understand? Only my teacher asked for help to continue this development, so now I no longer have so much restriction in time, I have a few months to develop just this. So this could be an option, the Wizard query, I’ll do a search on how it can be done.

  • I’ll take a look at this API too. Again, thanks for the layout!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.