11 Replies - 700 Views - Last Post: 15 December 2010 - 01:07 PM Rate Topic: -----

#1 sam_benne  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 16
  • View blog
  • Posts: 732
  • Joined: 16-January 08

Using an Array as a Database

Posted 14 December 2010 - 04:31 PM

What I am doing is storing data in an array and then accessing it. I want to be then able to change that data and use it later on.

So what I have done is created a public class with a method. This method allows me to access the array that is inside.
    public void user() {
        //Default User Data
        user[0] = "Karzar"; //Character Name
        user[1] = "50"; //Health
        user[2] = "loc1"; //Default Location - Starting point
    }


I have a function that allows me to change that data. This does work however, when I access that data again it has gone back to what it was before. So what I am asking is how do I permanently change the data or make:

LoadUserData User = new LoadUserData();
User.user();


Into a global statement. As I know calling this again will make the data go back but I cannot work out how to globalize it even with Google.

Is This A Good Question/Topic? 0
  • +

Replies To: Using an Array as a Database

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10573
  • View blog
  • Posts: 39,148
  • Joined: 27-December 08

Re: Using an Array as a Database

Posted 14 December 2010 - 04:52 PM

Not really good practice to use parallel arrays, or multidimmensional arrays as parallel arrays. Design a Player class instead, and store a Player[]. Leave the table design for relational database theory and SQL.

For more information on class design, check out my tutorial Moving Away From Parallel Arrays.
Was This Post Helpful? 0
  • +
  • -

#3 sam_benne  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 16
  • View blog
  • Posts: 732
  • Joined: 16-January 08

Re: Using an Array as a Database

Posted 14 December 2010 - 05:09 PM

Thanks will look at your tutorial, the only reason why I am doing it this way is because I could never get DB to work in Java. Almost decided to stop and do it in PHP.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10573
  • View blog
  • Posts: 39,148
  • Joined: 27-December 08

Re: Using an Array as a Database

Posted 14 December 2010 - 05:20 PM

JDBC takes a little more work to setup than PHP. Even if you use PHP but not the database aspect, you should still use classes and Objects with it. OOP allows for much cleaner organization of your code. :)
Was This Post Helpful? 0
  • +
  • -

#5 sam_benne  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 16
  • View blog
  • Posts: 732
  • Joined: 16-January 08

Re: Using an Array as a Database

Posted 14 December 2010 - 05:26 PM

I meant I would use PHP instead of doing my little project in Java. But I have tried all different ways of getting a DB setup in Java but I could never get it to work.
Was This Post Helpful? 0
  • +
  • -

#6 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Using an Array as a Database

Posted 14 December 2010 - 05:28 PM

What IDE are you using? What database is it? Postgres? MySQL?
Was This Post Helpful? 0
  • +
  • -

#7 sam_benne  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 16
  • View blog
  • Posts: 732
  • Joined: 16-January 08

Re: Using an Array as a Database

Posted 14 December 2010 - 06:11 PM

Netbeans and would be MySQL.
Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2873
  • View blog
  • Posts: 11,032
  • Joined: 15-July 08

Re: Using an Array as a Database

Posted 14 December 2010 - 07:23 PM

I like using Apache Derby for my Java Databases. It is made with JDBC in mind and the setup manual is great!

Here's the installation guide:
http://db.apache.org...l_software.html
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Using an Array as a Database

Posted 14 December 2010 - 09:44 PM

HSQLdb. It stores Java object as "object" no need to serialize in BLOB, CLOB, DLOB or whatever
Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5833
  • View blog
  • Posts: 12,689
  • Joined: 16-October 07

Re: Using an Array as a Database

Posted 15 December 2010 - 05:21 AM

Global is BAD. Arrays for complex data, also bad. The actual data store you use, a database or just some flat file, shouldn't really matter to the rest of the program. Define some classes and hide the persistence mechanism in one.

e.g.
class User {
	public String characterName;
	public int health;
	public String location;
}

class DataManager {
	public User loadUser() { /* your code here */ }
	public void saveUser(User user) { /* your code here */ }
}

class State {
	public static State getInstance() { /* your code here */ }
	private State { /* your code here */ }
	public void saveChanges() { /* your code here */ }
	public User getCurrentUser() { /* your code here */ }
}


Was This Post Helpful? 0
  • +
  • -

#11 sam_benne  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 16
  • View blog
  • Posts: 732
  • Joined: 16-January 08

Re: Using an Array as a Database

Posted 15 December 2010 - 08:00 AM

The reason why I used an array is that I will be having more than just the three data above as they are just there while I start so after a while loads of functions.
Was This Post Helpful? 0
  • +
  • -

#12 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5833
  • View blog
  • Posts: 12,689
  • Joined: 16-October 07

Re: Using an Array as a Database

Posted 15 December 2010 - 01:07 PM

What's your point? This actually sounds like a worse reason to use an array.

Perhaps you're thinking of a collection of key-value pairs? If so, then just use a Dictionary and call it a day.

If you want your data to be meaningful to the rest of your program, using attributes like class variables, where errors can be caught at compile time, is still preferred.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1