org.postgresql.util.PSQLException: No value specified for parameter 11 - java

I have 10 parameters set in the statement with the question marks and provided 10 associated values. At execution, it’s throwing this error “org.postgresql.util.PSQLException: No value specified for parameter 11.” I’m having a similar issue with another table where it’s also asking for an out of range parameter. I can manually run the query against PostGres without any problem. Please see log below, thanks in advance.
[DBOperations]:INSERT INTO product.SHIB_RP_MD_PROVIDER(RP_MD_PROVIDER_ID,MD_PROVIDER_TYPE_ID,MD_ID,SRC_ORG_ID,MD_NAME,MD_DESC,PARENT_RP_MD_PROVIDER_ID,LAST_UPDATE_USER,LAST_UPDATE_DATE,SYSTEM_IND,ORG_ID) VALUES (?,?,?,?,?,?,?,?,date_trunc('second' , now()),?,?)
[DBOperations]:No value specified for parameter 11. org.postgresql.util.PSQLException: No value specified for parameter 11.
[DBOperations] - Index 1 - Value 10042
[DBOperations] - Index 2 - Value 4
[DBOperations] - Index 3 - Value aa
[DBOperations] - Index 4 - Value 2
[DBOperations] - Index 5 - Value aa
[DBOperations] - Index 6 - Value null
[DBOperations] - Index 7 - Value 0
[DBOperations] - Index 8 - Value 1234
[DBOperations] - Index 9 - Value 0
[DBOperations] - Index 10 - Value 2

INSERT INTO dah53idm.SHIB_RP_MD_PROVIDER(RP_MD_PROVIDER_ID,MD_PROVIDER_TYPE_ID,
MD_ID,SRC_ORG_ID,MD_NAME,
MD_DESC,PARENT_RP_MD_PROVIDER_ID,
LAST_UPDATE_USERID,
LAST_UPDATE_DATE,
SYSTEM_IND,ORG_ID) VALUES (?,?,?,?,?,?,?,?,date_trunc('second' , now()),?,?)
well answer is pretty obvious . but i'll point it out anyway, you have 11 question marks which means you are supposed to add 11 values to execute the query but in your code you are adding only 10 , hence the exception "no value specified for 11"

assign all the parameters to the values properly
for (User user : users) {
System.out.println("Inserting Data for Userr name" + user.getName());
jdbcTemplate.update("insert into USERR(Id,Name,Dept,Salary)
values(?,?,?,?)",
preparedStatement -> {
preparedStatement.setLong(1,user.getId());
preparedStatement.setString(2, user.getName());
preparedStatement.setString(3, user.getDept());
preparedStatement.setLong(4, user.getSalary());
});

Related

Get all results that contain the largest value in table

Let's say I have a very simple table called test:
ID AGE NAME
1 12 Bob
1 13 Bob
2 13 John
3 9 Michael
3 11 Michael
I want to return all results that have the largest AGE in the table. So for this case, the result would be the entities with the age 13:
ID AGE NAME
1 13 Bob
2 13 John
I would think something like this should exist, my thought process would have been the following (even though I know the syntax would not work):
connection.getSqlQuery()
.from(test)
.max(test.age)
.list(test.id, test.age, test.name);
What would be the proper query for this?
You can create a subquery and use it in your where clause:
// Aggregate to get the maximum age
var subQuery = connection.getSqlQuery()
.select(test.age.max())
.from(test);
connection.getSqlQuery()
.from(test)
.where(test.age.eq(subQuery))
.list(test.id, test.age, test.name);
If you use JPA, you can also use var subQuery = JPAExpressions.select(...

Spring data query substring

I have a column called PAYMENT_REF that contains value of the following sequence:
DBC2020999999999999
It will always have the length 19 and starts with DBC followed by the year which is this part: DBC2020
The following numbers after the year is a sequence number which is 999999999999.
Basically I need to do a query that find the max value only from the sequence number ignoring the static DBC and year 2020.
Basically when I run the following query on oracle developer ide I get the correct result:
select MAX(SUBSTR(PAYMENT_REF,7,19))
from PAYMENT p
where PAYMENT_REF is not null;
So I tried the following query which looks should get the max value of:
public interface PaymentRepository extends JpaRepository<Payment, String> {
#Query(" select MAX(SUBSTRING(p.paymentRef,7,19)) from payment p where p.paymentRef is not null")
int getMaxRefNumber();
}
But I get a compilation error cannot resolve symbol: payment, any idea what I a missing here please?
I guess you should correct your query in the following way:
#Query("select MAX(SUBSTRING(p.paymentRef, 8, 12)) from Payment p where p.paymentRef is not null")
String getMaxRefNumber();
As it is stated in the documentation:
SUBSTRING
Extracts a portion of a string value. The second argument denotes the starting position, where 1 is the first character of the string. The third (optional) argument denotes the length.
1 8
| |
DBC2020999999999999
\ /
12 chars
P.S. To be honest I wasn't able to cast string to number in the hql.
The statement like the following:
select MAX( cast(SUBSTRING(p.paymentRef, 8, 12) as long) )
from Payment p where p.paymentRef is not null
will not work, the following exception is raced:
java.lang.IllegalArgumentException: Type specified for TypedQuery [java.lang.Long] is incompatible with query return type [class java.lang.String]
at org.hibernate.internal.AbstractSharedSessionContract.resultClassChecking(AbstractSharedSessionContract.java:863)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:817)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:23)

