Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Database in java?

Options
  • 21-07-2004 7:45pm
    #1
    Registered Users Posts: 2,327 ✭✭✭


    Is there anyway i can store say 4 values in a database that are linked to an object so that when i call the object, the 4 values will come up?


Comments

  • Closed Accounts Posts: 35 Ivan Dunaev


    what do you mean by "call the object"?


  • Closed Accounts Posts: 92 ✭✭tempest


    Entity Beans

    Java Data Objects

    Hibernate

    and probably loads more...

    It really depends on what you want to do, why you want to do it, budgets, time constraints, deployment platform limitations, etc..


  • Registered Users Posts: 2,327 ✭✭✭NeoSlicerZ


    Doing it myself as part of a project..

    I intend to use it to store the properties of say a tank. So whenever the tank is in action i.e moving/firing , those values can be called up..


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    I assume you mean that you need to be able to store loads of different tanks, e.g. if you have a game where each player has his own tank, then he logs in, and when the game loads up, it loads his tank, with its individual properties?

    So when you're loading the user's profile, part of it calls something like
    GenerateTank(int UserID)
    where UserID is an index corresponding to a row in a database, storing the properties of his tank.

    Check out the JDBC connector on www.mysql.com. It's a wee bit fiddly, but very very good.


  • Closed Accounts Posts: 35 Ivan Dunaev


    you should define way to identify your objects. then you can bind values to identificator in DB and select it every time you need with "select ........ where id=your_id" query


  • Advertisement
  • Registered Users Posts: 1,372 ✭✭✭silverside


    I would suggest MySql as well

    set up a simple database with a table
    TANKS
    with fields
    TANKID
    WEIGHT
    SPEED
    etc

    and set up simple queries to store/retrieve a single tank or collection of tanks by tankid (if you know it) or by the other attribute (if you dont).

    JDBC sounds good as well, not sure how you would go about it in Java. I am using a c++ wrapper around odbc and it is quite painless.


  • Registered Users Posts: 2,327 ✭✭✭NeoSlicerZ


    unfortunately i cannot setup a datebase in here....
    *shakes fist at restrictions*

    thus i am told my only alternative is a hashtable.. which i have no idea of how to use.


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Well, there's nothing stopping you from using a delimited text file as your database.

    If you're using a Windows box, you can also set up an Access DB, and set up an ODBC connection to it. Not sure how to do this in Java.


  • Registered Users Posts: 2,327 ✭✭✭NeoSlicerZ


    You see, i have no idea how to do that.. :(


  • Closed Accounts Posts: 35 Ivan Dunaev


    do you have no idea how to use text file for your purposes or what?


  • Advertisement
  • Registered Users Posts: 2,327 ✭✭✭NeoSlicerZ


    I'm trying to find out what's the best way to do it and how to do it.


  • Closed Accounts Posts: 35 Ivan Dunaev


    what's estimated data size?


  • Registered Users Posts: 1,372 ✭✭✭silverside


    It is a lot easier to use SQL/ODBC.

    Do you need to be able to :
    Create a database programatically? or can you ship an empty one?

    Create an ODBC datasource programatically? or can you ask the user to set one up?

    Are you happy with using a wrapper around ODBC (JDBC) to process SQL queries?

    Do you know SQL?

    You could stream objects in and out of a text file, but IMO that is reinventing the wheel, and does not easily give you random access.


  • Registered Users Posts: 2,327 ✭✭✭NeoSlicerZ


    this is the point where i stop understanding stuff.. thus the asking and the reading up on stuff, currently reading a tutorial of JDBC..


  • Registered Users Posts: 1,372 ✭✭✭silverside


    Sorry. :)

    Think about what you want to be able to do (in simple terms) and spell it out. It will help you understand the problem before you get too deep into any particular technology. And if you post it here, we may give you more focussed suggestions.


  • Registered Users Posts: 2,327 ✭✭✭NeoSlicerZ


    Well what i want to be able to do is to associate a set of values with a tank, so that when the tank is selected, these values will be selected along with it. I've gotten a new suggestion from my instructor to use FileOutputStream to do it. Though i'm not very sure how to use it.


  • Registered Users Posts: 1,372 ✭✭✭silverside


    one very simple way

    text file e.g.
    id,weight,speed
    Tank1,200,100
    Tank2,300,400

    Program starts up
    Opens text file
    Reads contents into array of TankInfo objects
    when you want to find the speed of Tank1, read through the array until name=="Tank1" and look at the other fields.

    When you add a new tank or change an existing one, delete the text file and write a new file with the complete list of new TankInfo objects converted into text.

    does that sound OK?


  • Registered Users Posts: 3,548 ✭✭✭Draupnir


    if you cant use a database you could use FileOutputStream and FileInputStream to read and write from a file which contains all the tank values. if you use indexes in the file then you can use it like a make shift, and rather slow, database solution.


  • Registered Users Posts: 4,676 ✭✭✭Gavin


    just serialise the object ?


    Gav


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Originally posted by silverside
    one very simple way

    text file e.g.
    id,weight,speed
    Tank1,200,100
    Tank2,300,400

    Program starts up
    Opens text file
    Reads contents into array of TankInfo objects
    when you want to find the speed of Tank1, read through the array until name=="Tank1" and look at the other fields.

    When you add a new tank or change an existing one, delete the text file and write a new file with the complete list of new TankInfo objects converted into text.

    does that sound OK?
    Exactly what I was thinking. If you have only a handful of tank object (<50), the time to load them into memory shouldn't be an issue.


  • Advertisement
  • Registered Users Posts: 1,372 ✭✭✭silverside


    for those of us who don't know Java that well (including me)
    can you tell us if it is a trivial thing to serialise a simple object to and from a text file?

    In that case it might be the best quick solution.


  • Registered Users Posts: 2,327 ✭✭✭NeoSlicerZ


    Now to figure out how to do it :)


  • Registered Users Posts: 4,676 ✭✭✭Gavin


    http://java.sun.com/docs/books/tutorial/essential/io/serialization.html

    Make your tank class implement Serializable. You don't need to write any methods, just stick implements Serializable after public class whatever.

    Lash all your tank objects into a vector. Serialize the vector using ObjectOutputStream

    and deserialize using ObjectInputStream. It's piss easy, the link above shows how to do it.

    Gav


Advertisement