• Post Reply Bookmark Topic Watch Topic
  • New Topic

SELECT statements --- Vectors  RSS feed

Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
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);
for (int i=0; i < v.size(); i++) {
Object o = v.elementAt(i);
if (o instanceof integer) {
else if (o instanceof String) {
else if (o instanceof Double) {
catch (Exception e) {
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.
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!