Cant get data from sql array with JDBC and HSQLDB - jdbc

This code:
Array a=rs.getArray("curAccs");
ResultSet rs1=a.getResultSet();
int rs1size=rs1.getFetchSize();
return empty rs1, but a is ARRAY[221,222]
I also cant convert sql array to java
(Integer[])a2.getArray()
printing
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer;
How to fix it?

The array contents can be retrieved with these calls:
java.sql.Array a = rs.getArray("curAccs");
Object[] values = (Object[]) a.getArray();
Integer intValA = (Integer) values[0];
Integer intValB = (Integer) values[1];

Related

Web3j: Get value of transaction token transfered using Transaction Hash

With Web3j (not Web3js), how do I get the value of token transferred using transaction hash?
I found this example for web3j:
String transactionHash = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
TransactionReceipt transReceipt = web3j.ethGetTransactionReceipt(transactionHash).send().getTransactionReceipt();
Transaction transaction = ethTransaction.getResult();
int size = tokenImpl.getTransferEvents(transReceipt).size();
size = size -1;
BigInteger valueTransaction = tokenImpl.getTransferEvents(transReceipt).get(size).value;
The value will be recieved in BigInteger

How to count distinct fields in mongodb java Api

I need to find count of a distinct field. I used MongoCollection.distinct() which returns DistinctIterable. But it does not have any size method. To find the size I need to iterate DistinctIterable and find the size. Is there any method by which I can find the size of the distinct field values with out iterating it?
MongoCollection collection = db.getCollection("test");
DistinctIterable disIterable =collection.distinct("name");
int count =0;
Iterator iterator = disIterable.iterator();
while(iterator.hasNext()) {
iterator.next();
count = count +1;
}
try use it !!
public long returnSize(){
MongoCollection collection = db.getCollection("test");
DistinctIterable disIterable =collection.distinct("name");
return disIterable.into(new ArrayList<>()).size()
}

Postgres datatype conversion differ on Ubuntu and windows

I am getting following exception on windows while running the below
ERROR: operator does not exist: numeric = character varying Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts." while executing with query parameter
I am passing the Numeric String for parameter using function as a named parameter to the query
getUIDCount(String id) {
...
select count(UID) as icrd FROM UID_tbl WHERE id = ?
...
}
where id is numeric(5,0)" in table
Everything works well on Ubuntu but getting Error while running the same code on windows. I have to do the explicit casting just for windows. I am using PostgreSQL 9.4.3. I am using "org.hibernate.dialect.PostgreSQLDialec" and grails 2.3.11 with runtime 'org.postgresql:postgresql:9.3-1100-jdbc41'
updated with how it is getting called
def Integer getUIDSetSize(String _id)
{
Integer i = 0;
Sql sql = new Sql(dataSource);
String sqlt = """select count(UID) as icrd FROM UID_tbl WHERE _id = ?""";
log.trace(sqlt);
sql.eachRow(sqlt, [_id], { row -> i = row.icrd; });
return i;
}
This how it get called def _id1 = params._id1; count1 = HelperService.getUIDSetSize(_id1)
The workaround for casting from varchar to numeric is
CREATE CAST(VARCHAR AS NUMERIC) WITH INOUT AS IMPLICIT;
This is not the best solution and would suggest selective casting from the code.

Linq Query Message: Specified cast is not valid

Following are the columns name and its data type:
TemplateName string
TaskName string
AvgDays string (but contains int values)
my LINQ query:
DataTable newTable = new DataTable();
newTable = (from r in table.AsEnumerable()
group r by r.Field<string>("TemplateName") into templates
let totalDays = templates.Sum(r => r.Field<int>("AvgDays"))
from t in templates
group t by new
{
TemplateName = templates.Key,
TaskName = t.Field<string>("TaskName"),
TotalDays = totalDays
} into tasks
select new
{
tasks.Key.TemplateName,
tasks.Key.TaskName,
AvgDays = tasks.Sum(r => r.Field<int>("AvgDays")),
tasks.Key.TotalDays
}).CopyToDataTable();
I am getting error after execution of query. Error is "Specified cast is not valid.".
Please let me know where am I doing wrong.
I guess Field<int> causes the problem, because the field is not really an int. As you said before, it's a string.
Try following
AvgDays = tasks.Sum(r => int.Parse(r.Field<string>("AvgDays"))),
Field<T> does not perform some magic transformation between different types. It's implemented like that:
return (T)((object)value);
In practice there is a little bit more code, but logic is the same. It just tried to cast your value to desired type, and as you probably know, casting string to int does not work.

Linq 2 SQL Sum using lambda and handling nulls

When using sum with lambda in Linq to SQL using the following code:
int query = (from f in odc.RDetails
where f.ticketID == int.Parse(ticket.ToString())
select f).Sum(x => x.Rate);
I get the following error:
The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type.
. You have to make sure x.Rate is an int, and not an int? (an int that accepts null as a value).
. If the query has no elements, .Sum won't do anything and will return null. Choose a default value, let's say 0.
var query = from f in odc.RDetails
where f.ticketID == int.Parse(ticket.ToString())
select f;
int result = query.Any()
? query.Sum(x => x.Rate ?? 0) // use the ?? if x.Rate is an "int?".
: 0; // default value you can choose.
I would break the int.Parse(ticket.ToString()) onto its own line to isolate that parse from the Linq for debugging.
We don't know whether that is throwing the exception or if one of the RDetails.Rate values is null. Is it indeed a Nullable<int>?
If RDetails.Rate is a Nullable<int>, then you could ...Sum(x => x.Rate ?? 0) and avoid the exception.

Resources