Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how Can I fix this problem

 
Sohail Mahmood
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sohail,
Welcome to Javaranch! You want to use rs.getDouble() instead. With getInt(), it is truncating your output to fit it into an integer.
 
michael yue
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the JDBC forum.
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ResultSet.getDouble() methods return a double not a Double.
What kind of error are you getting when you do the rs.getDouble("column1")?
 
michael yue
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic