Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Acces table

 
Kim Mirtens
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm fairly new to java and I'm trying to make a simple program that accesses some data from a table. The table has about 12 columns and 100 rows and consists exclusively out of numbers. The rows are linked to each other and using data from one column I want to be able to access the data of an other column on the same row. I want to be able to write a get function which gives me a value back of a specific row based on an argument from a different column (on the same row).
How should I go about this? Should I put my table in a database? Is there something in java like struct in C and records in Pascal? Should I use 2D arrays? Should I make write a class which has all the columns as instance variables (making an object of each row)? Use an Arraylist or LinkedList? Basically what is the easiest option to do this?
[ December 29, 2006: Message edited by: Tim Pintens ]
 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://java.sun.com/docs/books/tutorial/jdbc/TOC.html
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34401
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the future, note we have a forum on JDBC.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12124
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JDBC sounds good to me, so off this thread goes!!!
 
Kim Mirtens
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't know if a database was the best way to go. The only thing my program has to do is USE the values from the table. This table doesn't change; it only holds constants. Setting up a DB-connection just to use values, seemed kinda hard and maybe unnecessary if there was an other way (which I guess there is not).
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34401
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim,
You are correct that a database isn't necessarily the best way to go. Since you mentioned it in your post, that was the way the advice went.

I would use a comma or tab delimited file with the data and read that in.
 
Kim Mirtens
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merely thought of DB as a possible solution, but was actually inquiring what was the best way to go.
If I just read the file, then how would I go abouut searching for values. Just use loops to scan all values (is there a function to search for values?)? Do I put the table in a 2D array or is it better to search and read on the fly.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34401
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim,
Assuming you plan to use this data more than once, it is best to read it in and store it in a data structure of your choosing.

That structure may be a 2D array or it may be a map of custom objects. If your data has some sort of key (like an id number), the map will be easier to use. If all the fields are equally meaningful, you could use the 2D array. Or you could use a 1D array of Objects that you create.

There's no right and wrong on this. It depends on your data and preferences.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tim Pintens:
I'm fairly new to java and I'm trying to make a simple program that accesses some data from a table. The table has about 12 columns and 100 rows and consists exclusively out of numbers. The rows are linked to each other and using data from one column I want to be able to access the data of an other column on the same row. I want to be able to write a get function which gives me a value back of a specific row based on an argument from a different column (on the same row).
How should I go about this? Should I put my table in a database? Is there something in java like struct in C and records in Pascal? Should I use 2D arrays? Should I make write a class which has all the columns as instance variables (making an object of each row)? Use an Arraylist or LinkedList? Basically what is the easiest option to do this?

[ December 29, 2006: Message edited by: Tim Pintens ]



We do not use 'structs' in Java. Instead you will make a class which will serve the same purpose in the beginning. So what you were thinking to do with struct, instead do with "class".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic