wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Vectors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Vectors" Watch "Vectors" New topic
Author

Vectors

Peter Simard
Ranch Hand

Joined: Oct 31, 2001
Posts: 54
I'm storing the result set from a query in a vector. Each result returns one row of data from the SELECT statement. At present I'm extracting the data I want and using addElement, so my vector looks like this:
element 0 holds product id,
element 1 holds product name,
then I repeat. (These are cast as Strings by the way)
My question is: Should I store the entire result set in each element as opposed to individual pieces of data? If so, can I access the data in the columns of each result set stored in each element?
I want to use a "Next" and "Back" button and have the actionPerformed() method move forward or backward one row accordingly.
This code should give you an idea of what I'mgetting at:
public static void main(String[] args)throws SQLException, ClassNotFoundException
{
MyDBAccess connect = new MyDBAccess(); //connect using predefined login parameters
Northwind2 objNorthwind = new Northwind2();
Connection c = DriverManager.getConnection(connect.dbUrl,connect.userName,connect.passWord);
Statement sqlQuery = c.createStatement();
ResultSet rs = sqlQuery.executeQuery("SELECT ProductID, ProductName FROM Products WHERE ProductID < 6");

/* The Result Set is an OBJECT. Each individual result set represents one row
returned from the query. As such, the data stored in each column of
each row (result set) is accessible.
To get the data use the methods inherited from the ResultSet object,
then CAST the data into the proper type for later use.
Add each item desired into a vector using a loop.
*/

Vector data = new Vector();//create vector to store result set in

while(rs.next())//while result set has more records move forward one row at a time
{
String pid = rs.getString("ProductID");//returns the data in THIS row & column of the result set
data.addElement(pid);//add to vector, (here named "data") the Product ID from this result set / row
String prodName = rs.getString("ProductName");
data.addElement(prodName);//add to vector the name for product from this result set row
}
objNorthwind.tfProductId.setText((String)data.elementAt(0));//set to textField with the data
objNorthwind.tfProduct.setText((String)data.elementAt(1));// " " " "
objNorthwind.setDefaultCloseOperation(objNorthwind.EXIT_ON_CLOSE);
System.out.println("URL is: " + connect.getdbUrl());//get the url
simpleQuery();//call this static method, defined in class Northwind2
}//eom

------------------
2b | | !2b


PAS<br />peter@panvox.net<br />2b || !2b
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

Peter,
You may want to post questions like this in the JDBC forum in the future where you will have a better chance of getting more answers.
As for your question, I think you should at least create a class to encapsulate each row of the resultset. The class would have some way of returning the key for the row it encapsulates (usually with getPrimaryKey() method). Then, take a look at the Collection framework in java.util.* and find an appropriate class to hold instances of your row class, depending on what you want to do: e.g. do you want to sort your resultset, are you going to do frequent searches, etc.


------------------
Junilu Lacar
Sun Certified Programmer for the Java� 2 Platform


Junilu - [How to Ask Questions] [How to Answer Questions]
Peter Simard
Ranch Hand

Joined: Oct 31, 2001
Posts: 54
Thanks for the help, and I'll heed your advice about future posts.
For my solution, the following code does what I want:
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

/**
* Title:
* Description: Using JDBC
* Copyright: Copyright (c) 2001
* Company:
* @author Peter Simard - peter@panvox.net
* @version 1.0
*/
public class Northwind2 extends JFrame implements ActionListener
{
//String user = "", pw = "";
//String dbUrl = "jdbc dbc:Northwind";
JLabel lblProductId;
JLabel lblProduct;
JTextField tfProductId;
JTextField tfProduct;
JButton btNext;
JButton btPrevious;
JButton btFirst;
JButton btLast;
JButton btQuery;
ResultSet rs;
public Northwind2()
{
super("Northwind Products Database - JDBC test");
Container c = getContentPane();
c.setLayout(new BorderLayout());
JPanel main = new JPanel();
main.setLayout(new GridLayout(2,2));
lblProductId = new JLabel("Product ID");
lblProduct = new JLabel("Product");
tfProductId = new JTextField(10);
tfProduct = new JTextField(10);
btNext = new JButton("Next>>>");
btPrevious = new JButton("<<<Back");>
btFirst = new JButton("First Record");
btLast = new JButton("Last Record");
// btQuery = new JButton("Launch Query");
main.add(lblProductId);
main.add(tfProductId);
main.add(lblProduct);
main.add(tfProduct);
JPanel nav = new JPanel();
nav.setLayout(new FlowLayout());
nav.add(btFirst);
nav.add(btNext);
nav.add(btPrevious);
nav.add(btLast);
c.add(main, BorderLayout.CENTER);
c.add(nav, BorderLayout.SOUTH);

btNext.addActionListener(this);
btPrevious.addActionListener(this);
btFirst.addActionListener(this);
btLast.addActionListener(this);
setSize(525,300);
setVisible(true);
try{
MyDBAccess connect = new MyDBAccess(); //connect using predefined login parameters
Connection conn = DriverManager.getConnection(connect.dbUrl,connect.userName,connect.passWord);
Statement sqlQuery = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = sqlQuery.executeQuery("SELECT ProductID, ProductName FROM Products");
rs.first();
displayCurrentRecord();

}
catch(Exception ex){System.out.println(ex);
}
}//end constructor
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == btFirst)
{
try
{
rs.first();
displayCurrentRecord();
}
catch(Exception ex)
{
System.out.println(ex);
}
}
if(e.getSource() == btNext)
{
try
{
rs.next();
displayCurrentRecord();
}
catch(Exception ex)
{
System.out.println(ex);
}
}
if(e.getSource() == btPrevious)
{
try
{
rs.previous();
displayCurrentRecord();
}
catch(Exception ex)
{
System.out.println(ex);
}
}
if(e.getSource() == btLast)
{
try
{
rs.last();
displayCurrentRecord();
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
public void displayCurrentRecord()
{
try{
tfProductId.setText(rs.getString("ProductID"));
tfProduct.setText(rs.getString("ProductName"));
}
catch(Exception ex)
{System.out.println(ex);}
}

//******************* ***********************
//******************* Main method/driver class ***********************
public static void main(String[] args)throws SQLException, ClassNotFoundException
{
Northwind2 objNorthwind = new Northwind2();
objNorthwind.setDefaultCloseOperation(objNorthwind.EXIT_ON_CLOSE);
System.out.println("URL is: " + connect.getdbUrl());//get the url
simpleQuery();//call this static method, defined in class Northwind2 */

}//eom
}//eoc
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Vectors
 
Similar Threads
Unable to find specific product
BMP -last inserted record alone gets fetched
Out of Memory when testing a BMP Bean
Displaying images dynamically from database
Unable to update database