Serge
Serge

Reputation: 626

How does resultSet store values

I'm running a query that loads into a ResultSet. When I'm creating my object from the ResultSet I use various get methods of the ResultSet. Does the ResultSet store all the values as Strings no matter what they're defined as in the DB? So if the result of a query is the number 1, can I call

rs.getString("result");
AND
rs.getInt("result");

?

Or does the get method of the ResultSet have to correspond to a correct type?

Upvotes: 1

Views: 679

Answers (2)

Potatow
Potatow

Reputation: 76

What is important here is the type of the variable that will receive the returned value.

For example, you can without any problem use rs.getString() in a column that return int values since numbers are valid string values ("1234", "23,45", etc.).

But you cannnot use rs.getInt() in a column with string values because the returned value will be not valid for a int variable.

MySQL help has more information about this:

In general, any MySQL data type can be converted to a java.lang.String, and any numerical type can be converted to any of the Java numerical types, although round-off, overflow, or loss of precision may occur.

These MySQL Data Types Can always be converted to these Java types:

  • CHAR, VARCHAR, BLOB, TEXT, ENUM, and SET: java.lang.String, java.io.InputStream, java.io.Reader, java.sql.Blob, java.sql.Clob

  • FLOAT, REAL, DOUBLE PRECISION, NUMERIC, DECIMAL, TINYINT, SMALLINT, MEDIUMINT, INTEGER, BIGINT: java.lang.String, java.lang.Short, java.lang.Integer, java.lang.Long, java.lang.Double, java.math.BigDecimal

  • DATE, TIME, DATETIME, TIMESTAMP: java.lang.String, java.sql.Date, java.sql.Timestamp

More info Here

Upvotes: 2

Cody S
Cody S

Reputation: 4824

Yes and yes. You can get an INT from MySQL as a java.lang.String, and the types have mappings

https://dev.mysql.com/doc/refman/5.0/es/connector-j-reference-type-conversions.html

Hope this helps

Upvotes: 4

Related Questions