This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Kubernetes in Action and have Marko Luksa on-line!
See this thread for details.
Win a copy of Kubernetes in Action this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how Can I fix this problem  RSS feed

 
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
 
author & internet detective
Marshal
Posts: 37714
579
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.
 
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
 
Author and ninkuma
Marshal
Posts: 66352
155
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the JDBC forum.
 
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);
 
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
Boost this thread!