SQL object to int

A

Ann

I have a MySQL database; two of the columns are int(5).
I wrote a method to extract a tuple and put it into an
Object[][] myData; using getObject()
How can I get the integer values?
Using trial and error, the following works, but it looks awful.

int x = Integer.parseInt(myData[0][3].toString());
 
T

Tony Morris

A

Ann

Tony Morris said:
Ann said:
I have a MySQL database; two of the columns are int(5).
I wrote a method to extract a tuple and put it into an
Object[][] myData; using getObject()
How can I get the integer values?
Using trial and error, the following works, but it looks awful.

int x = Integer.parseInt(myData[0][3].toString());
http://java.sun.com/j2se/1.5.0/docs/api/java/sql/ResultSet.html#getInt(java.
lang.String)
http://java.sun.com/docs/books/tutorial/jdbc/index.html
Ok, I looked at that and it is easy to use getInt(col), but
I wanted my method to return a single two dimentional array.
Maybe I need to figure out how to have an array of differing
typed arrays, each of which is the same length.
 
T

Tony Morris

Ann said:
Tony Morris said:
Ann said:
I have a MySQL database; two of the columns are int(5).
I wrote a method to extract a tuple and put it into an
Object[][] myData; using getObject()
How can I get the integer values?
Using trial and error, the following works, but it looks awful.

int x = Integer.parseInt(myData[0][3].toString());
http://java.sun.com/j2se/1.5.0/docs/api/java/sql/ResultSet.html#getInt(java.
Ok, I looked at that and it is easy to use getInt(col), but
I wanted my method to return a single two dimentional array.
Maybe I need to figure out how to have an array of differing
typed arrays, each of which is the same length.

Sounds like one big bowl of spaghetti to me.
Try this, but it requires an intimate knowledge of JDBC, Java, and database
design - it will sort out your spaghetti though.
http://www.hibernate.org/
 
L

Lee Fesperman

Ann said:
I have a MySQL database; two of the columns are int(5).
I wrote a method to extract a tuple and put it into an
Object[][] myData; using getObject()
How can I get the integer values?
Using trial and error, the following works, but it looks awful.

int x = Integer.parseInt(myData[0][3].toString());

int(5) is not standard, but if the JDBC driver follows the spec, getObject() should
return an Integer object for those columns. In that case, you can do:

int x = ((Integer) myData[0][3]).intValue();

.... which looks a little better ;^)

Be sure and check your driver though; I think some MySQL JDBC drivers don't provide that
(proper) functionality.
 
A

Ann

Lee Fesperman said:
Ann said:
I have a MySQL database; two of the columns are int(5).
I wrote a method to extract a tuple and put it into an
Object[][] myData; using getObject()
How can I get the integer values?
Using trial and error, the following works, but it looks awful.

int x = Integer.parseInt(myData[0][3].toString());

int(5) is not standard, but if the JDBC driver follows the spec, getObject() should
return an Integer object for those columns. In that case, you can do:

int x = ((Integer) myData[0][3]).intValue();

... which looks a little better ;^)

Be sure and check your driver though; I think some MySQL JDBC drivers don't provide that
(proper) functionality.

--
Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)

This is what I was looking for, thanks. I use MySQL and specified
x int(5) using the mysql console. The 5 means that mysql will print
output will be 5 characters or less. With JDBC, a java Object that contains
a java Integer is returned, and the (5) is totally ignored as it
should be.
 
T

Tony Morris

Lee Fesperman said:
Ann said:
I have a MySQL database; two of the columns are int(5).
I wrote a method to extract a tuple and put it into an
Object[][] myData; using getObject()
How can I get the integer values?
Using trial and error, the following works, but it looks awful.

int x = Integer.parseInt(myData[0][3].toString());

int(5) is not standard, but if the JDBC driver follows the spec, getObject() should
return an Integer object for those columns.

This is blatantly incorrect.
JDBC 3.0 Specification TABLE B-6.
In that case, you can do:

int x = ((Integer) myData[0][3]).intValue();

... which looks a little better ;^)

Since the suggestion is on a false premise, it is wrong (the downcast may
not succeed), but suppose it wasn't, the suggestion is nasty anyway.
Avoid downcasts at all costs - abstraction aids future maintenance. It might
"look a little better" to you (I prefer blondes to brunettes), but it's
terrible form.
 
A

Ann

Tony Morris said:
Lee Fesperman said:
Ann said:
I have a MySQL database; two of the columns are int(5).
I wrote a method to extract a tuple and put it into an
Object[][] myData; using getObject()
How can I get the integer values?
Using trial and error, the following works, but it looks awful.

int x = Integer.parseInt(myData[0][3].toString());

int(5) is not standard, but if the JDBC driver follows the spec, getObject() should
return an Integer object for those columns.

This is blatantly incorrect.
JDBC 3.0 Specification TABLE B-6.
In that case, you can do:

int x = ((Integer) myData[0][3]).intValue();

... which looks a little better ;^)

Since the suggestion is on a false premise, it is wrong (the downcast may
not succeed), but suppose it wasn't, the suggestion is nasty anyway.
Avoid downcasts at all costs - abstraction aids future maintenance. It might
"look a little better" to you (I prefer blondes to brunettes), but it's
terrible form.

