Reading from Vector


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 25

Thread: Reading from Vector

  1. #1
    Join Date
    Apr 2004
    Posts
    28

    Reading from Vector

    I have successfully connected my application to an Access database using JDBC and have put these details into a Vector.

    The database is of user details ie records of users however in the Vector each record is stored sequentially ie user1,name ,tel, etc User2, name, tel etc

    Does anyone know how I can put this into a JTable

    ie
    UserID, Surname
    User 1 , name etc
    User 2 , name etc

    Thanks in advance

    GM

  2. #2
    Join Date
    Dec 2002
    Posts
    83
    It would be best to create a class that holds the attributes you are dealing with, and put instances of that class into the Vector.
    Code:
    class UserInfo
    {
    	private String userId = null;
    	private String name = null;
    	private String phone = null;
    
    	// empty constructor
    	public UserInfo() {
    
    	}
    
    	// attribute constructor
    	public UserInfo(String aUserId, String aName, String aPhone) {
    		setUserId(aUserId);
    		setName(aName);
    		setPhone(aPhone);
    	}
    
    	public String getUserId() {
    		return userId;
    	}
    	public String getName() {
    		return name;
    	}
    	public String getPhone() {
    		return phone;
    	}
    
    	public void setUserId(String aUserId) {
    		userId = aUserId;
    	}
    	public void setName(String aName) {
    		name = aName;
    	}
    	public void setPhone(String aPhone) {
    		phone = aPhone;
    	}
    
    }
    So grab each user's info, create an instance of UserInfo with it, and put it in the Vector. When you loop through the Vector later, pull out each object and cast it as a UserInfo and access the attributes with the getter methods. This assumes you don't have a need to access just a specific user's info at any point. To do that you'd want a different data structure.
    -- Steven

  3. #3
    Join Date
    Feb 2004
    Posts
    541
    firstly, definately do what reinkesm says about making a User object to store all the data and use getter and setter methods to access it. Then store the User object in the Vector (or whatever else you want to store it in).

    To display the data in a JTable you need to create a class that extends AbstractTableModel and overides all it's abstract methods. The JTable will use this object to determine what to display where. I won't explain how to use it here because sun have done a wonderful job of explaining it on their own. If you go here you'll be taken to suns "How to use JTable" page. I read this a few weeks ago and in half an hour i'd knocked up an AbstractTableModel to suit my needs. You'll need to add your Vector of information to this abstract table model object using a method of your own making, and the methods in the object will be tailored to your data so all the JTable has to do is ask "What should i put in row 2 column 5" and your data model will know what to tell it.

  4. #4
    Join Date
    Apr 2004
    Posts
    28
    Playing Devils advocate here

    But are you seriously suggesting I use a UserClass to store the user details. I thought that was what the Access database did.!!!! When I need to get info I use JDBC and SELECT and when I need to set data I use UPDATE. My problem is that when I get ALL the records from the database I cant seperate them out into individual users using a Vector ie

    3,Mr,GM,Hutcheson,Administrator,Development,2365,null,null,4,Mrs,J,Hitler,Clerk,Sales,6546 ,null,null,6,CAPT,null,Peel,Traffic,Ops,null,null,null etc

  5. #5
    Join Date
    Dec 2002
    Posts
    83
    Yep that's what I'm suggesting! The database is for persistence, a way to hold all of your data outside of the application. The Java class (ie: UserInfo) is for holding the data for use within the code. The UserInfo objects only live for a short amount of time, while the database lives forever.

    Almost always, if you are getting a set of data from a database (ie: user information), the first thing you do is put it into an Object so you can easily pass it around to different methods and use it in your business logic, processing, etc.

    Database calls are very very "expensive". Your goal is to read the data from the database once, store it in Java objects and manipulate it that way throughout your code, then when you are ready, and if it's needed, write any changes back out to the database.
    -- Steven

  6. #6
    Join Date
    Feb 2004
    Posts
    541
    But are you seriously suggesting I use a UserClass to store the user details. I thought that was what the Access database did.!!!! When I need to get info I use JDBC and SELECT and when I need to set data I use UPDATE. My problem is that when I get ALL the records from the database I cant seperate them out into individual users using a Vector ie
    You just about answered your own question there. You suggested a plan for the program (using the database to get and set data), thought it through a bit, and then realised it's huge limitations. A user class wouldn't simply store the data in your program, it would manage it too. It will validate the input you're trying to write to the database (eg Date of Birth should be a valid date, not a random bunch of characters, or age should be a valid number, not a word). Also, it encapsulates and greatly simplifies your program. You only need to write the code once, and any number of users can be represented by it. If you alter it, it alters it EVERYWHERE you call one of it's mehtods. If theres something wrong with it you can easily seperate it from the rest of your program and test it independant of anything else that your program might do. You never have to worry about things like "if i change this line here, will another line all the way over there still work" because it's all internal to the UserClass only.

    Also, as reinkesm pointed out, accessing the database takes a lot of system resources. I doubt you could do anything your computer couldn't handle with a simple database in a simple situation, but imagine if there were potentially thousands of people using the database all at once (such as an online store). If they repeatedly asked to see some bit of information about themselves it would clog the entire system up. whereas if the system got their info once and stored it then there wouldn't be such a problem. Many large database systems (such as banks) only perform intensive transactions once a day so the bandwidth isn't constantly clogged up.

  7. #7
    Join Date
    Feb 2004
    Posts
    808

    Re: Reading from Vector

    Originally posted by Green_Manalishi
    in the Vector each record is stored sequentially
    well thats a daft idea... the whole idea of a Vector storing objects is that it is a more flexible storage container than a single array.

    at the moment you effectively have a one dimensional array.. suppsoe you have 3 fields per record and 3 records, youve done this:

    String[] details = new String[9];

    and then stored the details sequentially in the array..

    If you go 2 dimensional you will have a data model that actually matches how a JTable looks.

    You dont necessarily need to create a custom object for your records but remember that java is an OBJECT ORIENTED programming language... you SHOULD create objects.. youre modelling users, create a User object. Then you can have a sequence of User objects in your vector.

    If thats all a bit complicated, dont forget that in java, an array IS AN OBJECT no matter what it holds... yup.. tis true, int[] myInts = new int[10] is actually an object.. so if you cant be bothered making an object for your users, just shove all their details into a string array and shove the array into the Vector...

    PHP Code:
    Vector v = new Vector();

    while(
    rs.next())
      
    String[] userDetail = new String[3];
      for(
    int i=0i<userdetail.lengthi++){
        
    userDetail[i] = rs.getString(i);
      }

      
    v.add(userDetails);

    by the way.. vector is a oooold, slow, inefficient data container. Use an ArrayList instead.. or some other member of the List framework..
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  8. #8
    Join Date
    Apr 2004
    Posts
    28
    Mike, cj and reink ... Thanks for your input.

    Thanks to your efforts I am now beginning to see some light in this dark tunnel.

    Funnily enough I have tried using Hashtables, Hashmaps ArrayLists and Vectors and have came full circle again. The annoying thing is that it takes so much effort just to advance a wee bit in the implementation phase.

    Just one more question before I kill this topic off.

    one field of my user details is a photo which is stored in the database as an Object and in Java.

    Each time I retrieve it I just get a hex dump of the bitmap appear on my screen. Is there an easy method for handling images between access and my Java program?

    Thanks

  9. #9
    Join Date
    Feb 2004
    Posts
    808
    you should convert the photo into a BufferedImage, or ImageIcon or something like that, in java, then use an ObjectOutputStream when you upload it to the database, and store it in a BLOB. When the time comes to read the Blob, use an ObjectInputStream, and read the data back into an Object, then cast the Object into what it is supposed to be, e.g. a BufferedImage

    alternatively.. just copy the image file to the database server, DONT put it in the database, and then store the PATH to the image, in the db.. upon retrieve, download the image from the specified path and put it into your app. Better.
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  10. #10
    Join Date
    Apr 2004
    Posts
    28
    Originally posted by reinkesm So grab each user's info, create an instance of UserInfo with it, and put it in the Vector. When you loop through the Vector later, pull out each object and cast it as a UserInfo and access the attributes with the getter methods. This assumes you don't have a need to access just a specific user's info at any point. To do that you'd want a different data structure. [/B]
    I have done exactly as you said but when I try and get the Employee(UserInfo) back out again, I cant get it into the correct format...for example

    I store the userdetails in an instance of Employee then add this to an ArrayList

    When I iterate through the ArrayList I can get the individual Employee objects out using

    ArrayListName.get(i) <---returns Object

    When I try to cast it as an Employee it says I need an Object


    Any ideas ??

  11. #11
    Join Date
    Feb 2004
    Posts
    541
    show us how you are casting it...it should work

  12. #12
    Join Date
    Apr 2004
    Posts
    28
    Originally posted by reinkesm So grab each user's info, create an instance of UserInfo with it, and put it in the Vector. When you loop through the Vector later, pull out each object and cast it as a UserInfo and access the attributes with the getter methods. This assumes you don't have a need to access just a specific user's info at any point. To do that you'd want a different data structure. [/B]
    I have done exactly as you said but when I try and get the Employee(UserInfo) back out again, I cant get it into the correct format...for example

    I store the userdetails in an instance of Employee then add this to an ArrayList

    When I iterate through the ArrayList I can get the individual Employee objects out using

    ArrayListName.get(i) <---returns Object

    When I try to cast it as an Employee it says I need an Object


    Any ideas ??

  13. #13
    Join Date
    Feb 2004
    Posts
    541
    well nowhere in your post does it show casting, so i don't know what you're doing wrong. it should look something like this though
    Code:
    Employee anEmployee = (Employee)ArrayListName.get(i);

  14. #14
    Join Date
    Apr 2004
    Posts
    28
    This code is in a Class called DBAccess used to access the database using JDBC

    // Loop through the rows retrieved from the query
    while (more) {
    Employee e = new Employee();//create new user
    v1 = rs.getInt("UserID");e.setUserID(v1);
    v2 = rs.getString("Title");e.setTitle(v2);
    v3 = rs.getString("Initials");e.setInitials(v3);
    v4 = rs.getString("Surname");e.setSurname(v4);
    v5 = rs.getString("Role");e.setRole(v5);
    v6 = rs.getString("WorkDept");e.setWorkDept(v6);
    v7 = rs.getString("Tel");e.setTel(v7);
    v8 = rs.getInt("FingerPrint");e.setFingerprint(v8);
    v9 = rs.getInt("Retina");e.setRetina(v9);
    v10 = rs.getObject("Picture");e.setPicture(v10);
    v11 = rs.getInt("Pin");e.setPinNo(v11);
    v12 = rs.getInt("SL");e.setSL(v12);
    v13 = rs.getBoolean("Door1");e.setDoor1(v13);
    v14 = rs.getBoolean("Door2");e.setDoor2(v14);
    v15 = rs.getBoolean("Door3");e.setDoor3(v15);
    v16 = rs.getBoolean("Store");e.setStore(v16);
    v17 = rs.getBoolean("ServerRoom");e.setServerRm(v17);
    v18 = rs.getBoolean("Spare1");e.setSpare1(v18);
    v19 = rs.getBoolean("Spare2");e.setSpare2(v19);
    v20 = rs.getBoolean("Any");e.setAny(v20);
    v21 = rs.getBoolean("Day");e.setWorking(v21);
    v22 = rs.getBoolean("Night");e.setNight(v22);
    v23 = rs.getBoolean("Locked");e.setLocked(v23);

    allData.add(e);//add each iterated Employee to the ArrayList
    more = rs.next();
    }

    rs.close();
    stmt.close();//
    } catch (SQLException e) {}
    //Code used to see if I can get it back in its native form
    for (int i= 0;i<allData.size();i++){
    Employee r = allData.get(i);
    // do something with r (Employee)

    any ideas

  15. #15
    Join Date
    Apr 2004
    Posts
    28
    Originally posted by reinkesm So grab each user's info, create an instance of UserInfo with it, and put it in the Vector. When you loop through the Vector later, pull out each object and cast it as a UserInfo and access the attributes with the getter methods. This assumes you don't have a need to access just a specific user's info at any point. To do that you'd want a different data structure. [/B]
    I have done exactly as you said but when I try and get the Employee(UserInfo) back out again, I cant get it into the correct format...for example

    I store the userdetails in an instance of Employee then add this to an ArrayList

    When I iterate through the ArrayList I can get the individual Employee objects out using

    ArrayListName.get(i) <---returns Object

    When I try to cast it as an Employee it says I need an Object


    Any ideas ??

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center