Solved

write to database in java

Posted on 2012-03-20
12
408 Views
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 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
on
1 error
sample.java
0
Comment
Question by:onyourmark
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
12 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37745383
change
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)
0
 
LVL 47

Expert Comment

by:for_yan
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
0
 

Author Comment

by:onyourmark
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?
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 47

Expert Comment

by:for_yan
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
use
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
0
 
LVL 47

Expert Comment

by:for_yan
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
0
 

Author Comment

by:onyourmark
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.
0
 
LVL 47

Expert Comment

by:for_yan
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
0
 
LVL 47

Expert Comment

by:for_yan
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
0
 

Author Comment

by:onyourmark
ID: 37745515
Ok. I think it is here
http://weka.sourceforge.net/doc/weka/core/Instance.html

" 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
dataTest.instance(i).value(0)
to get the value here?

Like
dataTest.instance(i).stringValue()

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));
0
 
LVL 47

Expert Comment

by:for_yan
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

therefore

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
0
 
LVL 47

Accepted Solution

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

dataTest.instance(i).stringValue()

only if the type tow which the product of this method

dataTest.instance(i).

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

dataTest.instance(i).

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
0
 

Author Closing Comment

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

Thanks again.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

691 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