How do I save an Integer type (not int) into Database?
Using plain JDBC, I'd use the following:
Integer myInteger = ...;
PreparedStatement ps = ...;
if (myInteger == null) {
ps.setNull(1, Types.INTEGER);
} else {
ps.setInt(1, myInteger); // will be autounboxed
}
I think just use Integer is OK.Or use intValue() to convert it.
http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#setInt(int, int)
Depending on your database system (mysql, postgre, ...), the integer type will or won't exist in your database. It is then better to use java Integer functions to make an Integer from your database value, which will probably be int or even bigint, depending on what's needed.
As I said in my comment, something like Integer myinteger = new Integer(yourdatabasevalue) should work fine.
Related
I have the following SQL Statement and I used .getSingleResult() to get a Object from the EntityManager:
SELECT sum(pos_netto) as revenue from Auftraege_Positionen_BI_Test where art_nr = :artNr
I stored it in the Object returnObject. Now my Question is how can I get the value I selected via the SQL Statement out of the Object?
Given your case
It seems like you want to take out the sum which most probably is a double value
You can simply use
double returningSum=(Double) query.getSingleResult();
Or to check if it is null and taking appropriate actions like assigning 0
Object result=(Object) query.getSingleResult();
double returningSum=result==null?0.0: (Double) result;
Based on this link , sum returns Long or Double or BigInteger or BigDecimal etc value, so I think you don't need to do any conversion when using query.getSingleResult().
Just check what is type of pos_netto field in your class & use that type directly as a return type of query.getSingleResult().
I am running a simple insert query on a MySQL table with the following primary key declaration :
id BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY
They key returned is a BigInteger for some reason. What changes do I need to make for a Long value to be returned as the key?
EDIT
I'm making a mini-ORM basically and the Long type is not known at compile time since it is a class parameter (AbstractDao<T, I>). Essentially I need to be able to convert a Number to a Long without using anything that has long in it (Long.valueOf(), .longValue(), etc).
I do however have a Class instance that contains Long.class :
protected Class<?> idClass = ((Class<?>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[1]);
Can that instance somehow be used to perform the casting? You cannot just cast a BigInteger to a Long, nor can you cast a String representation of BigInteger to a Long sadly. If there was a way to access valueOf() from my class object this might work?
executeAndReturnKey(...) returns a Number, so call longValue() if you want the result as a long.
If your method doesn't know the exact numeric type needed by the caller, let the caller do the longValue(). Alternatively, write multiple methods, for the various return types you want to support, e.g.
public Number insertAndGetKey(...) {
return ...executeAndReturnKey(...);
}
public int insertAndGetIntKey(...) {
return insertAndGetKey(...).intValue();
}
public long insertAndGetLongKey(...) {
return insertAndGetKey(...).longValue();
}
This, although extremely ugly, works like a charm :
var method = idClass.getMethod("valueOf", String.class);
var result = method.invoke(null, key.toString());
This returns something I can then typecast to (I) without issues. Please do let me know if you have a better solution to all this. The original question being about the BigInteger return type, this can probably be avoided.
I have this int column:
#Column(length = 4)
private int contract_owner_id;
I don't need to set always value for every table row. I get this exception when I make select query:
Can not set int field org.entity.contracts.contract_owner_id to null value
Is there some way to set numb setups when there is no data into the table column?
The primitive datatype int isn't nullable. You need to use the Wrapper class Integer in this case. So just replace the type.
use Integer instead of int
ex-:
private Integer contract_owner_id;
Could be because you are using primitive datatype, try to use Wrapper instead (Long, Integer, Double)
I want to the set null values on runtime depending on the type I get
Here is my code
#SuppressWarnings("rawtypes")
// input can be Intger,Date, BigInt etc which Mysql supports
String input = "."+st.getType();
// param :- java.sql.Types.Integer or java.sql.Types.BigInt
Class clazz = Class.forName("java.sql.Types");
Field field = clazz.getField(input);
int val = field.getInt(null);
pstmt.setNull(1,val);
I'm going to slightly challenge your question in this answer. I do not believe that you need to do what you are doing, but as it's somewhat unclear, I'll do what I can to help you out.
First, based on the variable name pstmt I'm going to guess that this is a PreparedStatement that looks something like this:
INSERT INTO my_table (my_int, my_date, my_varchar, my_bool) VALUES (?, ?, ?, ?);
If this is the case, then you don't need to do any reflection at all, because Parameter 1 will always be an INTEGER, Parameter 2 will always be a DATE and so on. So you know what type to use in setNull because you know the position already (because you have to, because it's the first argument to setNull), and you know the statement. It's generally going to be much easier to follow your code if you just do the obvious thing and set the values.
if (myInt != null) {
pstmt.setInt(1, myInt);
} else {
pstmt.setNull(1, Types.INTEGER);
}
pstmt.setDate(2, myDate); // already handles null
// and so on
Now, this might get a tad trickier if you're trying to set the columns in a loop for some reason. I suggest to you that this is a bad idea because it'll be less obvious to future developers what you're up to.
However, the JDBC API has setObject for just such an occasion. setObject will allow null values, so you don't have to handle the null and not-null cases separately. There's even a version of setObject that will allow you to do this without specifying the type. In that case, it's just:
for (int i = 0; i < myObjects.size(); ++i) {
Object obj = myObjects.get(i);
pstmt.setObject(i, obj);
}
No casting, no remembering the type, no reflection needed. The documentation says it's not supported by every database, but I'd give this a shot before you do reflection.
Now to Answer Your Question
If, for some reason, you need to do this with reflection (because you're one of the unlucky few whose database doesn't support un-typed nulls), you were actually pretty close.
public void apply(PreparedStatement pstmt, int param, String typeName, Object value) {
try {
Class<?> types = Types.class;
Field field = types.getField(typeName);
int typeValue = field.getInt(null);
pstmt.setObject(param, obj, typeValue);
} catch (Exception e) {
// handle exceptions
}
}
So what's different here from what you were trying? First, the field name doesn't have a . in front of it. That's not part of the field name. Secondly, the typeName parameter here must exactly match the java.sql.Types field name. This is a case-sensitive String. So, for example it's "INTEGER" not "int". In your comment, you indicated that your st.getType() method had an ArrayList. I've assumed that this method returns a single string, based on its knowledge of the query. Just make sure that your type names are the same as the names used by java.sql.Types and that the object in question can actually be used as (cast / converted to) that type.
I'm using jOOQ to get id which in MySQL is smallint unsigned primary key auto_increment
public List<Integer> getID() {
Factory sql = new Factory(Database.getInstance().connect(), SQLDialect.MYSQL);
return (List<Integer>) sql.select().from("users").fetch().getValues("id_users");
}
And go error
org.jooq.tools.unsigned.UShort cannot be cast to java.lang.Integer
Here they wrote that smallint unsigned should be cast to int.
Edit
Method should be
public List<UShort> getID() {
Factory sql = new Factory(Database.getInstance().connect(), SQLDialect.MYSQL);
return (List<UShort>) sql.select().from("users").fetch().getValues("id_users");
}
And in loop result should be cast to int.
You cannot cast UShort into Integer as it does not inherit that class. I guess you should use UShort.intValue() to retrieve the Integer.
The java lang cannot cast that directly. You need am intermediate step.
Something like UShortval.intValue()
Iterate the result of the query, and build up a new List where you add the result of ushortval.intValue()
If the users is a system table, thus you cannot change the type then you have to convert it to Integer. Otherwise you could change a table and provide TINYINT(4) for type Short Java.
jOOQ can do the type casting and/or conversion for you. Here are a couple of examples:
Result<?> result = sql.select().from("users").fetch();
// Without any type information
List<?> list1 = result.getValues("id_users");
// Using your implicit knowledge about id_users being an unsigned short:
#SuppressWarnings("unchecked")
List<UShort> list2 = (List<UShort>) result.getValues("id_users");
// Using jOOQ's code-generator for type-safe field access:
List<UShort> list3 = result.getValues(USERS.ID_USERS);
// Using jOOQ's conversion features to convert UShort into whatever you prefer
List<Integer> list4a = result.getValues("id_users", Integer.class);
List<Integer> list4b = result.getValues(USERS.ID_USERS, Integer.class);
The last example being what you were probably looking for.
See the relevant Javadocs here:
org.jooq.Result.getValues(Field, Class)
org.jooq.tools.Convert.convert(Object, Class)
Note that the link to the Sybase ASE manual is refering to a SQL cast operation, not a Java cast expression. That might have been what was misleading to you...