• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Need help reading ResultSet from mySql into Observable list

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a relatively simple program where I'm trying to display values from a 2 column mySql table into a TableView in Java. My program is getting choked up with an error that says "java: incompatible types: java.lang.String cannot be converted to int."

My setup is as follows:

I've created a separate Java class that creates an object with 2 variables (brandId and brandName) called "Brand"



I have another Java class called "DatabaseHandler" that has all the code that interacts with the mySql database. In that code I have the following method that is supposed to connect to mySql database table called "dim_brand," retrieve all values, and populate them in an ObservableList  that I will use to populate the TableView.



In testing and debugging the code, I've gotten the connection to the database to succeed and I've be able out sout the results. My program is choking on the statement:



I have no idea why since I'm able to use the getString() method in the sout statement without any issue. I'm thinking it has something to do with the ObservableList<Brand> that I'm trying to insert the values into. Any help our insight would be greatly appreciated.

View of mySql table dim_brand:

brand_id: VARCHAR10
brand_name: VARCHAR45

brand_idbrand_name
JONMURPHYJohnston & Murphy
JOSABANKJos A Bank
NIKENike
TOMJAMESTom James
 
Saloon Keeper
Posts: 7645
178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error message about the "int" is strange, because what you have is a String, but you need a "Brand" - because that is what the ObservableList expects.

I think what you're looking for is something like this:

 
Chris Whitcomb
Greenhorn
Posts: 21
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you! Your suggestion worked. Now that you point it out it makes total sense to me. Thank you for helping me on my journey of learning!
 
Sheriff
Posts: 28323
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:The error message about the "int" is strange...



The compiler thinks that List.add(a, b) means to insert the element b at index a. So it expects an int for the first parameter.
 
Bartender
Posts: 303
12
IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Everyone else already hogged all the helpful replies (y'all are fast) so all I have left to add is this...

Your IDE's autocomplete functionality also helps you with this kind of stuff (usually CTRL+Space triggers it). And not that it's bad to ask here, but to answer questions like this yourself, being able to navigate and understand JavaDoc is helpful:

https://docs.oracle.com/javase/8/javafx/api/javafx/collections/ObservableList.html

You'll find the two add() methods there (they're actually inherited from another class).

But most of us are lazy, so IDE autocompletion is easier. Here's a capture from mine:



Originally you were calling the 2nd one I have highlighted, which is meant to add an element at a certain location in the list. You switched to the first version of add() which just adds the item to the end of the list.

And if you're wondering why mine says add(String e), it's just because I declared my ObservableList with a String as the generic type (ObservableList<String>). If it was Brand it'd say add(Brand brandId), so it's telling you exactly what it expects you to provide.
 
F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic