Java JDBC UPDATE Add Number? - java

So let's say I have an int column in MySQL. I want to update that column by adding to it, without running a SELECT query to get the number and add it. Is this possible?

update tablename set field = field + 1 where condition
This is a direct MySql update command to do so. You didn't mention if you were using a specific ORM like Hibernate or anything, but this same concept can be applied in HQL etc.

Related

Update Statement with hibernate switching unique key fields

I'm not sure exactly where the error is coming from, unfortunately, but I have a guess and would like to know the best way to solve it.
Problem
Suppose we have the following table in the database
ID
Field A
Field B
Field C
1
A
C
Something
2
B
C
Something else
And we have two unique indexes on the table
Unique-Index1 (ID)
Unique-Index2 (FieldA, FieldB)
Now I am loading both entities
Session session = ...();
Transaction tx = session.beginTransaction();
TestTable dataset1 = (TestTable) session.get(TestTable.class, 1);
TestTable dataset2 = (TestTable) session.get(TestTable.class, 2);
And now I want to do something like this
update testtable set fielda = 'B' where id = 1;
update testtable set fielda = 'A' where id = 2;
So at the end the unique key is not violated, but after the first statement, the unique index is violated.
In my JAVA application it looks like this
dataset1.setFieldA("B");
dataset2.setFieldA("A");
session.saveOrUpdate(dataset1);
session.saveOrUpdate(dataset2);
tx.commit();
After executing the application I get the following exception
Could not execute JDBC batch update
Unfortunately, the error is not really meaningful. Also, I don't get any information whether it might be a duplicate or not. But if I delete the unique index, it works. So my guess is that it is because of that.
Used frameworks / systems
Java 17 SE application, using Hibernate 3.2 (very old version) with the legacy mapping XML files (so still without annotations). The database is an IBM Informix database.
The database model, as well as the indexes are not generated by Java, but by regular SQL scripts.
I can't change anything about the versions of Hibernate or the database either, unfortunately. Also I cannot influence how the index was created. This all happens outside the application.
Idea
The only idea I had was to first change all records that need to be changed to fictitious values and then set the correct values again. But that would mean that two update statements are triggered per record, right?
Something like this:
dataset1.setFieldA("XXX");
dataset2.setFieldA("YYY");
session.saveOrUpdate(dataset1);
session.saveOrUpdate(dataset2);
dataset1.setFieldA("B");
dataset2.setFieldA("A");
session.saveOrUpdate(dataset1);
session.saveOrUpdate(dataset2);
tx.commit();
However, I am not even sure if I need to commit the transaction. Maybe a flush or something similar is enough, but the solution is not really nice. I can kind of understand the problem, but I would also have thought that this would be legitimate within a transaction then - only at the end of the transaction the constraints have to be correct.
Many greetings and thanks for your help,
Hauke
You have two options. Either you configure the unique constraint to be "deferrable" and also mark it as "initially deferred" so that the constraint is only enforced at transaction commit time, or you delete and re-insert the entries.
I would suggest you to use the first option if your database supports this. You didn't specify which database you are using, but PostgreSQL supports it. You'd only have to run alter table test_table alter constraint your_unique_constraint deferrable initially deferred.

How to add 1 on the "like" value using QueryDSL

In my postgres db, I have a cell to save how many people clicked "i like" for the corresponding post. However, I am wondering if my current solution is concurrent in postgres. What I did now is two steps in my dao class.
1) fetch the current value in DB.
2) add 1 and then update it.
If no, how can I make it concurrent? I am using Java (querydsl) and Postgres. Can any one help, please?
Update:
Thanks for the answers. One thing I didn't make clear is that I use querydsl. So I don't use normal column way. But still thanks.
Assuming your table is named posts and you have a QPosts query class, then you can do:
QPosts p = QPosts.posts;
queryFactory.
update(q).
set(q.likes, q.likes.add(1)).
where(q.id.eq(42));
This would then generate the following SQL:
update posts
set likes = likes + 1
where posts.id = 42
I've never do PostgreSQL but I found something.
UPDATE tableName
SET likes = likes + 1;
Tell me if it's good,
Happy if I help you !

Liquibase : build where clause with java

I want to execute an update on a row using Liquibase but how can I set where param ? i can find the setter but I don't know what to put in it ? where id=value
UpdateDataChange updateDataChange=new UpdateDataChange();
columnConfig = new LoadDataColumnConfig();
columnConfig.setName(targetFieldKey);
columnConfig.setType("String");
columnConfig.setValue(value);
updateDataChange.addColumn(columnConfig);
Usually you put a condition in the where clause which makes the row(s) you want to update unique.
If you want to update one row, the most obvious choice would be to use the identifier of the table. Updating multiple rows could involve a condition with a LIKE.
For example: WHERE name LIKE "%test%", would update all the rows where the name contain test.
It always depends on the use-case.

update multiple row with a query in mysql using java

Helo,
I am a beginner java programmer.
I need to update multiple rows with a query using mysql database and java codes.
I need to update the age field (data type int) in the database based on the current date. I believe I need to iterate and use the hasnext ... but I just unable.
If you need to update all the rows using a common logic based on current date, write a single update query and execute it. It will update all the rows. If logic is different then use updatble result set.
Are you facing a problem in executing an update query or is it with the iteration on integer array. Please provide more details. And if you are attempting to update similar data, try doing it using a single query, as executing queries in loop is not recommended.

Netbeans jTable custom WHERE clause

Something simple I guess... I have a jTable created by Netbeans, which is bound to a mysql DB.
The table is getting all the data from the table, but I only need to get data with a particular field value. Netbeans creates a class automatically, but I can't find a place to add a WHERE clause.
For example I need the table to execute the final query as, SELECT * WHERE ID = 123;
select * where ID = 123
is incorrect as a query. You should research first, in 30 seconds I've found this, which I believe solves the problem.

Categories