Now I am confused. Maybe I am the
beneficiary of dumb luck. Here is more detail.
My database table is like this:

mysql> describe tbl000;
+-------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+----------------+
| i | int(5) | | PRI | NULL | auto_increment |
| f | double(14,3) | YES | | NULL | |
| t | timestamp(14) | YES | | NULL | |
| x | int(5) | | | 0 | |
| y | int(5) | | | 0 | |
+-------+---------------+------+-----+---------+----------------+
5 rows in set (0.03 sec)

mysql> select * from tbl000;
+---+---------+----------------+-----+-----+
| i | f | t | x | y |
+---+---------+----------------+-----+-----+
| 1 | 104.000 | 20041218211840 | 98 | 65 |
| 5 | 104.000 | 20041218211844 | 301 | 105 |
| 6 | 104.000 | 20041218211844 | 415 | 263 |
+---+---------+----------------+-----+-----+
3 rows in set (0.00 sec)

In java I do this:
"SELECT * FROM tbl000" using a "execute" statement
and get a result set. Then I put the data in the result set
into an array of Objects;

Object[][] data;
int k = 0;
while (rec.next()) {
for (int i = 0; i < numCols; i++) {
data[k] = rec.getObject(i + 1);
}
k++;
--------------
From the source file C:\j2sdk1.4.2\src\java\sql\ResultSet.java:
/**
* <p>Gets the value of the designated column in the current row
* of this <code>ResultSet</code> object as
* an <code>Object</code> in the Java programming language.
*
* <p>This method will return the value of the given column as a
* Java object. The type of the Java object will be the default
* Java object type corresponding to the column's SQL type,
* following the mapping for built-in types specified in the JDBC
* specification. If the value is an SQL <code>NULL</code>,
* the driver returns a Java <code>null</code>.
<snip>
* @param columnIndex the first column is 1, the second is 2, ...
* @return a <code>java.lang.Object</code> holding the column value
* @exception SQLException if a database access error occurs
*/
Object getObject(int columnIndex) throws SQLException
--------------

Doesn't this mean in effect that, for the x and y columns,
it stuffs an Integer into the Object?
If it didn't do it this way, I would expect that intValue()
would barf, but
int x = ((Integer) myData[0][3]).intValue();
works fine.
 
D

Dave Glasser

Ann said:
I have a MySQL database; two of the columns are int(5).
I wrote a method to extract a tuple and put it into an
Object[][] myData; using getObject()
How can I get the integer values?
Using trial and error, the following works, but it looks awful.

int x = Integer.parseInt(myData[0][3].toString());

int(5) is not standard, but if the JDBC driver follows the spec, getObject() should
return an Integer object for those columns. In that case, you can do:

int x = ((Integer) myData[0][3]).intValue();

... which looks a little better ;^)

But this would be safer (the cast, that is) and give the same result:

int x = ((Number) myData[0][3]).intValue();

--
Check out QueryForm, a free, open source, Java/Swing-based
front end for relational databases.

http://qform.sourceforge.net

If you're a musician, check out RPitch Relative Pitch
Ear Training Software.

http://rpitch.sourceforge.net
 
L

Lee Fesperman

Tony said:
Lee Fesperman said:
Ann said:
I have a MySQL database; two of the columns are int(5).
I wrote a method to extract a tuple and put it into an
Object[][] myData; using getObject()
How can I get the integer values?
Using trial and error, the following works, but it looks awful.

int x = Integer.parseInt(myData[0][3].toString());

int(5) is not standard, but if the JDBC driver follows the spec,
getObject() should return an Integer object for those columns.

This is blatantly incorrect.
JDBC 3.0 Specification TABLE B-6.

You're looking at the wrong table. JDBC 3.0 TABLE B-3 describes the Java Object Types
returned by getObject() for various JDBC Types. It shows java.lang.Integer for
Types.TINYINT/Types.SMALLINT/Types.INTEGER.
In that case, you can do:

int x = ((Integer) myData[0][3]).intValue();

... which looks a little better ;^)

Since the suggestion is on a false premise, it is wrong (the downcast may
not succeed), but suppose it wasn't, the suggestion is nasty anyway.
Avoid downcasts at all costs - abstraction aids future maintenance. It might
"look a little better" to you (I prefer blondes to brunettes), but it's
terrible form.

A downcast of the result of getObject() is quite reasonable since the spec (see above)
guarantees the actual type. The only reason it won't work is if the driver violates the
spec. Hardly a 'nasty' suggestion.
 
L

Lee Fesperman

Dave said:
Ann said:
I have a MySQL database; two of the columns are int(5).
I wrote a method to extract a tuple and put it into an
Object[][] myData; using getObject()
How can I get the integer values?
Using trial and error, the following works, but it looks awful.

int x = Integer.parseInt(myData[0][3].toString());

int(5) is not standard, but if the JDBC driver follows the spec, getObject() should
return an Integer object for those columns. In that case, you can do:

int x = ((Integer) myData[0][3]).intValue();

... which looks a little better ;^)

But this would be safer (the cast, that is) and give the same result:

int x = ((Number) myData[0][3]).intValue();

Good point! It would be safer given the circumstances ... possibly non-conformant
drivers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top