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.
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.
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.
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
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.
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