write to database in java

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 JavaWekaJ48TestTrainPredSQL_updated.java
JavaWekaJ48TestTrainPredSQL_updated.java:140: 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
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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)
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
onyourmarkAuthor Commented:
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?
Acronis Data Cloud 7.8 Enhances Cyber Protection

A closer look at five essential enhancements that benefit end-users and help MSPs take their cloud data protection business further.

>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
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
onyourmarkAuthor Commented:
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.
>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
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
onyourmarkAuthor Commented:
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?


see: http://weka.sourceforge.net/doc/weka/core/Instance.html

Would this work?
Then I could write:
stmt.setString(1, dataTest.instance(i).stringValue(0));
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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
onyourmarkAuthor Commented:
Excellent help.
It worked!! !! !! !! !! !!!!! !! !! !! !!

Thanks again.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.