the J2EE container DataSource should be used; but it will not be removed for a while AFIAK.
(Deprecated just means it might be removed in some futre version. Just like hopefully bean, logic and html:link get deprecated in 2.0 in favor of JSTL. It's just a nudge from developers (of which I am not one) for you to start considering the better alternatives. I even think Java should deprecate Connection , ResultSet and java.util.Date in favor of DataSource, RowSet and Calendar)
Poolman is very, very nice... I used it before 2001; when I switched to RowSet (before I started using a SQL based DAO). DAO is best thing to do now, and all the DB issues would disapper.
.V
Edgar P Dollin wrote:
Don't spend the time to get DataSource working. It is deprecated and will be removed from struts in 1.2. I use Poolman at sourceforge (I highly reccomend it for non-j2ee projects). Most others use the DataSource supplied with the container.
Edgar
-----Original Message----- From: Caroline Jen [mailto:[EMAIL PROTECTED] Sent: Monday, December 08, 2003 4:29 PM To: Struts Users Mailing List Subject: Re: HELP: about to get datasource of struts and pass to logic beans...
There are lots of classes involved. I will give you an example:
1. my LogonAction calls EditorService.java (business delegate) 2. EditorService.java calls MySQLEditorDAO.java (data access object implements EditorDAO.java, which is a data access interface) 3. the MySQLEditorDAO.java returns EditorBean.java (a Java bean with three properties)
Here is my LogonAction.java:
package org.apache.artimus.logon;
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionError; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionServlet;
import org.apache.artimus.lang.Tokens;
public final class LogonAction extends Action {
public ActionForward execute(ActionMapping
mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws java.lang.Exception {
// Obtain username and password from web tier
String username = ((LogonForm)
form).getUsername();
String password = ((LogonForm)
form).getPassword();
EditorService service = new EditorService();
EditorBean editor = service.findEditorData(
username );
HttpSession session = request.getSession(); session.setAttribute( "editor", editor );
// Log this event, if appropriate
if (servlet.getDebug() >= Tokens.DEBUG) { StringBuffer message = new StringBuffer("LogonAction: User '"); message.append(username); message.append("' logged on in session "); message.append(session.getId()); servlet.log(message.toString()); }
// Return success return (mapping.findForward(Tokens.VALID));
}
} // End LogonAction
Here is the EditorService.java:
package org.apache.artimus.logon;
import org.apache.artimus.logon.dao.*;
public class EditorService {
EditorDAO ed = new MySQLEditorDAO();
public EditorBean findEditorData( String username )
{ return ed.findEditor( username );
}
}
Here is the EditorDAO.java:
package org.apache.artimus.logon.dao;
import org.apache.artimus.logon.EditorBean; import org.apache.artimus.logon.exceptions.EditorDAOSysException;
public interface EditorDAO.java { public EditorBean findEditor( String username ) throws EditorDAOSysException; }
Here is the MySQLEditorDAO.java:
package org.apache.artimus.logon.dao;
import java.io.IOException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.sql.SQLException;
import org.apache.artimus.logon.EditorBean; import org.apache.artimus.logon.exceptions.EditorDAOSysException; import org.apache.artimus.ConnectionPool.DBConnection;
public class MySQLEditorDAO implements EditorDAO
{
// Here the return type is EditorBean
public EditorBean findEditor( String username ) throws EditorDAOSysException {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = DBConnection.getDBConnection();
stmt = conn.createStatement();
String query = "SELECT user_role,
journal_category FROM members WHERE user_name = '" +
username + "'"; rs = stmt.executeQuery( query );
if (rs.next()) {
return new EditorBean( username,
rs.getString( "user_role" ), rs.getString(
"journal_category" ) );
}
else
{
System.out.println( "invalid user name" );
return null; }
} catch (SQLException se)
{
throw new
EditorDAOSysException("SQLException: " +
se.getMessage());
}
finally
{
if ( conn != null )
{
try
{
rs.close();
rs = null;
stmt.close();
stmt = null;
conn.close();
}
catch( SQLException sqlEx )
{
System.out.println( "Problem occurs
while closing " + sqlEx );
}
conn = null;
} }
}
}
Here is the EditorBean.java:
package org.apache.artimus.logon;
import org.apache.artimus.logon.dao.*;
public class EditorBean {
private String username; private String userrole; private String keyValue;
static EditorDAO ed = new MySQLEditorDAO();
public EditorBean() {} public EditorBean( String username, String userrole, String keyValue ) { setUsername( username ); setUserrole( userrole ); setKeyValue( keyValue ); } public String getUsername() { return username; } public void setUsername( String username ) { this.username = username; } public String getUserrole() { return userrole; } public void setUserrole( String userrole ) { this.userrole = userrole; } public String getKeyValue() { return keyValue; } public void setKeyValue( String keyValue ) { this.keyValue = keyValue; } }
Here is the DBConnection.java, which gets a connection object from a connection pool (you have to configure your server's connection pool):
package org.apache.artimus.ConnectionPool;
import java.sql.Connection; import java.sql.SQLException; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource;
public class DBConnection {
public static Connection getDBConnection() throws
SQLException
{
Connection conn = null;
try { InitialContext ctx = new InitialContext(); DataSource ds = ( DataSource ) ctx.lookup( "java:comp/env/jdbc/MySQLDB" );
try {
conn = ds.getConnection();
}
catch( SQLException e )
{ System.out.println( "Open connection
failure: " + e.getMessage() );
}
}
catch( NamingException nEx )
{
nEx.printStackTrace();
}
return conn;
}
}
Here is the exception handling class:
package org.apache.artimus.logon.exceptions;
/** * EditorDAOSysException is an exception that extends the standard * RunTimeException Exception. This is thrown by the DAOs of the article * component when there is some irrecoverable error (like SQLException) */
public class EditorDAOSysException extends RuntimeException {
/**
* Constructor
* @param str a string that explains what the
exception condition is
*/
public EditorDAOSysException (String str) {
super(str);
}
/** * Default constructor. Takes no arguments */ public EditorDAOSysException () { super(); } }
-Caroline --- Ricky <[EMAIL PROTECTED]> wrote:
hi, there,
with my project i had several queries about get
datasource in struts. i know how to get datasource
in struts, just as in myAction, use getDataSource
method and return DataSource object, and go on to
get connection ....
i also know the logic beans between the Controller and Module, so i create a logic bean ,i don't know how to get datasourc of pass the datasource, in some tutorial , i got a way to solve this, create a method getDAO(HttpServletRequest request) and pass the request to the method to get DataSource, i was lost here, i dont' know what should i return in getDAO, should i return a DataSource object or others and in my logic bean i can use the datasource and also get connection , statement and resultset object , there i have to close it after i finish my process ....what do you think of this i did ? in general, when i got a DataSource, i will got a connection from datasource and a PrepareStatement and also a ResultSet , when i finished my process, i can close all object i opened before in final method of try ...catch...experession.. now what should i do to return a proper object in getDAO method and in logic beans i can use the object to process logic business...
i appologized for this ... i hope you can give me some suggestion or flow...... thanx!
now here is my flow of struts framework to pass the datasource to the logic beans... i hope you can reword if you have some good idea...of correct my mistake... :)
myAction :
myAction extends Action() {
public ActionForward execute(ActionMapping
mapping,
ActionForm form,
HttpServletRequest
request,
HttpServletResponse
response) {
LogicBean bean = new LogicBean();
Object objcet =
bean.doMethod(getDAO(request), other param)
}
public DataSource getDAO(HttpServletRequest
request) {
DataSource ds = null;
ds = getDataSource(request);
return ds;
}
}
and in my logic bean :
class LogicBean { public Objct doMethod(DataSource ds, other param) { try { Connection conn = ds.getConnection(); PrepareStatment stmt = conn.prepareStatment("SELECT ......."); stmt.setInt(1,....); ResultSet rs = stmt.executeQuery(); .... return Object.....; } catch (SQLException ex) { ex.printStactTrace(); } final { try { rs.close(); stmt.close(); conn.close() } catch (Exception ex) { ex.printStactTrace(); } } return null; } }
should i pass the DataSource to my logic beans? otherwise what object should i pass....
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
__________________________________ Do you Yahoo!? New Yahoo! Photos - easier uploading and sharing. http://photos.yahoo.com/
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]