I need help on this, im not getting how to go about this... ok, let me explain where im stuck,

i have a class, and its structure goes like this(i will show a some kind of replica of original class, so that you guys can easily follow my question),

public class testClass{
	String name = "";
	ArrayList arrList = new ArrayList();
	public viod setName(String string)
	{
		name = string;
	}
	public String getName(String string)
	{
		return name;
	}
}

This class i use as a helper class in one of the EJB(Stateless Session Bean), and i have created a webservice of it.
I have another class(say dbClass) which handles all database queries. I have defined a function(say, dbFunction()) in this class(dbClass) which returns
Array of the above testClass , for eg: testClass[] dbFunction(){...} . The function, dbFunction() , should go like this,
i will write a psuedo code of this,

public testClass[] dbFunction(){
	ArrayList a =new ArrayList();
	PreparedStatement ps = null;
	ResultSet rs = null;
	try{
		ps = conn.prepareStatement("select * from testTable");
		rs = ps.executeQuery();
		if (!rs.next()){
			# i have done this so far
			a.add(new testClass());
			# if there is no records in the result set then, what should i return...?
		}
		else{
			while(rs.next())
			{
				# create an object of testClass, set the properties of it usig setter methods..
				# and add that objet to array list, a
			}
		}
	}catch(SQLException)
	{...}
	testClass[] res = new testClass[ a.size() ];
	a.toArray(res);
	return res;
}

but some how i need function to return some meaning full, saying the array of testClass is empty... but i get exception, if there is no records in the resultset. The exception is my own defined exception(say, testException).
But function works fine when i have record set in the result set.

Please let me know,
- where im doing wrong
- how i should go with this problem ...


thanks in advance.

kath.

Why not simply return the empty array from the function? It's easier to work with than a null or an exception.

A few points:

• Class names should begin with an uppercase character. Following the Java coding conventions is a must considering you are developing enterprise applications using J2EE which itself needs you to follow rigorous coding practices.

• Comments in java begin with // or /* and not # as in Python.

• By returning an array you lose the flexibility provided by collections API and needlessly end up creating a copy of the data in the List, a bad thing. Looks like a case of premature optimization.

But if you still need it, try something like:

public TestClass[] dbFunction()
{
    int size = -1;
    TestClass arr[] = null;
    List aList = new ArrayList<TestClass>();
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try
   {
        ps = conn.prepareStatement("select * from testTable");
        rs = ps.executeQuery();
        while(rs.next())
        {
            aList.add(new TestClass(rs.getString(1)));
        }
        size = aList.size();
        if(size > 0)
        {
            arr = new TestClass[size];
            aList.toArray(arr);
        }
    }
    catch(SQLException e)
    {
        //do some exception handling
    }
    finally
    {
        //close everthing, general cleanup
        return(arr);
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.