This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Servlets and the fly likes SELECT statements --- Vectors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "SELECT statements --- Vectors" Watch "SELECT statements --- Vectors" New topic
Author

SELECT statements --- Vectors

Naveen R R
Greenhorn

Joined: May 04, 2001
Posts: 20
I am trying to create a SelectComponent class for selecting data from Maintenance Tables. This class will be used for all the tables. I am trying to use vector objects. But, I am not sure how to do this. The constructor in the code below should take sql statements of the form: Example: "SELECT * FROM TABLENAME WHERE FIELDNAME= ? and FIELDNAME= ?". And, the following code has a few errors too.
Code:
import java.io.*;
import java.sql.*;
import java.util.*;
public class SelectComponent {
private int rowCount;
private int columnCount;
private int currentRow;
private Statement stmt;
private ResultSet rs;
}
/**
Insert the method's description here.
Creation date: (5/2/01 10:24:34 AM)
@param c java.sql.Connection
@param sql java.lang.String
@param input java.lang.String
/
//String input is what the users enter on the screen
public SelectComponent(Connection c, String sql, String input) {
try {
java.util.Vector v = new java.util.Vector();
PreparedStatement p = c.prepareCall(sql);
v.addElement();
for (int i=0; i < v.size(); i++) {
Object o = v.elementAt(i);
if (o instanceof integer) {
p.setInt();
}
else if (o instanceof String) {
p.setString();
}
else if (o instanceof Double) {
p.setDouble();
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
If I am working with a particular table and its values then I will type in something like this:
"SELECT * FROM ISPROJ WHERE Proj = ? and dept = ? and Cost_Center = ?";
java.util.Vector v = new java.util.Vector();
v.addElement (new Integer (Proj));
v.addElement (dept);
v.addElement (new Double (Cost_Center);
and so on...
But, how do I do it for multiple tables and values???
I will greatly appreciate any help.
Thanks,
Naveen
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
A couple of comments, none of which directly answer your question. Sorry, I'm probably in soapbox mode tonight. Hope you still find it helpful.
To start with a pet peeve, one of these days you may want to familiarise yourself with the ArrayList and LinkedList classes. I don't understand the fascination with Vectors. They sit uneasily in the Collections framework. They are synchronized, which is just a waste of good CPU cycles in 90% of the cases, and the wrong type of synchronization in another 9,9% of the cases.
More to the point, I'm not sure what you mean with "how do I do it for multiple tables and values". Multiple tables aren't really different from a single table: you just join them up. You already seem to be able to handle multiple values, unless you mean return values. You could just return a List (Vector, if you will) of Object[] arrays (check out ResultSet.getMetaData()), but by all means have a look at javax.sql.CachedRowSet as well. Returning the ResultSet directly is asking for resource leaks: there should be a finally clause somewhere which closes your Statement, and you probably don't want to leave that responsibility to the client code using your class. Really, it pays to be anal retentive about cleaning up resources. The garbage collector won't always do that for you, and you don't want to leak resources with every exception thrown.
Generally, what exactly are you trying to achieve with your class? What are its responsibilities? The best, most productive, and bug-free development you will ever do is the development that you avoid doing. Are you sure you're not just trying to reinvent something that has already been done elsewhere?
- Peter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SELECT statements --- Vectors