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