Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Constructor(recordset record) 1

Status
Not open for further replies.

lespaul

Programmer
Feb 4, 2002
7,083
0
0
US
Is it possible to pass my class constructor a record from a recordset object? So inside my class I have variables for all the information coming from the database and I just want to do something like this:
Code:
package model;

import java.util.ArrayList;
import java.util.List;
import java.sql.*;

public class Statute {
	String statuteNumber;
	String chargeAbbrev;
	boolean active;
	String shortDescrip;
	String extendedDescrip;
	String ncic_id;
	String effectiveDate;
	boolean mandatoryCourt;
	int fineAmount;
	int bondAmount;
	String chargeType;
	String chargeCategory;
	String crimHistory;
	String origin;
	String crStatute;
	String user;
	String lastUpdate;
	String chargeSubCategory;
	String Prefix;
  
[b]	Statute(recordObject From Database){
	/*assign values from database records to class variables*/
	}[/b]
  public List getStatutes(String status, String origin){
	/*List<Statute> statutes = new ArrayList<Statute>();*/
     String library = "CMLIB";
     String statusClause = null;
     String originClause = null;
	  
     if (status.equals("Inactive")){
	statusClause = "STASTS='I'";
     }
     else if (status.equals("Active")){
	  statusClause = "STASTS<>'I'";
     }
	  
    if (origin.equals("City")){
   	  originClause = "ORIGIN = 'C'";
    }
    else if (origin.equals("County")){
	  originClause = "ORIGIN = 'N'";
    }
    else if (origin.equals("State")){
	  originClause = "ORIGIN = 'S'";
    }
    else if (origin.equals("Local")){
	  originClause = "ORIGIN = 'L'";
    }
		
    List statutes = new ArrayList();
    try
	{
    jdbcAccess datasource = new jdbcAccess();
	datasource.connect();
	Connection conn = datasource.getConnection();
	Statement sqlstmt =   conn.createStatement();    
	String stmt = "SELECT * FROM " + library +  "CMPSTAMFE E INNER JOIN " + library + 
"CMPSTAMF M ON E.STATUT = M.STATUT AND E.CHGABV = M.CHGABV";
/*build WHERE clause based on values determined above*/
ResultSet rsStatutes = sqlstmt.executeQuery(stmt);

while(rsStatutes.next()){			
[b]statutes.add(new Statute(rsStatutes.record));[/b]
}
	    
	    
}
catch (SQLException sql)
{
System.out.println("statute Access - Sql Exception: " + sql.getMessage());
}
return(statutes);
}  
}
Thanks for any help!

Leslie

Have you met Hardy Heron?
 
I HAVE been trying! I haven't been able to find the "record" object...There's a result set object but I can't seem to find how to pass the RECORD i'm on into the constructor...I haven't been able to figure out what type I should put in the constructor parameter.

So thanks for helping me assume that what I'd like to do is possible, perhaps someone can help me figure out how to do it.

Leslie
 
Maybe I'm wrong, but I don't think there's a "record" object. I think you will need to retrieve the individual values and pass them to the constructor.

Cheers,
Dian
 
you will need to retrieve the individual values and pass them to the constructor
that's what i'm trying to avoid!

after some of the reading I've done this morning I think i need to use something like iBATIS or Hibernate...

Thanks!

Leslie

Have you met Hardy Heron?
 
It seems to me that you are trying to pass all records from the resultset one by one, to the constructor. Why not pass the entire resultset and then treat each record in the class.
You can also put the row of the resultset in an array and pass the array to the class constructor. I don't think that there is a way of passing a specific record.
 
I'm trying to create a class that contains a public function that returns an ArrayList of that class to a servlet to process a jsp page. I'm trying to keep the class and processes generic enough that I can use it in other places, that's the whole point of OOP, right? This is my first attempt at servlets and jsp (I'm reading the Head First book) and I'm applying the principles to a simple web site. I have a little bit of Java, and just had a eureka moment with OOP a few weeks ago.

I have my main page where the user has to select the status and origin of the statutes they want to see. I then have a results page that at the moment I can get basic information for each statute listed, but eventually I'd like to make a link on that page to a detailed page of the statute information.

So, when I use List, I get warning messages that I should be using ArrayList<type>. So, I'm trying to create an ArrayList<Statute>, the ArrayList needs to include a Statute object for each record returned from the database query so I can return the ArrayList to my servlet and it can write the result page and hopefully use the Statute objects in the ArrayList to populate the "detail" page for that statute when the user selects the link.

So, does this all sound reasonable? Is this how I should do it?
Why not pass the entire resultset and then treat each record in the class.
how would you do that?

Thanks,
Leslie
 
Yes... you're right. I didn't get the problem right at first.

In this case i think that you'll have to get each collumn value and send them separetly or put the values in an array and send the array. This way you'll still be sending out just one parameter.

I really don't see any other choice.
If the rest of the class is generic, it may be usefull having two contructor methods, each one with different input parameters. This way, you can still take advantage of this class for other purposes.
 
so you're saying do something like this:
Code:
Statute(Array constInfo){ 
      statuteNumber = Array[0];
      chargeAbbrev = Array[1];
      active = Array[2];
    }


while(rsStatutes.next()){            
  create an array of Statute information;
  statutes.add(new Statute(array));
}

Leslie

Have you met Hardy Heron?
 
Yup. Looks all right to me.
Let me know if it works.

Good luck.
 
One more thing...
I'm sorry i didn't noticed this sooner.
As you probably know, arrays can only assume one type of data. If create a string arrays, you'll only be able to store string values. The attribute active, however is a boolean data type. Because of this, you'll be needing to parse the value in the array (let us supose it's a string) to a boolean value.
If you're getting this from a database, boolean values will probably assume the form of "True" or "False", so it should be easy to parse the values.

if (active[2].equalsIgnoreCase("true")) active=true;
else active=false;

Check if the values returned from the database are really in this form. If not, tou'll have to adapt the code to your needs.
 
Better yet.
If you just do this:
active=Boolean.parseBoolean(active[2]);

the you won't be needing to the if statement. This parser will automatically compare the string in the active[2] with the string "true". This comparison ignores the string case, and if active[2] equals "true", then it will return a boolean value of "true".
 
thanks for the pointers!

I'm sure that the database type is CHAR with a I for inactive and a blank for active...it's an old, non-normalized legacy database. it SHOULD be a boolean type, which is why I made the class this way, so that in the future another developer can just use the class and not have to worry about the sorry state of the database!

I appreciate your thoughts and guidance!

leslie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top