0

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

3
  • 1
    Whats the issue? Autoboxing happens in Java so what different does it make? Commented Jun 12, 2012 at 7:28
  • 2
    @subirkumarsao Maybe the issue is with the null value? Commented Jun 12, 2012 at 7:30
  • Im new to database. Im using 'Integer' and 'Number' type. If I rmmber correctly, I'd have to specify the table column what is the type of data that it will hold, am I right? Commented Jun 12, 2012 at 7:39

4 Answers 4

3

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
 }
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#setInt(int, int)

Comments

-2

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.

2 Comments

Im using sqlite, as embedded database.
I don't know sqlite very well, but from what I can see of the docs, it only has the integer type. Then you can store it as Integer in sqlite and make an Integer in java like Integer myinteger = new Integer(here you retrieve your value) (don't really do the request in there though ^^ this is just an example)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.