You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Chris Pratt <th...@gmail.com> on 2010/01/07 19:18:27 UTC
Re: Struts, Tomcat, JNDI, JDBC without use of an ORM Library such as
Hibernate
We use Spring (and Spring-LDAP) for that layer. It works great, manages all
our DAO's and their resources, and then injects them into the Actions as
needed.
(*Chris*)
On Thu, Jan 7, 2010 at 10:06 AM, Jasvinder S. Bahra <
bbdl21548@blueyonder.co.uk> wrote:
> Does anyone know of any techniques that would allow a Database Access
> Object (DAO) to be used from the various actions that make up a Struts
> application?
>
> I have a DAO which, when instantiated, acquires a data source from the
> servlet container (in this case, Apache Tomcat). Thereafter, I can execute
> the various methods provided by the object which use the data source to
> execute SQL queries/statements, and return usable data in the form of
> Strings, Maps, Vectors etc, as appropriate.
>
> Instantiating the DAO in the execute() method of each of my action's
> however, seems a little inefficient. Does Struts provide any way to
> instantiate the object once and then make it available for the lifetime of
> the container (in a way that my Actions can access it)?
>
> Note please that i'm well aware that this is a somewhat non-standard
> approach to database interaction. I am intending to investigate Hibernate
> (and like) at some point, but for the time being I want to avoid such.
>
> This is the skeleton of the DAO I have in mind...
>
> ----------------------------------------------------------------------
> import java.sql.*;
> import javax.sql.*;
> import javax.naming.*;
> import javax.servlet.*;
>
> public class MyDataAccessObject {
>
> private DataSource dataSource;
> private String SQL;
>
> public MyDataAccessObject() throws Exception {
>
> try {
> Context init = new InitialContext();
> Context ctx = (Context) init.lookup("java:comp/env");
> dataSource = (DataSource) ctx.lookup("jdbc/mysqldb");
> } catch (NamingException ne) {
> throw new Exception("Cannot retrieve java:comp/env/jdbc/mysqldb",
> ne);
> }
>
> SQL = null;
> }
>
> public String getValue(String name) {
>
> Connection dbcon = null;
> PreparedStatement pstmt = null;
> ResultSet rs = null;
>
> String value = null;
>
> try {
> dbcon = dataSource.getConnection();
>
> SQL = "SELECT value FROM table WHERE name = ?";
>
> pstmt = dbcon.prepareStatement(SQL);
> pstmt.setString(1, name);
>
> rs = pstmt.executeQuery();
> while (rs.next()) {
> value = rs.getString("value");
> }
>
> rs.close();
> rs = null;
>
> pstmt.close();
> pstmt = null;
>
> dbcon.close();
> dbcon = null;
> } catch (SQLException ex) {
> log(ex);
> } catch (Exception ex) {
> log(ex);
> } finally {
> if (rs != null) {
> try { rs.close(); } catch (Exception fe1) { log(fe1); }
> rs = null;
> }
>
> if (pstmt != null) {
> try { pstmt.close(); } catch (Exception fe2) { log(fe2); }
> pstmt = null;
> }
>
> if (dbcon != null) {
> try { dbcon.close(); } catch (Exception fe3) { log(fe3); }
> dbcon = null;
> }
> }
>
> SQL = null;
> return value;
> }
>
> private void log(...) {
> // Handle logging
> }
> }
> ----------------------------------------------------------------------
>
> Thanks,
>
> Jazz
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
Re: Struts, Tomcat, JNDI, JDBC without use of an ORM Library such as Hibernate
Posted by "Jasvinder S. Bahra" <bb...@blueyonder.co.uk>.
> Jazz, here's a lightweight package that so far has met all of my needs:
> http://butterfly.jenkov.com/ ...
I'll be sure to take a look.
Thanks for the response.
Jazz
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Struts, Tomcat, JNDI, JDBC without use of an ORM Library such as
Hibernate
Posted by Bill Bohnenberger <bi...@gmail.com>.
Jazz, here's a lightweight package that so far has met all of my needs:
http://butterfly.jenkov.com/ ...
Cheers, Bill*
*
On Thu, Jan 7, 2010 at 4:30 PM, Jasvinder S. Bahra <
bbdl21548@blueyonder.co.uk> wrote:
> I'll echo the Spring suggestion. It simplifies Action-level programming by
>> quite a bit.
>>
>> On the flip side, it requires some extra XML configuration, but IMO it's a
>> good tradeoff.
>>
>
> Spring seems to be quite popular here. I'm somewhat reluctant because
> Spring provides a lot more than just Database Injection - and for the time
> being, DI is all I really want.
>
> If I cant find what I want, I guess i'll have to bite the bullet and jump
> into it.
>
> Thanks for the response.
>
>
> Jazz
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
Re: Struts, Tomcat, JNDI, JDBC without use of an ORM Library such as Hibernate
Posted by "Jasvinder S. Bahra" <bb...@blueyonder.co.uk>.
> I'll echo the Spring suggestion. It simplifies Action-level programming
> by
> quite a bit.
>
> On the flip side, it requires some extra XML configuration, but IMO it's a
> good tradeoff.
Spring seems to be quite popular here. I'm somewhat reluctant because
Spring provides a lot more than just Database Injection - and for the time
being, DI is all I really want.
If I cant find what I want, I guess i'll have to bite the bullet and jump
into it.
Thanks for the response.
Jazz
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Struts, Tomcat, JNDI, JDBC without use of an ORM Library such as
Hibernate
Posted by Brian Thompson <el...@gmail.com>.
I'll echo the Spring suggestion. It simplifies Action-level programming by
quite a bit.
On the flip side, it requires some extra XML configuration, but IMO it's a
good tradeoff.
-Brian
On Thu, Jan 7, 2010 at 12:18 PM, Chris Pratt <th...@gmail.com>wrote:
> We use Spring (and Spring-LDAP) for that layer. It works great, manages
> all
> our DAO's and their resources, and then injects them into the Actions as
> needed.
> (*Chris*)
>
> On Thu, Jan 7, 2010 at 10:06 AM, Jasvinder S. Bahra <
> bbdl21548@blueyonder.co.uk> wrote:
>
> > Does anyone know of any techniques that would allow a Database Access
> > Object (DAO) to be used from the various actions that make up a Struts
> > application?
> >
> > I have a DAO which, when instantiated, acquires a data source from the
> > servlet container (in this case, Apache Tomcat). Thereafter, I can
> execute
> > the various methods provided by the object which use the data source to
> > execute SQL queries/statements, and return usable data in the form of
> > Strings, Maps, Vectors etc, as appropriate.
> >
> > Instantiating the DAO in the execute() method of each of my action's
> > however, seems a little inefficient. Does Struts provide any way to
> > instantiate the object once and then make it available for the lifetime
> of
> > the container (in a way that my Actions can access it)?
> >
> > Note please that i'm well aware that this is a somewhat non-standard
> > approach to database interaction. I am intending to investigate
> Hibernate
> > (and like) at some point, but for the time being I want to avoid such.
> >
> > This is the skeleton of the DAO I have in mind...
> >
> > ----------------------------------------------------------------------
> > import java.sql.*;
> > import javax.sql.*;
> > import javax.naming.*;
> > import javax.servlet.*;
> >
> > public class MyDataAccessObject {
> >
> > private DataSource dataSource;
> > private String SQL;
> >
> > public MyDataAccessObject() throws Exception {
> >
> > try {
> > Context init = new InitialContext();
> > Context ctx = (Context) init.lookup("java:comp/env");
> > dataSource = (DataSource) ctx.lookup("jdbc/mysqldb");
> > } catch (NamingException ne) {
> > throw new Exception("Cannot retrieve
> java:comp/env/jdbc/mysqldb",
> > ne);
> > }
> >
> > SQL = null;
> > }
> >
> > public String getValue(String name) {
> >
> > Connection dbcon = null;
> > PreparedStatement pstmt = null;
> > ResultSet rs = null;
> >
> > String value = null;
> >
> > try {
> > dbcon = dataSource.getConnection();
> >
> > SQL = "SELECT value FROM table WHERE name = ?";
> >
> > pstmt = dbcon.prepareStatement(SQL);
> > pstmt.setString(1, name);
> >
> > rs = pstmt.executeQuery();
> > while (rs.next()) {
> > value = rs.getString("value");
> > }
> >
> > rs.close();
> > rs = null;
> >
> > pstmt.close();
> > pstmt = null;
> >
> > dbcon.close();
> > dbcon = null;
> > } catch (SQLException ex) {
> > log(ex);
> > } catch (Exception ex) {
> > log(ex);
> > } finally {
> > if (rs != null) {
> > try { rs.close(); } catch (Exception fe1) { log(fe1); }
> > rs = null;
> > }
> >
> > if (pstmt != null) {
> > try { pstmt.close(); } catch (Exception fe2) { log(fe2); }
> > pstmt = null;
> > }
> >
> > if (dbcon != null) {
> > try { dbcon.close(); } catch (Exception fe3) { log(fe3); }
> > dbcon = null;
> > }
> > }
> >
> > SQL = null;
> > return value;
> > }
> >
> > private void log(...) {
> > // Handle logging
> > }
> > }
> > ----------------------------------------------------------------------
> >
> > Thanks,
> >
> > Jazz
> >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
>
Re: Struts, Tomcat, JNDI, JDBC without use of an ORM Library such as
Hibernate
Posted by Chris Pratt <th...@gmail.com>.
Good luck, and let us know how it goes.
(*Chris*)
On Thu, Jan 7, 2010 at 3:44 PM, Jasvinder S. Bahra <
bbdl21548@blueyonder.co.uk> wrote:
> We use Spring (and Spring-LDAP) for that layer. It works great, manages
>> all
>> our DAO's and their resources, and then injects them into the Actions as
>> needed.
>>
>
> Chris,
>
> At the moment, i'm trying to reaquaint myself with the ins and out of
> Struts (I haven't touched it for several years), so I dont (yet) want to
> introduce any additional libraries.
>
> I'm sure its possible to do what I need without them.
>
> Thanks for the suggestion anyway.
>
>
> Jazz
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
Re: Struts, Tomcat, JNDI, JDBC without use of an ORM Library such as Hibernate
Posted by "Jasvinder S. Bahra" <bb...@blueyonder.co.uk>.
> We use Spring (and Spring-LDAP) for that layer. It works great, manages
> all
> our DAO's and their resources, and then injects them into the Actions as
> needed.
Chris,
At the moment, i'm trying to reaquaint myself with the ins and out of Struts
(I haven't touched it for several years), so I dont (yet) want to introduce
any additional libraries.
I'm sure its possible to do what I need without them.
Thanks for the suggestion anyway.
Jazz
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org