Mobile Phone | Xbox Mod Chip | Cheap Car Insurance | Web Games | Actress
Help with java databse [Archive] - PCMech Forums

PDA

View Full Version : Help with java databse


Bull
12-30-2000, 12:22 PM
Can any one help mi with a college assignment i have.
I,ve got to produce a database in java based upon a Bank or building society, Can any one give me any help on the classes i need or direct me to any Tutorial web sites on java databases.

Paul Victorey
12-30-2000, 06:14 PM
Ok, first, what features does the database need, and how fast must it run?

Features I am referring to:

1) Is search time important? Do you need to search by multiple fields, or only by a single one?
2) Is add/remove time important?
3) Do you need to be able to traverse the database in some particular order, e.g. do you need to be able to do something like print all records in alphabetical order?
4) Will you be writing to/reading from a disk, or will the database only exist in memory?
5) How large of datasets must the database operate on? It's a veeeery different thing if your code has to handle, say, 20 records vs. 200,000.
6) Do you ever really need to sort the records at all, or are searching/adding/removing the primary functions of the database?
7) Have they specified how the database should be implemented (array, linked list, BST, hash table, etc) or is it up to you to choose?

Also, what is the focus of the course? Is it a course designed only to teach Java, or have you been talking about efficiency and time-orders and the like?

Classwise, I'd say you need 3 major classes:

Record -- contains all the data to implement a single record in the database
Database -- provides an interface for adding, removing, finding, etc. records.
DataTest -- a place for your main method, where you create a Database object, add a bunch of Record objects, and verify correct operation

This is only a rough guess based on the information you provided; I don't know fully all that they want you to do.

Bull
12-30-2000, 07:01 PM
Thanx for your reply Paul in such a short time.

One of my course modules this year is 'java'. After completing Visual Basic and C programming last Term.
The main objective really is to progress and obtain a fundemental understanding of OO Programing which i seem to have taken on board so far. I,ve attempted a simple Vehicle class dba so far but nothing too hard.
My task is to create and implement a Bank Database in Java with the following functionality;

*create accounts(deposit and current)
*make a deposit
*make a withdrawel
*view account details(including personel info)
*a method to simulate the date changing to implement charges and pay interest, this could be a simple 'quartely update' option but a oppurtunity for a Date class exists where an 'increment day' method could be implemented.

I also need to include details such as charges and interest rates which i,ve been supplied.

I think i might attempt the database in Access to try to figure out its functionality.

Any help is welcome

Regards
BULL @(--)@

Paul Victorey
12-30-2000, 08:28 PM
What are the limits on outside help? How much help can I give before you get in trouble?

I'll tell you, I'd do this as a doubly linked list.

Under this, you'd do a class like this:


class Record{
// all the data you need to collect on each person
// all the methods that operate on a record (account)

Record prev;
Record next;
//initialize both prev and next to null in the constructor
}

class Database{
private Record first;
private Record last;
private Record current;

//initialize all pointers (first, last, current) to null in constructor.

public void Start(){
// make current point to the first object
}

public void Advance(){
// make current point to the object after the one it currently points to. Check if current is null before doing this!
}

public bool IsCurrent(){
//return true if current is not null.
}

public Record GetCurrent(){
// return current
}

public void AddRecord (Record r){
//Add a record. Two general cases: empty list, nonempty list.
}


}



The way Database looks is like a list of records. Imagine one with 4 records, here I'll call them A through D. Each record R has 2 pointers, like this:

R.prev <- R -> R.next

Note that first.prev and last.next will both equal null.

Overall, the whole database of 4 objects conceptually looks like:

null <- A <-> B <-> C <-> D -> null

In this case, first would point to A, and last would point to D. Signify this with (f) and (l), and we get this picture:

null <- A(f) <-> B <-> C <-> D(l) -> null

Now, let's trace how adding a new record, E, will work.

Assume we have a Database object db, and a Record object E, and are doing db.AddRecord(E);

First, we add E after D in the list, by setting D.next to E.

null <- A(f) <-> B <-> C <-> D(l) -> E -> null

However, E is not backlinked to D yet. Set E.prev to D:

null <- A(f) <-> B <-> C <-> D(l) <-> E -> null

But, by definition, last should point to the last object we have linked, so we change it in the third and final step:

null <- A(f) <-> B <-> C <-> D <-> E(l) -> null

Up until this point, we've ignored current. Current is used in 'for' statements and elsewhere to go through the list systematically, for searches, for day advances, for anything else we need to do to each record.

Let (*) refer to current. Now, we execute

db.Start();

null <- A(f)(*) <-> B <-> C <-> D <-> E(l) -> null

after doing a db.Advance(), we would see:

null <- A(f) <-> B(*) <-> C <-> D <-> E(l) -> null

After 3 more, we would have:

null <- A(f) <-> B <-> C <-> D <-> E(l)(*) -> null

And, after one more, current should go to null -- this makes loops easier, you'll see how.

null <- A(f) <-> B <-> C <-> D <-> E(l) -> null (*)

Now, Advance should NOT change current in any way. Current is now a null pointer, and attempting to access its data members can't be done.

Why this is useful:

You can use a for loop like this to go through all records, easily:

for (db.Start();db.IsCurrent();db.Advance()){
Record r = db.GetCurrent();
// do whatever you have to with r here.
}

Because of the "fall off the end of the list" feature of current, it's easier to make loops that work right; otherwise you can easily get cases in which you never access the last record of the list. This is the simplest way to do linked lists, and one I learned and liked for how it can handle any situation.

Bull
01-01-2001, 12:14 PM
Cheers for your info regarding my database.
I,m going to have a good go in the forthcoming week and hopefully have a working model to show for my efforts.
When its complete, i,ll mail you a version but i think it will only be a "cheap and cheerful" version of a bank dba.
Would you recommend using any Visual Java packages 'out there' as we are using the traditional JDK 1.7.

@(--)@

Paul Victorey
01-02-2001, 02:42 AM
You shouldn't even need to use any of the advanced features of the compiler-specific java classes. Assuming you're using a text interface (or not coding any interface at all) you'll never make use of anything beyond standard java.

Also, if you need some reference to look at, the online notes from my last Java class are up at http://www.cs.wisc.edu/~cs367-1/topics.html . Check out the linked lists sections, as I think they're your best bet. I recommend using a non-circular but doubly-linked list, as the easiest to work with. Doubly linked makes removing objects much easier (if you never have to remove, and want to save 4 bytes per record you can make it singularly linked), circular reduces some checks, but introduces others. Generally, if you screw up the checking on a non-circular LL you'll get a null pointer exception; if you screw up in a circular you'll infinitely loop, or have some more hard-to-find errors like accidently doing 2 operations on one record, or skipping one. Being as the non-circular code is cleaner and easier to write, I never use circular unless the assignment specifically mandates it.