issue regarding postgres function test run in jmeter

i am testing a plpgsql function in jmeter.
The following sample is to replicate the issue. i have a table named sing with definition as follows
db=# \d sing
Table "schema1.sing"
Column
Type
id
bigint
valr
numeric
and my plpgsql function is as follows
create or replace function schema1.insissue(val text) returns text as $$
declare
_p text;_h text;
ids text[];
valid numeric := functiontochangetoid(val); // a sample function to change value into id.
slid bigint:= nextval('rep_s'); // sequence value
dup text := null;
begin
select array_agg(id) from sing where valr = valid into ids;
raise notice 'ids %',ids;
if coalesce(array_upper(ids,1),0) > 0 then
dup = 'FAIL';
end if;
raise notice 'dup %',dup;
if dup is null then
insert into sing values (slid,valid);
return 'SUCCESS'|| slid;
end if;
return 'FAIL';
exception
when others then
get stacked diagnostics
_p := pg_exception_context,_h := pg_exception_hint;
raise notice 'sqlerrm >> :%',sqlerrm;
raise notice 'position >> :%',_p;
raise notice 'hint >> :%',_h;
return 'FAIL';
end;
$$ language plpgsql;
simply in my function it checks if the value exist in valr column of sing table and if not exist inserts the value to the table.
now my jmeter config
to connect i use postgresql-42.2.14.jar.
when the ramp up period is 1 sec IE 200 request in one second the function creates duplicate values like this, when ramp up period is 100 sec no issue.
db=# select * from sing;
id
valr
897
1095
898
1095
89+
1095
900
1095
901
1095
902
1095
903
1095
but it shoul be actually like this
db=# select * from sing;
id
valr
897
1095
how can i avoid these type of duplicate values ? because my app will have high traffic may be 100 calls in second also i can't make "valr" column a primary key. because it contains other type of values.
my postgres version
db=# select version();
version
------------------------------------------------------------------------------------------------------------------
PostgreSQL 12.3 (Debian 12.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
at last found the solution , the transaction isolation to serialize works for my actual problem. checkout this link https://www.postgresql.org/docs/12/sql-set-transaction.html. The transaction is read committed by default. when we change transaction to serialization on a session it works.
To make a transaction serialized you can use set command on a session before any select query
SET transaction isolation level serializable;
it cannot be done inside a function or procedure in PostgreSQL only for sessions. we can use set in procedure but there will an error like this
NOTICE: sqlerrm >> :SET TRANSACTION ISOLATION LEVEL must be called before any query
NOTICE: position >> :SQL statement "SET transaction isolation level serializable"

String sort Based on number present on that String

I have some data in my SQL database like
1 Group 1
2 Group 1
11 Group 1
1 Group 2
1 Group 11
And I am querying like
String sortOrder = WiSeGroupContract.ColumnEntry.COLUMN_NAME_NAME + " COLLATE NOCASE";
contentResolver.query(WiSeGroupContract.ColumnEntry.CONTENT_URI,
null,
null,
sortOrder,
from,
limit);
And I am getting the output like
1 Group 1
1 Group 11
1 Group 2
11 Group 1
2 Group 1
but I wanted to like this
1 Group 1
1 Group 2
1 Group 11
2 Group 1
11 Group 1
The sorting based on the number in the string. It should be in alphabetical and also in ascending order of numbers.
in iOS, the database sorting will give the required output.
if(!sortDescriptors)
{
sortDescriptors = [NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:EntityAttributeName ascending:YES]];
}
[self groupWithPredicate :predicate sortDescriptors :sortDescriptors start :start limit :limit];
So is there any mechanism available for this? Or after fetching data, do I need to sort again?
How are they fetching data very much fast than android from the database with this sorting mechanism?

MongoDB How to write query for checking array of values for time intervals greater than and less than time

I have table name users, and I want to get the users between some
time intervals.
My users table
user_id,updated_at
1 100
2 200
3 300
4 400
5 500
6 600
7 700
Normal query for checking time of greater than and less than is below:
db.users.find({"updated_at":{$gt:90,$lte:200}})
I have map of timestamp for this
Map<Long,Long> timestamp: which contains values for greater than and less than time.
Now If I pass values in map like
timetampMap.put(90,200)
timetampMap.put(350,400)
timetampMap.put(560,700)
The output should be after query
select * from users where (updated_at>90 && updated_at<=200)
select * from users where (updated_at>350 && updated_at<=400)
select * from users where (updated_at>560 && updated_at<=700)
user_id,updated_at
1 100
2 200
4 400
6 600
7 700
Please help to write query according to the above requirement, I don't want to use for loop for this in code.
Use $or operator something like db.users.find({ $or: [ { updated_at:{ $gt:90, $lte:200}}, { updated_at:{ $gt:350, $lte:400}}, { updated_at:{ $gt:560, $lte:700}} ] })
This query is doing or for your different ranges.
You can read more about it here https://docs.mongodb.org/manual/reference/operator/query/or/

Categories

Resources