File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Database connection with struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Database connection with struts" Watch "Database connection with struts" New topic

Database connection with struts


Joined: May 12, 2008
Posts: 2
Hi guys.

I have a problem with connecting to Access database like inserting values in a table etc.

My Registration.jsp:

<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>




<html:form action="">
First Name: <html:text property="firstName"/><br>
Last Name: <html:text property="lastName"/><br>
Address: <html:textarea property="address"/><br>
Phone no: <html:text property="pnumber"/><br>
D.O.B: <html:text property="dob"/><br>
Sex: <html:radio property="sex" value="M"/>Male
<html:radio property="sex" value="F"/>Female<br>


<logic resent name="lastName" scope="request">
<logic:equal name="submitForm" property="age" value="a">
<logic:equal name="submitForm" property="age" value="c">
<bean:write name="lastName" scope="request"/>
</logic resent>


My Action Class :

import javax.servlet.http.HttpServletRequest;
import javax.servlet.*;
import javax.servlet.GenericServlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletConfig;
import javax.servlet.http.*;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;

import javax.servlet.http.*;
import org.apache.struts.action.*;

public class RegisterAction extends Action {

// private String Mobile;
static Connection theConn;
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
String lastName,firstName, upno,uname,uaddress,upwd,dob, mob;
RegisterForm f=(RegisterForm)form;
request.setAttribute("firstName", firstName.toUpperCase());

request.setAttribute("lastName", lastName.toUpperCase());

String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
String url = "jdbc dbc:Church";
// return mapping.findForward("success");
Connection connection = DriverManager.getConnection(url);
// theConn = MyConnection.getConnection();

String ssql="insert into userinfo values(?,?)";
PreparedStatement pistmt=connection.prepareStatement(ssql);
ResultSet rs= pistmt.executeQuery();
// pistmt.executeUpdate();
return mapping.findForward("success");

} catch(Exception e)
return mapping.findForward("error");

/** Creates a new instance of Registeruser*/
// return mapping.findForward("success");



Struts Config.xml:

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"


<set-property property="driverClass"
<set-property property="url"
value="jdbc dbc:Church" />

<form-bean name="registerForm"



<forward name="welcome" path="/"/>

<action path="/submit"
<forward name="success" path="/Success.html"/>
<forward name="error" path="/Error.html"/>


<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>

<message-resources parameter="com/myapp/struts/ApplicationResource"/>

<plug-in className="org.apache.struts.tiles.TilesPlugin" >
<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
<set-property property="moduleAware" value="true" />

<plug-in className="org.apache.struts.validator.ValidatorPlugIn">



When the data-sources tag is added in the struts config.xml file, an error comes and if I remove it, atleast it goes to Error.html page. No data is entered in the "userinfo" table in the "Church" database.

Is my data-source tag is alrite or any changes have to be made?

Please help me so that I could successfully enter data in the Access database table.

Thanks a lot.
Joe Ess

Joined: Oct 29, 2001
Posts: 9189

Originally posted by NIRMAL YARAM:
When the data-sources tag is added in the struts config.xml file, an error comes and if I remove it,

Welcome to the JavaRanch. It is a good idea to Tell The Details, for example, which version of Struts are you using and what are the errors you are getting?
You should be aware that datasource is not available in all versions of Struts. The Struts FAQ explains why and the better approaches to accessing databases.

[How To Ask Questions On JavaRanch]

Joined: May 12, 2008
Posts: 2
Thanks for your suggestion.

I'm using NetBeans 5.0

Struts- 1.2.7

Apache Tomcat-5.5.9

Databse: MS Access.

I wrote a Registration.html page which takes the first name and last name as inputs and sumits them.

When it submits, the following error occurs:

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.5.9

My Registration.html form looks like thhis:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Contact Us</title>
<link href="style.css" rel="stylesheet" type="text/css" />

<h1><p>Registration Details</p></h1>
<div id="body2"><form action="./" method="post" >
<label>FIRST NAME:</label>
<input name="firstName" type="text" id="fname" value="" /><br>
<label>LAST NAME:</label>
<input name="lastName" type="text" id="lname" value="" /><br>

<input name="" type="submit" class="botton" value="SUBMIT" />

The Reegister form and Register Action class remain the same as in the prev post.

Please help me....plzzzzzzzzzzzzzz
I agree. Here's the link:
subject: Database connection with struts
jQuery in Action, 3rd edition