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