aspose file tools*
The moose likes Struts and the fly likes Database Access problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Database Access problem " Watch "Database Access problem " New topic
Author

Database Access problem

Rahil Hurzuk
Greenhorn

Joined: Dec 28, 2004
Posts: 7
Hi All

I am trying out struts example from the Struts Alive available for free download at TheServerSide.com

I am facing some problem accessing the database. I am using Tomcat5.0 & also have the commons-dbcp & commons-pool jar in the appropriate classpath.
I am using the HSQLDB as given in the book.


here is the db code for your perusal. Could somebody point where I am going wrong.


struts-config.xml
-----------------

<data-sources>
<data-source
type="org.apache.commons.dbcp.BasicDataSource"
key="userDB">
<set-property property="driverClassName"
value="org.hsqldb.jdbcDriver" />
<set-property property="url"
value="jdbc:hsqldb:c:/strutsTutorial/db" />
<set-property property="username" value="sa" />
<set-property property="password" value="" />
</data-source>
</data-sources>


build.xml
---------
<target name="builddb"
description="builds the database tables">
<sql driver="org.hsqldb.jdbcDriver"
userid="sa"
password=""
url="jdbc:hsqldb:c:/strutsTutorial/db">
<classpath>
<pathelement
path="C:/tools/hsqldb/lib/hsqldb.jar"/>
</classpath>
CREATE TABLE USER(
EMAIL VARCHAR(80),
FIRST_NAME VARCHAR(80),
LAST_NAME VARCHAR(80),
PASSWORD VARCHAR(11),
PHONE VARCHAR(11),
FAX VARCHAR(11),
CONSTRAINT USER_PK PRIMARY KEY
(EMAIL)
);
</sql>
</target>


Action class
-------------
import java.sql.Connection;
import javax.sql.DataSource;
import java.sql.PreparedStatement;


public class UserRegistrationAction extends Action {

static Category cat = Category.getInstance( UserRegistrationAction.class.getName());


public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)
throws Exception {

DataSource dataSource =getDataSource(request);
Connection conn = dataSource.getConnection();
UserRegistrationForm userForm =(UserRegistrationForm) form;

----------
some more code
----------


try{
PreparedStatement statement = conn.prepareStatement(
"insert into USER " +
"(EMAIL, FIRST_NAME, LAST_NAME, PASSWORD, PHONE, FAX)" +
" values (?,?,?,?,?,?)");
statement.setString(1,userForm.getEmail());
statement.setString(2,userForm.getFirstName());
statement.setString(3,userForm.getLastName());
statement.setString(4,userForm.getPassword());
statement.setString(5,userForm.getPhone());
statement.setString(6,userForm.getFax());
statement.executeUpdate();
}finally{
conn.close();
}


return mapping.findForward("success");
}
}

The error which I get on the browser

----------------------------------------------------------------------------

HTTP Status 404 - Servlet action is not available

type Status report

message Servlet action is not available

description The requested resource (Servlet action is not available) is not available.


Apache Tomcat/5.0.28

----------------------------------------------------------------------------

The moment i remove the database tags from the struts-config.xml & the db code from Action class everything works fine

Kindly respond

Rgrds
RAHIL
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Database Access problem