Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Create Vector of Object

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Create Vector of Object

    Hi, I'm a newbie.
    How can I create a vector of Object. Suppose I declare a class

    public class Book{
    String id;
    String Title;
    sign int published;
    };

    I want to make a data like this

    01 Romeo 2000
    02 Juliet 3000



    Thx.


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Create Vector of Object

    1. Wrap your code in code tags.
    2. Write Accessors and Mutators in the class.
    3. Declare all the variables as private.
    4. Make an object of Book class in the main class.
    5. Use Mutators to set the values.
    6. Use Accessors to get the values.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Create Vector of Object

    package friends;

    import java.sql.*;
    import java.util.*;

    public class Friends {

    public static void main(String[] args) {
    String DRIVER = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost/friends";
    String user = "bobby";
    String pass = "ganteng";
    Connection conn = null;
    Statement s = null;
    ResultSet rs = null;
    ResultSetMetaData rmd = null;

    Vector<friend> data = new Vector<friend>();

    try{

    Class.forName(DRIVER).newInstance();
    conn = DriverManager.getConnection(url, user, pass);
    s = conn.createStatement();
    System.out.println("Connected to database.");
    rs = s.executeQuery("select * from friends");
    rmd = rs.getMetaData();
    int col = rmd.getColumnCount();

    while(rs.next()){
    for(int i = 1; i <= col; i++){
    System.out.print(rs.getObject(i) + " ");
    }
    System.out.println();
    }
    friend fr = new friend();

    while(rs.next()){

    fr.id = rs.getString(1);
    fr.name = rs.getString(2);
    data.add(fr);
    }

    System.out.println(data.size());

    for(int i = 0; i < data.size(); i++){
    System.out.print(data.elementAt(i).id);
    System.out.println(data.elementAt(i).name);
    }

    rs.close();
    s.close();

    }

    catch(Exception e){
    e.printStackTrace();
    }
    }
    }

    class friend {
    String id;
    String name;
    }

    --------------------------------------
    Something wrong with the vector. It supposed to output

    1 J1
    2 J2
    3 J3
    4 J4
    5 J5

    But, it doesnt print anything. Pls help what wrong with Vector declaration.

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Create Vector of Object

    Paste here the full exception message.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

Similar Threads

  1. Create Object
    By TitanVex in forum Object Oriented Programming
    Replies: 8
    Last Post: December 29th, 2011, 10:24 PM
  2. create object problem
    By milas in forum Member Introductions
    Replies: 0
    Last Post: February 25th, 2011, 09:24 AM
  3. How do I create an object(function)?
    By amarettoCoffee in forum Object Oriented Programming
    Replies: 3
    Last Post: January 18th, 2011, 06:54 PM
  4. How to create a new object of another class within a method
    By davie in forum Object Oriented Programming
    Replies: 1
    Last Post: April 16th, 2010, 05:53 PM
  5. Create a CLOB object with the string value
    By oshoarun in forum JDBC & Databases
    Replies: 0
    Last Post: March 6th, 2010, 01:54 AM