This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes Database project like SCJD (threads, server/client, locking) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Database project like SCJD (threads, server/client, locking)" Watch "Database project like SCJD (threads, server/client, locking)" New topic
Author

Database project like SCJD (threads, server/client, locking)

Richard Henderson
Ranch Hand

Joined: Feb 18, 2013
Posts: 42
I'm studying at a private terciary provider and currently have a module based on the SCJD. The module is way above the level of study so far and the tutor who will mark it has told me that it's relatively easy to get around %70 (C) but very hard to get higher grades. I don't want to ruin my average though, so i want to do as good as i can on this, which means i need a lot of help...

I have the Apress SCJD book for reference.

So. What i need to do is make a client/server app which uses sockets or RMI. Which needs to access and write to a Data Base File, presents the data in a JTable and needs to lock records when one of the clients is working with them. And a few other things.

What i want to do first is get the database working. For that i need to read the file with a random access method and transfer the info to a table via an array or collection. Then do the same in reverse to add or update a record. But i don't really know how to do that...

All help is appreciated.
Richard Henderson
Ranch Hand

Joined: Feb 18, 2013
Posts: 42
Ok, i've got a random access method reading data out of my file but now i'm trying to figure out how to put it into a String[]. The String[] can then be displayed in the JTable.
Richard Henderson
Ranch Hand

Joined: Feb 18, 2013
Posts: 42
I'm getting a null pointer exception when i try to put the byte arrays into strings in the records[][]...

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Which line does the Exception occur on? Then you need to check that line to see what might be null.
Why are you using the equals method on an array? It is not overloaded. You need to go through the Arrays class to look for equals methods.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Richard Henderson wrote:

That line creates an array that can take 6 arrays of String. Each of those 6 slots is still null though. I think you have the dimensions the wrong way around - you want to store arrays that can take 6 Strings, but you don't know how many. A List<String[]> is probably a better choice.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Richard Henderson
Ranch Hand

Joined: Feb 18, 2013
Posts: 42
Thanks.

Yeah, according to the Sierra/Bates SCJP book, i don't have to declare the size of the second []. Which would have been nice as it would allow for a variable sized array (it would have been sideways though).

Declaring the size of both,
ie: String [][] records = new String [6][40];
fixed the problem. Seeing as how i had to declare both i switched them back around to [40][6].

So i got my JTable working.

Next i'm going to work out how to search and update the records. Via the Table.


edit: @ Campbell. Line 37 threw the exception. But it was because the array size wasn't declared, like Rob said.

I used the .equals() method to compare the contents of the 2 byte flag to a sample 2 byte array. This 'feature' was to indicate valid records.

I'll check out a List<String[]>, but as this is working now i probably will keep it.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Richard Henderson wrote:Thanks.

Yeah, according to the Sierra/Bates SCJP book, i don't have to declare the size of the second []. Which would have been nice as it would allow for a variable sized array (it would have been sideways though).
. . ..
You don’t, but then you get 6 nulls, and using them caused the Exception.
You can still get different array sizes
myArray[3] = new Foo[]{f1, f2, f3};
Richard Henderson
Ranch Hand

Joined: Feb 18, 2013
Posts: 42
OK, new problem. I am getting "non static method getSearchName() cannot be referenced from the static context" error from this code:
(lines 15, 17)


As far as i'm aware, i'm not calling it from a static method or have any other statics related to it in anyway. I can't figure out where this "static context" is coming from.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
You are calling getSearchName on the name of a class. That counts as static. It is difficult to create good error messages for every circumstance, and that is not a good error message.
Richard Henderson
Ranch Hand

Joined: Feb 18, 2013
Posts: 42
Oh, i thought that was how to call a method in a different package.

What should be written there instead?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
You need to create an instance of that class, then call getSearchNames on that instance. In fact, the same as for any instance method. Only you have to import the class. It is better style to import an individual class than a whole package.
Richard Henderson
Ranch Hand

Joined: Feb 18, 2013
Posts: 42
OK, makes sense now.

I just went through and made everything static.

I might have to change some stuff later when i do the sockets or RMI.
Richard Henderson
Ranch Hand

Joined: Feb 18, 2013
Posts: 42
Well, I've moved on a fair bit from what is above, mostly copying and modifying the sample project code in the Apress book.

I have a new issue when it comes to adding data to my "database" file using these methods:


.getUpdateData returns a String from a JTextField and replaces the last index of the array.
System.out.printlns are there for me to see where the execution has reached.

The trouble happens when it writes that update data. the position in the file is not consistent and the characters don't show properly, leading me to suspect that it's not reading the chars in the String by bytecode instead.

I'm stumped.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Database project like SCJD (threads, server/client, locking)