File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes how i can get the vector element? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how i can get the vector element?" Watch "how i can get the vector element?" New topic
Author

how i can get the vector element?

paoh adam
Greenhorn

Joined: May 26, 2006
Posts: 11
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
Marshal

Joined: Mar 22, 2005
Posts: 41089
    
  44
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 ]

Ping & DNS - my free Android networking tools app
Divya Shastry
Greenhorn

Joined: Dec 05, 2006
Posts: 11
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

Joined: May 26, 2006
Posts: 11
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

Joined: Dec 05, 2006
Posts: 11
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

Joined: May 26, 2006
Posts: 11
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

Joined: Dec 05, 2006
Posts: 11
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

Joined: May 26, 2006
Posts: 11
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

Joined: Dec 06, 2005
Posts: 1321

hi paoh adam.

Welcome to javaRanch

in malay language.. "Salamat Datang to JavaRanch" am i correct.


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how i can get the vector element?
 
Similar Threads
use c:forEach tag inside another c:forEach tag
Failed to load JDBC/ODBC driver.
Vectors(again)