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.