• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

DB File Insert Values

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys I just started with the assignment. I have been assigned the urlybird assignment. I was thinking in order to start off, i will start off with fetching and inserting data in the .db file. If you guys think there is some better way to start, please let me know. But i thought, once I know how I am supposed to insert values, I can then design the rest of the application.

To just give inserting a try, I tried using the program posted on this forum. I wanted to know if i was on the right track. The given description file says that the header will give us the length of each field.
So i was thinking, in order to insert a string value for each record in the database, I will format the string using String.format("%"+lengthoffield+"s",value_to_insert); Then i will convert this into a byte array using String.getBytes().

I wanted to know if i was on the right approach or was there an easier way to do so.... morever, if i try to do so using regular strings, i get a missing character in the last field of the last record. Wanted to know if I was the only person with such problems.
 
Ranch Hand
Posts: 221
Scala Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gaurav,


Check the SCJD Faq and look for Roberto Perillo's Database File Reader program.


HTH,


Carlos.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would suggest writing your own little program to read the database file based on the data file Format (see your instructions). Or you could use Roberto's database file reader (which you'll can find in the ScjdFaq). This will give you a clear understanding of the base of your application, the database file. And then you can start from there.
And as a final remark: you don't have to insert any values into your database file, because it already contains everything you'll need. Don't forget to save an original copy of your database file, because you have to submit an original, unchanged version of the database file
 
Gaurav Raje
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:I would suggest writing your own little program to read the database file based on the data file Format (see your instructions). Or you could use
And as a final remark: you don't have to insert any values into your database file, because it already contains everything you'll need. Don't forget to save an original copy of your database file, because you have to submit an original, unchanged version of the database file



Wouldnt the following method from the interface write a record to the database? assuming there are no deleted entries

// Creates a new record in the database (possibly reusing a
// deleted entry). Inserts the given data, and returns the record
// number of the new record.
public int create(String[] data) throws DuplicateKeyException;
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, of course! But I'm convinced you'll 1st have to get familiar with your database file and its structure, before you can add records to the database file through the create-method. So I created my own database file reader (similar to Roberto's), because his tool was not available at that moment. And then I started with implementing the read-method, it's the easiest one Then I did the delete-method, followed by the update-method and finally the create-method.

And I tested each method implementation with running JUnit tests for each possible situation (success, failure, wrong parameters,...)
 
Gaurav Raje
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i had a small doubt. In urlybird While reading the file, the first part of the db file contains metadata about the size of each field, number of fields, magic cookie etc. Then the actual records start. The first part remains constant for this db file. So if i had to add a record, or delete, i dont actually need to parse through the metadata.
I was wondering, would it be a good design if i had a constant number of bytes which i can jump over during fetch/read/add operations? The disadvantage would be , in the real world, if someone changed the number of fields, my logic would collapse(since the size of the metadata would increase). But do i have to take that into consideration against performance? Or do i assume the db file has a constant structure no matter what. Another idea i thought was i could calculate the size of the metadata everytime i create an object of data.java, the data access component. I wanted a feed back as to what others might have thought or done in my place
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This decision is just yours: any option you choose will be fine as long as you argument this decision in your choices.txt file.

I read the database schema dynamically and kept a number, which indicates the start of the record section. So it's not a real constant and could change if fields are added, removed, size is extended,... But I know for example Roberto Perillo used all constants to navigate through the records and the database file.

It's up to you

Good luck!
Kind regards,
Roel
 
Gaurav Raje
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
one more design question. I wanted to know your thoughts. The interface for the db requires me to write two methods.
1) Read(int rec no) to read the record which returns the record as a string array
2) search (string[[ criteria) which will search based on the criteria and which effectively scans the enttire file.

As far as read is concerned, i start from the beginning of the record start and read all records untill i reach the record number which i am supposed to return and return the values.

My qustion was, in order to reduce code redundancy, is it a good idea to have the search method call the Read method? Because in searhc, i am effectively reading every record. However, since i reset the starter, the complexity will increase exponentially(O!) to be precise.

I am thinking of refactoring the code to achieve both, but in case i have to choose, i wanted to know if either seems to be a decent option for you guys
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gaurav,

I think your read-method should benefit from your random access database file. So based on the record number you can calculate on which position that record starts and you know from the database structure how big a record is. So a record can be read using:


Don't forget these 2 actions should be performed in an atomic operation.

Kind regards,
Roel
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic