I'm new in Java development and I need some help with writing batch of queries in my java DAO class. In my DAO class I would like to write an update query that will update few tables. Something like:
UPDATE deals, vehicles SET deals.client= "client",
vehicles.vehicleIdNumber="123456"
WHERE deals.id ="123" and deals.id= vehicles.id;
This will work if I execute it in MySQL but if I want to do this in my DAO class I have to write two queries in batch. The problem is that I don't know how to do it. From what I've already researched I can only find some examples using JDBC, Spring, Hibernate, but I don't use any of this in my application.
When I do an update for a single table I have something like this:
public void updateDeal(String dealNumber, Integer dealType) {
Query upQ = em.createQuery(
"update Deals d set d.dealType = :dealType where d.dealNumber= :dealNumber")
.setParameter("dealType", dealType)
.setParameter("dealNumber", dealNumber);
upQ.executeUpdate();
How can I update two or more tables in one transaction similar to the above, but without using JDBC, Spring, Hibernate...
Tnx!
UPDATE: I realized that I haven't explained it well what I meant with no using JDBC...my mistake. So I have JDBC, I have connection with my DB and it's working fine with a single table statements. I actually want to know if there is other way of doing this rather than something like: http://w3processing.com/index.php?subMenuId=146 In this example you have to first get connection to the DB, than create statement and than add all the queries in the batch. When I'm creating a query for updating one table is pretty simple (it's exactly like I have shown in the code example above). I want to know if there is some simpler way of doing this for updating more than one table. I don't want to create each time a connection to my DB.
I've tried something like:
public void updateDeal(Integer id, String client,
String vechicleNumber)
Query query = em.createQuery("UPDATE Deals, Vehicles SET deal.client=:client, vehicle.vehicleNumber=:vechicleNumber WHERE deal.id =:id AND deal.id=vehicles.id ").
setParameter("client", client).setParameter("id", dealID).
setParameter("vechicleNumber ", vechicleNumber );
and I get this error:
According to the error in the console, I have synatax error:
Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: , near line 1, column 35