Create a client-server database without using JDBC-ODBC or other SQL connections. Be able to read and write to a single table, one record at a time. Can be application or applet.

Intermediate to Advanced Level: Create a client server – database without using JDBC-ODBC
-----------------------------------
i am trying to implement the server code and client code for a custom-database program. I have to implement my own server-client java program and implement it without using JDBC ODBC etc (or other SQL databases). I know how to do it using JDBC connection but I have no clue on how to work with my own file (creating both server side code and client side code), the table of the database should be stored in one file.

Requirements and Limitations:
---------------------------------
1) The database should be included in 1 file holding the data for a single table.
2) The file should have fixed-length records and should use only simple data types: number, char etc
3) implement a Java class accessing this file for writing and reading.
4) Functionality: displaying the contents of a table and modifying, deleting or adding specific records in the table
5) The data should be read or written in the data file using: an iterator pattern (the server java program should be able to work on a single record of the data file when it’s called by the client program – I should not read the whole file into memory. I think a good idea is to use java.io.RandomAccessFile to accomplish this.
6) the program can be written as an application or as an applet.
7) I have to handle exceptions as well, (including exceptions similar to ODBC exceptions regarding duplicate keys,  etc.
8) The client and server program does not need to implement SQL queries. Just displaying the whole table and be able to add, remove and modify a specific line-record.


I would really appreciate any help on this. Especially with the server side part I have no idea how to do it. Please help me as much as you can, either full code for both client and server or parts of code from client and server.

Thank you in advance!

thief77Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RuadRauFlessaCommented:
Full code is not an option so don't even ask.

You actually have quite a couple of options open to yourself. You can eather use a clear text file or a binary file. I would advise a binary file since you would be using fixed length records and such. However clear text files are easier to read or write to.

One quick question... is there a set table with coloumns and such or may they be changed at any time?
0
thief77Author Commented:
There is only 1 table and this table will have fixed columns. Lets say for example the table has  6 columns only , and the columns are fixed length each. Also lets say the key is gonna be the column with name  :  NO  and its gonna be a 6 digit number.

0
petmagdyCommented:
Hi thief77,
 why don't u handle this single file as XML? is think it will be must easier
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

thief77Author Commented:
hmm, i never worked before using XML. I want to use the easiest method possible. My experience using my own files for read/write is very small. I created before some applications using JDBC to connect to MySql in order to read a table, but other than that i have no idea how to use any other kind of file.
0
RuadRauFlessaCommented:
(I assume that you know the ins and outs of how to read and write from and to files)

I am going through the file first....
Say you are using a text file then you would seperate diffirent records by means of a newline character (normally). So you could convert everything you need to write to the file into a string value and pad it so that it represents the correct length. When reading it you would read a certain ammount of characters and then parse all of those that are not blank characters (dep[ending on what you use for padding.)

The data in your file should look something like this:

1_____John___________Doe____________
2_____Wilma__________Oostenhuizen___
3_____Susan__________Kuisis_________
4_____Greg___________Guy____________
5_____Polly__________Blant__________

For a table with the defenition of:
ID int(6)
FirstName char(15)
LastName char(15)

Reading the file:

read a single line and then parse it into the diffirent sections:
String strBuffer = read a line

ID int(6):
So wahat we need to do is to simply read the first 6 characters and remove all of the "_" instances and then  parse it into an integer
int id = Integer.Parse(strBuffer.subString(0,6).replaceAll("_",""));

FirstName/LastName char(15):
Again we read the location of what we want from the strBuffer but now we do not need to convert it to an integer but rather a string
String firstName = strBuffer.subString(6,21).replaceAll("_","");
String lastName = strBuffer.subString(21).replaceAll("_","");
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RuadRauFlessaCommented:
petmagdy please take a look at what he needs. It clearly states that he needs a fixed length entry within the file and he won't get that using XML.

Anyway back to the toipic

Writing to the file:

ID int(6):
firstly you would need to parse your int into a string then get it's length and pad it accordingly.
String strId = new String("" + id);
if (strId.length() < 6) {
   add "_" characters until it's length is == 6
}

I think that when looking at the above the two string values shouldn't be too difficult to figure out.
0
RuadRauFlessaCommented:
you are still not listening petmagdy.

""""2) The file should have fixed-length records and should use only simple data types: number, char etc""""
0
RuadRauFlessaCommented:
So you need stuff like that in a class together with checking for key field duplication and such.
On that i would advise you to keep a tree or list of your keys in memory as a normal dbms does. This makes searching and such verry fast although it might show the toll on memory.

So what you would have to do in your file/table class is to keep a list of keys. You can initially with the creation of the class read all of the key fields and then update it accordingly when something is inserted or deleted. You could also use a delay write methedology whereby you keep the data that is to be saved to the file in memory antil you have an opertune time to write it. The delayed write however may case some issues when reading the data because you have to remember to include the data that has not yet been written to the file. Then when you are writing you also need to check the list of key fields against that of the record being written so that you know if a key already exists and then you can operate accordingly.
0
thief77Author Commented:
thx for the above replies ill try to implement what you said so far and show some code here.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.