I have some task to do and can't figure out how to reach the result.
For example i have some STRING like this :
key1=value1&key2=value2|key3=value3
This string like key=value with options AND or OR.
I have also some map that contains following things:
key1=ke1Mapped; key2=key2Mapped; key3=key3Mapped;
I want to build some sql Select * from table1 where
And insert mapped keys and its values, but also to take into account & and | , i.e. AND or OR to use in sql query when it's needed.
Actually, finally sql query should be:
Select * from table where ke1Mapped=value1 AND key2Mapped=value2 OR key3Mapped=value3
I tried to split the main string and convert to MAP and then to loop over the list of mapped values and build dynamically sql. But i don't know what is AND or OR.
Tried to push it in two stacks , also were complicated.
I try now to understand how to build only architecture of this flow. And can't figure out what approach is needed here.
Anyone can suggest something?
Thanks
Related
I have to work with a POJO "Order" that 8 fields and each of these fields is a column in the "order" table. The DB schema is denormalized (and worse, deemed final and unchangeable) so now I have to write a search module that can execute a search with any combination of the above 8 fields.
Are there any approaches on how to do this? Right now I get the input in a new POJO and go through eight IF statements looking for values that are not NULL. Each time I find such a value I add it to the WHERE condition in my SELECT statement.
Is this the best I can hope for? Is it arguably better to select on some minimum of criteria and then iterate over the received collection in memory, only keeping the entries that match the remaining criteria? I can provide pseudo code if that would be useful. Working on Java 1.7, JSF 2.2 and MySQL.
Each time I find such a value I add it to the WHERE condition in my SELECT statement.
This is a prime target for Sql Injection attacks!
Would something like the following work with MySql?
SELECT *
FROM SomeTable
WHERE (#param1 IS NULL OR SomeTable.SomeColumn1 = #param1) OR
(#param2 IS NULL OR SomeTable.SomeColumn2 = #param2) OR
(#param3 IS NULL OR SomeTable.SomeColumn3 = #param3) OR
/* .... */
Hi dear stackoverflow users,
Firstly,i want to specify that my platforms are JAVA, ORACLE and TOAD.
I have some SELECT queries that include parameters and i have stored them in properties file to make them more readable. I can use them with '?' and setTYPE in JAVA.
But i have to use (:) operator to define parameters in TOAD. So, when i want to run my query in TOAD, i copy the query from JAVA properties file and paste it to TOAD editor and add parameters manuelly. This is not professional i think. I want to store my queries with : operator and in JAVA platform. Is this possible ? Are there any framework or something like it ?
NOTE :
1-) i want to store my queries with my JAVA code, not in stored procedure.
2-) i want to store my queries that can run that two platform without change anything.
Thank you.
You can use the OraclePreparedStatement:
String query = myProperties.getProperty("some_key");
//Suppose query is 'SELECT * FROM MY_TBL WHERE ID=:myId'
OraclePreparedStatement statement = (OraclePreparedStatement) myConnection.prepareStatement(query);
statement.setStringAtName("myId", "abc2");
someResultSet = statement.executeQuery();
In a webApplication have 900pages java class & jsp pages, 920 tables,250 procedure,430 functions. now my question is that suppose that there is table "employee_Table" now i wanna know that in which class using this table and in which jsp page using this table, and in which procedure and also in which functions using this table.
I am working on it from 2 days but unable to get any logic so kindly help it.
A good starting point is probably to search all the sources (Java, DDL and SQL) for the string employee_Table (case insensitive, please). The reasoning here is that this string is pretty unique so you won't get many unwanted search results.
The next step is to use an enum or create a new type TableName and replace all string literals with it. That way, you can now tell your IDE to search for all places where the enum or TableName constant is being used.
I have a form with 8 fields and based on the values entered in them I have have to fetch the records from the DataBase. Now the problem is out of the 8 fields the user may fill any number of fields and that too in any order for example the user may fill fields 1,4 and 6 or he may fill 1 and 7 or he may fill all of them (of course he has to fill at least one field)... Now how will I write a query which will work for any number and order of input parameters? and also because this query will be used in reporting(iReport) I am not allowed to write any code with it , it has to be a SQL query. Any ideas
Thanks
there are a number of ways to do this. I have a blog post about doing something like this in a Microsoft SQL (T-SQL) stored procedure at http://code.scottshipp.com/2013/03/29/tutorial-stored-procedures-with-truly-optional-parameters/ but it is likely that you will want to do something more complex and/or you are not using MS SQL Server. You may have to write the query fragments yourself. My suggestion is to do something like the following:
Once the form is submitted, loop through the various fields in the form, and check if their values are empty or not.
For those that are not empty, add a string containing an appropriate corresponding query fragment to some collections object, like an ArrayList. Do not include "AND" or "OR" directly in this string. If you need to keep track of whether this query fragment gets "AND"ed or "OR"ed with other query fragments, track that in a separate collections object. The "query fragment" I'm talking about is what would show up in the "where __" portion of your SQL query. For instance, say the search was for someone's last name. The query fragment you add is a string that says "lastName='" + lastNameField.value() + "'".
Once you have iterated through all the various fields in the form and have a final collections object full of query fragments, construct the final SQL statement from it. Iterate through your collections object (ArrayList in this example) and connect each one with the appropriate "AND" or "OR". Say your ArrayList has the three fragments "firstName='Joe'", "middleName='Q.'", "lastName='Public'". Use a StringBuilder to keep adding these fragments together into a final where clause: "firstName='Joe' AND middleName='Q.' AND lastName='Public'"...you may want to change these to a "LIKE" style query with wildcard characters.
You now have everything you need to create the final select statement. Issue it to the database and retrieve your results!
I currently search the database to get certain results with a pl/sql query like this:
SELECT
*
FROM
citrostats cs
WHERE
(
trim(upper(cs.name)) like trim(upper('%'|| ? ||'%'))
OR
trim(upper(cs.UCODE)) like trim(upper('%'|| ? ||'%'))
)
ORDER BY NAME DESC
I reorganised this, and fetched all rows into Lists of Objects with have the respective columns as String attributes.
What I need is a java code that would search attributes that are String type to give the same set of objects as results like this query has.
Anyone can help?
There is String#contains:
if (name.toUpperCase().contains(uppercasedSearchString))
Since you are doing this in a loop, save work by upper-casing the search string only once before the loop.