nono, not at all. You understood this wrong.
I'm not using EJB's myself, but with EJB's there's a very detailed outline
on how you're supposed to create them and what methods are needed.
If you use simple classes/beans to do the db work, there isn't any guide.
So everyone has to re-invent what's the best way to code these. And I've
seen quite some bad solutions.
I used mainly 2 approaches.
* the first one is a generic wrapper class around a SQL statement. The
details (select statement, column list, data types, whether it's updateable
or not, connection name...) are stored in a configuration file.
This allows me to do easy reporting and direct updating/inserting in the
database. The data are cached in memory so that I can close the connection
after retrieving the data.
This is more or less like a CachedRowSet.
* On other occasions I create beans that map the specific business objects.
But especially here it's not clear how we 're supposed to do this.
I mostly create:
- a constructor without params (required for beans) and that allows me to
insert new objects in the db.
- a constructor that takes the primary key
- sometimes some other lookup constructors, like with a name.
- a load method
- a save method (that inserts or saves depending on whether the load
function was already called)
- sometimes a delete method (but I don't delete often from a database)
- get and set methods for the main data fields
- a generic get and set method for other data fields like
String getValue(String field) and
void setValue(String field, String value)
- Static lookup methods that return an instance, an array of instances or an
array of keys.
I would like to put this way of working in some sort of light framework with
interfaces, utility classes and parts of the code I keep rewriting in all my
classes.
I really need to do some good refactoring here.
Are there any suggestions? remarks on my outline above, comments,
alternatives....
Geert 'Darling' Van Damme
> -----Original Message-----
> From: A mailing list about Java Server Pages specification and reference
> [mailto:[EMAIL PROTECTED]]On Behalf Of Kishore Raghavan
> Sent: woensdag 28 juni 2000 20:51
> To: [EMAIL PROTECTED]
> Subject: Re: JSP/JDBC-bean - Geert
>
>
> Geert,
>
> I did not understand what you meant by this...
>
> [Geert Wrtoe...
> > As you see, there are quite some problems left ;-)
> > but the most important point is: How are we suppose to program
> things like
> > this?
> > That is, if you're not using EJB's, but simple beans to store
> the database
> > information.
> ...]
>
> Did you mean, we cannot write database applications without using EJBs ?
>
> Why do you feel EJBs are more scaleable ?
>
> Please clarify...
>
> Thanks
> Kishore Raghavan
>
> -----Original Message-----
> From: A mailing list about Java Server Pages specification and reference
> [mailto:[EMAIL PROTECTED]]On Behalf Of Pedro Meza
> Sent: Tuesday, June 27, 2000 7:22 AM
> To: [EMAIL PROTECTED]
> Subject: JSP/JDBC-bean
>
>
> Geert,
>
> I believe that you made some great points. I am just beginning to learn
> JSP -
> JDBC/JavaBeans. You are right in stating that I did ont close
> the resultset
> and
> statement. I see those errors and others. I have not found very much on
> the
> internet on JDBC/JavaBean coding so I tried something and appreciate the
> feedback.
>
> The questions I have to your comments are:
>
> How does one do connection pooling and caching ???
>
> When displaying the results, why would vectors be useful ??? I
> can sort in
> my
> query and do not need to reference the data but only to display it.
>
> I am not using EJB because my company does not support them ??? What are
> there
> advantages ??? I understand entity beans are too slow to use and resource
> intensive.
>
> Thanks,
>
> Pedro
>
>
> Geert Van Damme wrote:
>
> > Some remarks:
> >
> > Your class might work well for you, but to be used as a kind of
> reference
> > for other users, I see quite a few issues:
> > - Your object is very limited in that you always retrieve all
> records. You
> > don't provide any search capabilities or anything.
> > - You don't provide insert/update capabilities
> > - You're not using connection pooling and no caching.
> > - You have duplicated code: 1st to get the first record and then to get
> the
> > other ones. Are you sure this is working???? I think you need to call
> > rs.next once in order to fetch the first row.
> > - You only keep the results of the last row in you instance
> variables. Why
> > aren't you using arrays or vectors?
> > - You keep the Resultset as an instance variable in your bean????
> > - you're not closing the resultset.
> > - you're not closing the statement.
> > - worse of all: you're not closing the connection.
> > - You're catching the SQLExceoption in 2 places, and you're not looking
> for
> > nextException.
> > - you're not closing the connection in case of exception
> > - you're setting autocommit to false, but you never do a commit or a
> > rollback.
> >
> > As you see, there are quite some problems left ;-)
> > but the most important point is: How are we suppose to program
> things like
> > this?
> > That is, if you're not using EJB's, but simple beans to store
> the database
> > information.
> > I have a lot of things to tell about this, too much to put in
> this single
> > email.
> > Let's discuss this in some more detail.
> >
> > Geert 'Darling' Van Damme
> >
> > > -----Original Message-----
> > > From: A mailing list about Java Server Pages specification
> and reference
> > > [mailto:[EMAIL PROTECTED]]On Behalf Of Pedro Meza
> > > Sent: woensdag 21 juni 2000 21:16
> > > To: [EMAIL PROTECTED]
> > > Subject: JSP - JDBC/Javabean solution
> > >
> > >
> > > Ok folks.... I just created a solution that queries an Access
> > > Database. It is a
> > > JSP file that uses Javabean methods to query the database and get
> > > values. I would
> > > like feedback on improving the design or it could be used as a
> > > reference for an
> > > aspiring JSP user.
> > >
> > > Note: I used the jdbc-odbc bridge. If one is planning on using
> > > JDBC it is best to
> > > use another driver.
> > >
> > > The table is a Document Type Table with a TYPE_ID, DOC_TYPE,
> > > DOC_DESCRIPTION.
> > >
> > > Pedro
> > >
> > > =========================== JSP FILE ==============================
> > > <html>
> > > <body bgcolor="white">
> > > <jsp:useBean id="foo" scope="session" class="checkbox.Doc_Types" />
> > > <% foo.getRecords(); %>
> > > <hr><center><font face="verdana"><h2>Doc Type Table</h2>
> > > <form action="update_Doc_Type.jsp">
> > > <TABLE border=1 cellpadding=3 cellspacing=2>
> > > <TR><TD bgcolor="white"><B><font face="verdana"
> color="red">Delete</TD><TD
> > > bgcolor="navy"><font face="verdana" color="white">Type ID</TD><TD
> > > bgcolor="navy"><font face="verdana" color="white">DOC TYPE</TD><TD
> > > bgcolor="navy"><font face="verdana" color="white">DOC
> > > DESCRIPTION</TD></TR>
> > > <% while (foo.nextRecord() == true) { %>
> > > <TR><input type="hidden" name="Doc_Type_ID"
> > > value="<%=foo.getType_ID()%>">
> > > <td><center><input type="checkbox"
> name="delete_<%=foo.getType_ID()%>"
> > > value="Y"></td>
> > > <TD><font face="verdana"><%= foo.getType_ID() %></TD>
> > > <TD><font face="verdana"><%= foo.getDoc_Type() %></TD>
> > > <TD><font face="verdana"><%= foo.getDoc_Desc() %></TD>
> > > <%}%>
> > > </TR>
> > > </TABLE><BR><BR><BR>
> > > <INPUT TYPE="Submit" VALUE="SUBMIT"> <INPUT TYPE=RESET VALUE="CLEAR">
> > >
> > > </form>
> > > </font>
> > > </body>
> > > </html>
> > >
> > > =========================== JavaBean File
> > > =================================
> > > package checkbox;
> > >
> > > import java.io.*;
> > > import java.sql.*;
> > > import sun.jdbc.odbc.*;
> > >
> > >
> > > public class Doc_Types {
> > > int type_id;
> > > String doc_type;
> > > String doc_desc;
> > > ResultSet rs;
> > >
> > > // Get Database Records
> > > public void getRecords() {
> > > String url = "jdbc:odbc:pit";
> > > try {
> > > Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
> > > Driver driver = new sun.jdbc.odbc.JdbcOdbcDriver();
> > > } catch(Exception ee) {
> > > System.out.println(ee.getMessage());
> > > }
> > > try {
> > > java.sql.Connection con =
> > > DriverManager.getConnection(url,"admin", "pit");
> > > con.setAutoCommit(false);
> > > java.sql.Statement stmt = con.createStatement();
> > > String query;
> > > query = "SELECT * from DOC_TYPES";
> > > rs = stmt.executeQuery(query);
> > > type_id = rs.getInt("TYPE_ID");
> > > doc_type= rs.getString("DOC_TYPE");
> > > doc_desc= rs.getString("TYPE_DESCRIPTION");
> > > } catch (SQLException e) {
> > > System.err.println("SQLException: " + e.getMessage());
> > > }
> > > }
> > > // Get the next Database record. Returns true or false if there are
> more
> > > public boolean nextRecord() {
> > > boolean check;
> > > try {
> > > check=rs.next();
> > > type_id = rs.getInt("TYPE_ID");
> > > doc_type= rs.getString("DOC_TYPE");
> > > doc_desc= rs.getString("TYPE_DESCRIPTION");
> > > return check;
> > > }
> > > catch (SQLException e) {
> > > System.err.println("SQLException: " + e.getMessage());
> > > }
> > > return false;
> > > }
> > >
> > > public int getType_ID() {
> > > return type_id;
> > > }
> > >
> > > public String getDoc_Type() {
> > > return doc_type;
> > > }
> > >
> > > public String getDoc_Desc() {
> > > return doc_desc;
> > > }
> > >
> > >
> > > }
>
> ==================================================================
> =========
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
> JSP-INTEREST".
> Some relevant FAQs on JSP/Servlets can be found at:
>
> http://java.sun.com/products/jsp/faq.html
> http://www.esperanto.org.nz/jsp/jspfaq.html
> http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
> http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
>
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets