Solved

write to database in java

Posted on 2012-03-20
12
403 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
  • 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
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 …

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now