Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how i can get the vector element?

 
paoh adam
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi..Could anyone tell me, how i can get the vector element individually. here is the code:

//this is the method to add the vector element. It is from the database
public Vector getPersonData(){
Vector myVectorAll=new Vector();
try{
this.dbConnection();
String sql="select name,email from personal order by name";
PreparedStatement prepareStmt=con.prepareStatement(sql);
ResultSet rs=prepareStmt.executeQuery();
while(rs.next()){
Vector myVector=new Vector();
myVector.addElement(rs.getString(1));// 1st data
myVector.addElement(rs.getString(2));// 2nd data
myVectorAll.addElement(myVector);
}
rs.close();
prepareStmt.close();
}catch(Exception ex){
System.out.println(ex.getMessage());
}
return myVectorAll;
}

//at main method
public static void main(String[] args){
CayukPah pah=new CayukPah(); //I created the object of the class
pah.dbConnection();

Vector p_vektor=new Vector();//create the new vector
p_vektor=pah.getPersonData(); //access the vector from the //getPersonData()
for(int i=0;i<p_vektor.size();i++){
System.out.println("Nama: "+p_vektor.elementAt(i));
System.out.println("Email: "+p_vektor.elementAt(i));
System.out.println("\n");
}
}

//After compile and run I got the result:
Nama: [apau, musyi@gmail.com]
Email: [apau, musyi@gmail.com]

Nama: [azlan, azlan@azlan.com]
Email: [azlan, azlan@azlan.com]

//Actually I want the output to be like this:
Name : apau
Email : musyi@gmail.com

//i tried to used split funtion but i am stuck with it. i used split(",") //and i got
Value:[apau
Value: apau @gmail.com]

Thanks in advance for any hints
Regards
-paoh-
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're treating the elements of the Vector as if they were strings, but in fact they're Vectors themselves. Try something like:

[ December 07, 2006: Message edited by: Ulf Dittmer ]
 
Divya Shastry
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have noticed that the vector you are returning is a vector of vectors , that's why you are getting the square brackets .
I feel there is no need for you to take a vector of vectors .

Take a look at the following code, and adapt it to your needs :

 
paoh adam
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.. Thanks to Ulf Dittmer and Divya Shastry for the reply.

I have tried both. And both give me the result like I want =).. Thanks or in Malay�s language we say �Terima Kasih� !!!
But when I tried Divya Shastry�s code by adding the elements from my database, it only shows the last element in the myVector�
 
Divya Shastry
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi poah adam

I am happy to know that the code worked...but I fail to understand why it didn't work when you connected to the database .It works fine for me. Can you paste the code, so that we can debug it ?
 
paoh adam
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Divya Shastry. Below is the code:

//this method I modify from your method
public Vector myMethod() {
Vector myVector=null;
try{
this.dbConnection();
String sql="select name,email from personal order by name";
PreparedStatement prepareStmt=con.prepareStatement(sql);

ResultSet rs=prepareStmt.executeQuery();
while(rs.next()){
myVector=new Vector();
myVector.addElement(rs.getString(1));// 1st data
myVector.addElement(rs.getString(2));// 2nd data
}
rs.close();
prepareStmt.close();
}catch(Exception ex){
System.out.println(ex.getMessage());
}finally{
return myVector;
}
}

//this at main
public static void main(String[] args){
CayukPah pah=new CayukPah();
pah.dbConnection();

Vector p_vektor=new Vector();
//-------1)try:Ulf Dittmer ---------
System.out.println("TRY GET DATA (1):1\n");
p_vektor=pah.getPersonData();
for(int i=0;i<p_vektor.size();i++){
Vector vec = (Vector) p_vektor.elementAt(i);
System.out.println("Nama: "+vec.elementAt(0));
System.out.println("Email: "+vec.elementAt(1));
System.out.println("\n");
}
p_vektor.clear(); //clear the elements
//-------2)try ivya Shastry ---------
System.out.println("TRY GET DATA (2):1\n");
p_vektor=pah.myMethod();
for(int k=0;k<p_vektor.size();k++){
System.out.println("Nama: "+p_vektor.elementAt(k));
System.out.println("Email: "+p_vektor.elementAt(++k));
System.out.println("\n");

}
}

Regards,=)
-paoh-
 
Divya Shastry
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paoh
As I already mentioned before, I considered a single vector and stored all the values in the single vector. Thus the code which I wrote also works only if you store all the details that you get from the database in a single vector. According to the code in your method where you are adding elements to the vector,


You are creating a new Vector every time you extract the information from each row of the table. Thus when you try to get the information from the vector in the main method using my code, you only get the last row information.

If you use only a single vector to store all the information like :



All the data is stored only in 1 vector, and thus my code for retrieving the data from that single vector will work fine !

I am assuming that you are using Vectors only to store and retrieve information, and that's why I felt that you need not use a vector of vectors.

But I am not sure which piece of code is more efficient.

Comments are welcome !




 
paoh adam
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Divya Shastry for noticing me the mistake I have made in my code.

Since that I�m quite new with java, I myself also not sure which piece of code is more efficient.. but, I really enjoy to learn new things..
Thanks you again
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi paoh adam.

Welcome to javaRanch

in malay language.. "Salamat Datang to JavaRanch" am i correct.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic