write to database in java

Posted on 2012-03-20
Last Modified: 2012-06-27
Hi. I am using the attached code. I am getting errors (below). The output should be something like this
ID: 0.0, actual: no, predicted: no
ID: 0.0, actual: no, predicted: no
ID: 1.0, actual: yes, predicted: yes
ID: 2.0, actual: yes, predicted: yes
ID: 2.0, actual: yes, predicted: yes

So I think the output should all be text (strings).

But I am getting the following errors:

C:\Users\Bill\Desktop\test>javac error: method setString in interface PreparedS
tatement cannot be applied to given types;
      stmt.setString(1, dataTest.instance(i).value(0));
  required: int,String
  found: int,double
  reason: actual argument double cannot be converted to String by method invocation conversi
1 error
Question by:onyourmark
  • 8
  • 4
LVL 47

Expert Comment

ID: 37745383
stmt.setString(1, dataTest.instance(i).value(0));

 to this:

stmt.setString(1, "" + dataTest.instance(i).value(0));

(add "" + to your double - it will cnvert t to String)
LVL 47

Expert Comment

ID: 37745397
That's what I was talking about before - your method setString(,,)
expects struing as the second argunemnt

and your dataTest.instance(i).value(0) returns double
so when you do this

 "" + dataTest.instance(i).value(0)

it becomes string

Author Comment

ID: 37745455
Hi. Thanks very much. I don't understand why
dataTest.instance(i).value(0)   returns  a  double. Isn't this referring to  ID: 0.0, or ID: 0.0 etc? Aren't these strings? Where is the double here?
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

LVL 47

Expert Comment

ID: 37745456
>The output should be something like this

>So I think the output should all be text (strings).

when you insert into database table - it has nothing to do with the output

if you want to insert into the table - the table shoudl already have been created - it should exist

and its columns should be defibned - those columns may be either numbers or text or date

which setXXX method you'll use with preapredStatement for eacgh column has nothing to do with any out put. It has everything to do wityh how your corresponding table field was set up at the creation of the table
so if your fisrt column at the moment of creation of the table was creted as text(string) column then you should use for thias column
method setString(1,String);
if we assume that the second column, say is number then yous should
setInt(2, int) or setDouble(2, double)

Once again - outpiut is quite different matter
Ouptput will happend at another time when you slect form your table - and waht you stored in the column of type number after you select from the table you can print as string
LVL 47

Expert Comment

ID: 37745480
the printout which you see  will alwys be string, it does not mean that the variables which you print were all of the type string

if I say in some code::
double d = 5.0;

System.out.println("d is equal to " + d);

Open in new window

then you'll see in the output:

d is equal to 5.0

what will happen is that method println() would convert double value d
into the string "5.0" and then will concatenate this string with the beginning sentence and the whol
will become a string whicvh will be printed

the fact that when in your program it says:

System.out.println("ID: " + dataTest.instance(i).value(0));

you see it all as a string does not mean that

 dataTest.instance(i).value(0) is of the type String

To know what its real type you need to know
what type is  dataTest , what type is the result of invoking instance() metyhod
on entity of type dataTest, and then what type is produced by method value() on the result of method instance()

To know that you need to instapce the code for the class to which blongs variable
dataTest and go deep into code and uunderstand it
You cannot judge about it based on wht you see on the priontout - especially it prints
not the text, but the numeric string , say 1.0 or 2.0

Compiler chacked that type of  dataTest.instance(i).value(0) and relaized that it is not the string which it exepcetd for this method - so it compalined

compiler ususally does not make mistakes - it indeed shlould be double and youll understadn that if you go through all that code I mentioned above

Author Comment

ID: 37745484
Thanks again. OK, I understand that the table should be created. It has been created actually. It is called weatherSample. I select from it twice (lines 55 and 72). All of the fields in the table called weatherSample are varchar. I do now also understand that it is not the output that counts. I guess the output could be coerced into some other type. But still I think that all the fields should be strings. Sorry I don't get it yet.
LVL 47

Expert Comment

ID: 37745497
>But still I think that all the fields should be strings.

The field in the table may all be strings and that;s what we do when we say

"" + dataTest.instance(i).value(0)

before we actually feed it to pst.setString(...) method

It does not mena that in the program, in java code where you produce this value
dataTest.instance(i).value(0) it cannot be produced as double. It may be the result of some calculation, summsation or whatever and it as a result it comes out as anumber
which we eventually turn to string when we conatenate it wit empty string ""

As we do not have time to stuudy the code of all those classes which produce thsi
value we cannot say for sure, but as compiler writes this error, so we may be sure that compiler did do the checking
and indeed this entity is of type double
LVL 47

Expert Comment

ID: 37745505
It is the same as in this code:

doubel d1 = 3.0;
double d2 = 5.0;

System.out.println("mult is equal to " + (d1*d2));

Open in new window

here d1 is double
d2 is double

d1*d2 is double

and only when java conactenates string "mult is equal to " with double (d1*d2) (equlas 15 here) only then it will convert double to the string and then concatenate - internally converting double to string is quite an operation with many many CPU steps and calculations, but lucking Java does it for us

The same happend with your enity which we discvussed

Author Comment

ID: 37745515
Ok. I think it is here

" double      value(int attIndex)
          Returns an instance's attribute value in internal format."

I guess as you say, it is because of the calculation of   .value()  that it becomes a double.

But that leads me to say, shouldn't I use some other code instead of
to get the value here?



Would this work?
Then I could write:
stmt.setString(1, dataTest.instance(i).stringValue(0));
LVL 47

Expert Comment

ID: 37745518
method setString(...) of preapredStatement as opposed to concatenation which I metioned above strictly requires only String as the second parameter; it does not do automatic conversion; it just complains when insted of string you feed some other type to it


setString(1, (d1*d2))

(using simple example above)

will complain, but if we do this

setString(1, "" + (d1*d2))

then conactentaion would help us and its product will be string so method setString will be happy
LVL 47

Accepted Solution

for_yan earned 500 total points
ID: 37745525
You can use this:


only if the type tow which the product of this method


belongs , defines the method stringValue(0)

Those are not just any words - they all correspond to the defginitions of the methods of corresponsing classes.

if class to which


belongs does not defien method stringValue(int) then you would not be able to use it

even if iot does - it probably will do something similar as we do when concatente empty string insdie that methdo, so it does not matter what you will use

It is most probable that this value(0) is actually the result of some calculation, that;s why it reprtesents double not string

Author Closing Comment

ID: 37745566
Excellent help.
It worked!! !! !! !! !! !!!!! !! !! !! !!

Thanks again.

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword This article was written many years ago, in the days when PHP supported the MySQL extension (  Today ( you would not use MySQL…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL ( several years ago, it seemed like now was a good time to updat…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question