Fadhlie Ikram
Fadhlie Ikram

Reputation: 119

Saving Integer value into database

How do I save an Integer type (not int) into Database?

Upvotes: 0

Views: 1551

Answers (4)

Gabriel Theron
Gabriel Theron

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

beny23
beny23

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

plucury
plucury

Reputation: 1120

I think just use Integer is OK.Or use intValue() to convert it.

Upvotes: 0

Related Questions