Oracle SQL Update nested within Select - java

Is it possible to nest an Update query within a Select query?
Or is there another method of updating rows given that the first word in the query must be 'Select' and ensuring that it is one query (ie. there is no semicolon between queries).
Furthermore, it must be accepted by JdbcTemplate query (which is why there must be no semicolons).
Thanks for the help.
=====
Update:
I'm trying to inject SQL to update table rows. The table structure doesn't matter but the table must already exist and the rows that it is trying to update must already exist as well.
Example query:
UPDATE users
SET username = 'Mike';
The only requirement is that the first word in the SQL query must be SELECT because that is the only basic sanity check that the server does. Is it possible to do something like this?
=====
Update:
One solution mentioned was to use PL/SQL to call an function that would mutate data. I'm currently looking to see if you can create this function and call it in one or more SELECT statement.

Related

Programmatically executing a query cannot find the entry. In SQL Developer works

I have a very strange behavior when I execute e query from Java code with Hibernate and when I call the same query directly from SQL Developer.
In the first case the entry could not be found but in the second case it is found.
1st case (Java code query):
getEntityManager().createNativeQuery("SELECT MAX(VERSION) FROM TIME_VERSION WHERE DIRECTORY=1").getResultList();
returns null.
2nd case (SQL Developer query):
SELECT MAX(VERSION)
FROM TIME_VERSION
WHERE DIRECTORY=1;
returns a value.
Important: A more strange behavior, is that, if I will edit any column value
from this entry (ex. VERSION) with SQL Developer and then I try to execute again the
Java Query from inside the code, it works correctlly and returns the same result as the query from the SQL Developer.
Does anybody knows why happens such a behaviour and what could be done to avoid it?
P.S. TIME_VERSION table has 7 Columns and a custom primary key. The database is an Oracle DB.
As your query is returning a single result, can you try
getEntityManager().createNativeQuery(
"SELECT MAX(VERSION) FROM TIME_VERSION WHERE DIRECTORY=1").getSingleResult()
I did found the problem:
When I was adding this entry in the database I had not done a commit at the end. I don't know why if you make a search query inside SQL Developer it will be found (even without the commit) but if you make a search from some external environment (like Java Hibernate) it will not be found! Maybe there is a more professional answer.
Solution: I just made a commit; at the end (after the insert query) and now is functioning correctly.
Thank you anyway!

multiple selects in batch using spring jdbc

I have to execute a query where there are multiple attributes in where clause and we have multiple such requests. eg. we have to execute below query
Select *
from Equipments
where equipId=?
and equipType=?
I have 1000s of set of these values like (1000,wireless),(2000,wire) . . . .
Which means i want to execute multiple select statements like
Select *
from Equipments
where equipId=1000
and equipType='wireless'
Select *
from Equipments
where equipId=2000
and equipType='wire'
how can i run this select statement in bulk and get the list of java Object mapped to Equipments table, instead of making one select at a time and going in loop .
While doing research on this, i found this and modified my query accordingly and i am able to do multiple select.
There is one limitation still left that in the 'in' clause we have a limitation on number of parameters being passed. If some someone know of that solution please comment

Possible in JDBC / MySQL to retrieve updated rows?

preparedStatement.executeUpdate()
Returns the number of rows updated. To my research so far it's not possible to do an update-query in which you would retrieve the updated rows, but this seems like such a basic feature that I'm clearly missing something. How to accomplish this?
Per first comment on question this is simply not possible in MySQL. PostgreSQL supports UPDATE...RETURNING as this feature.
If you use executeQuery instead of executeUpdate, you get a resultset back.
Then, change your stored procedure to be a function, and return the changed rows in a select at the end of the function. AFAIK, you cannot return data from a procedure in MySQL (as opposed to e.g. Microsoft SQL server).
EDIT: The suggestion struck out above is not possible. The JDBC specification does not allow updates in query statements (see the answer for this one: http://bugs.mysql.com/bug.php?id=692).
BUT, if you know the WHERE clause of the rows you are about to update, you can always select them first, to get the primary keys, perform the update, and then perform a select on them afterwards. Then you get the changed rows.
when you fire preparedStatement.executeUpdate() you already have the row identifiers using which you can uniquely identify the rows you want updated- you need to use the same identifiers to do a query and fetch the updated rows. you can not accomplish update and retrieval in one shot using JDBC apis.

can we use table name instead java class name in select or update queries in hibernate?

In hibernate we use class names generally in select and update queries.My question is it possible to direct give table name (if user wants in some scenario).For example :- in below queries i have table name as ContentModification.
select from ContentModification as cm where cm.XWC_NAME=:spaceName
update ContentModification set lastname="miles"
Another question can we use query object in hibernate to insert the data(like insert into ..). Every where i just see the examples of update ans select.
Create native query
But why bother with hibernate in the first place then ?

Deleting multiple rows in a one statement

I have some trivial table in database (let say Oracle10g) and I need to implement at DAO ability to delete multiple records. The method remove() receives as a parameter an array of ids (integers).
For now I have a query string "DELETE FROM news WHERE id = ?" which I use at PreparedStatement. I simply add batch for every id from array and then perform execute on PreparedStatement.
I wonder if there any ability to perform it through one query statement, something like "DELETE FROM news WHERE id IN ?". But I cannot find how to properly set an array of integers instead of '?'.
The same question applies to Hibernate and JPA. If there any constructions to solve this ? Because now I use batch-like-way: add Query to Session on every id from array and commit transaction.
The best I've seen done is to dynamically build the String used by the PreparedStatement, inserting the proper # of ?, sequences, then use a for loop to call setInt as appropriate for each row - each row to be deleted in your case.
JPA provides a special syntax for this (can accept a Collection to populate the list of arguments), since it has to create the SQL anyway - and likely does so very similar to how I just described. Specifics as to the API calls (for both JPA and HQL) are available at Hibernate HQL Query : How to set a Collection as a named parameter of a Query? .

Categories

Resources