Reputation: 119
How do I save an Integer
type (not int
) into Database?
Upvotes: 0
Views: 1551
Reputation: 1376
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.
Upvotes: -2
Reputation: 35018
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
}
Upvotes: 3
Reputation: 4604
http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#setInt(int, int)
Upvotes: 0