Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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.

Posted on 2004-11-08
Medium Priority
Last Modified: 2010-03-31
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!

Question by:thief77
  • 5
  • 3
  • 2

Expert Comment

ID: 12521543
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?

Author Comment

ID: 12521560
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.

LVL 13

Expert Comment

ID: 12521633
Hi thief77,
 why don't u handle this single file as XML? is think it will be must easier
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 12521648
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.

Accepted Solution

RuadRauFlessa earned 2000 total points
ID: 12521652
(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:


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("_","");

Expert Comment

ID: 12521687
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.

Expert Comment

ID: 12521727
you are still not listening petmagdy.

""""2) The file should have fixed-length records and should use only simple data types: number, char etc""""

Expert Comment

ID: 12521753
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.

Author Comment

ID: 12521801
thx for the above replies ill try to implement what you said so far and show some code here.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month10 days, 23 hours left to enroll

571 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