Hi

Give me one good reason as to why you are doing this. If you let existing
tools do the job for you, you are going to relieve your self of a lot of
trouble. Have a look at Hibernate or equivalent, combined with Spring.

What you are doing here is really is something of the past.

Hermod


-----Opprinnelig melding-----
Fra: James Reynolds [mailto:[EMAIL PROTECTED] 
Sendt: 29. mars 2006 19:33
Til: Struts Users Mailing List
Emne: [Shale] When to create a database Connection?


I'm getting a null pointer error in my backing bean.  My grand plan was
to create a Connection in the init() method extended from the
AbstractViewController, so it can be used by any number of methods and
getters in my bean, and then close it in destroy().

In this simple case, the only place this Connection is used is in a
getter that returns a Result for a dataTable.  However, it doesn't seem
to be working as I get a null pointer exception.  If I move the
getConnection() method out of the init() method and into this getter,
then everything works great.

Am I miss-using the init() method?


Import lots.of.stuff;

public class Testimonial extends AbstractViewController {
    private Connection conn = null;

    public Testimonial(){
    }

    public void init() {
        try {
            conn = DbConnection.getConnection();
            System.out.println();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void destroy() {
        try {
            if (!conn.isClosed()) {
                conn.close();
            }
        } catch (SQLException sqle) {
            log(sqle.getMessage(), sqle);
            sqle.printStackTrace();
        }
    }

    public Result getAllTestimonials() {
        /* ### moving the "conn = DbConnection.getConnection()" here
solves the problem ### */
        Result result = null;
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(SqlStatements.ALL_TESITIMONIALS);
            result = ResultSupport.toResult(ps.executeQuery());
        } catch (SQLException sqle) {
            System.out.println("SQL Exception:" + sqle.getMessage());
            sqle.printStackTrace();
        } catch (Exception e) {
            System.out.println("Exception:" + e.getMessage());
            e.printStackTrace();
        } finally {
            try {
                if (ps != null) {
                    ps.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to