This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes how Can I fix this problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "how Can I fix this problem" Watch "how Can I fix this problem" New topic
Author

how Can I fix this problem

Sohail Mahmood
Greenhorn

Joined: May 20, 2004
Posts: 1
I have database in MS-ACCESS and have following values in the "column1" of "table1".

column1
-------
0.1234
0.2345
1.1233

I write the following code in JSP
---------------------------------

String query1 = "SELECT column1 FROM table1";
ResultSet rs = stmt.executeQuery(query1);

<% while(rs.next()) {
out.println(rs.getInt("column1");
}
%>


it is showing me the following Values
-------------------------------------

0.0
0.0
1.0
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30050
    
149

Sohail,
Welcome to Javaranch! You want to use rs.getDouble() instead. With getInt(), it is truncating your output to fit it into an integer.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
michael yue
Ranch Hand

Joined: Nov 20, 2003
Posts: 204
I face a problem when using getDouble like

<% while(rs.next())
{out.println(rs.getDouble("column1");}%>

How do I assign rs.getDouble("column1") to a variable?
Using
double var = rs.getDouble("column1") and
Double var = rs.getDouble("column1")
gives error.
I know Double is a class so have to change from double to Double first. I am getting confused here.

Anyway initializing double also a problem like
double var = 0; -->gives error int found instead of double
How can I initialize double?

Now I have to be content with grabbing the data in String then convert it to double only then display it.
Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60732
    
  65

Moving to the JDBC forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
ResultSet.getDouble() methods return a double not a Double.
What kind of error are you getting when you do the rs.getDouble("column1")?


Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
michael yue
Ranch Hand

Joined: Nov 20, 2003
Posts: 204
I mean I am having difficulties trying to initialize a double value. I declare
double x = 0; returns error int found instead of double.

So how can I initialize a double before assigning it to getDouble ?
michael yue
Ranch Hand

Joined: Nov 20, 2003
Posts: 204
This is something that i forget to add.

This is what i am doing now to get a double value.

String amount = rst.getString("amount");
if(amount!=null)
amount = amount.trim();
else
amount = "0";

Double Damount = Double.valueOf(amount);
double damt = (Damount.doubleValue()) ;
amount = String.valueOf(damt);
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
Hi,

Why are you trying to get the double value in String? rs.getDouble() method return double not Double. What is the datatype of the column for which you are using getDouble() method?

You can initialize a double as
double d = 0.0d;
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how Can I fix this problem
 
Similar Threads
UNION for Columns of CLOB data type does not work
Escape Character Help
Select statement error
JRC - multiple tables in crystal report
cmp UNIQUE KEY CONSTRATINT