my dog learned polymorphism*
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
Author

Database connection with struts

NIRMAL YARAM
Greenhorn

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>
<head><title>Registration</title></head>
<body>

<h3>Register</h3>

<html:errors/>

<html:form action="submit.do">
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>

<html:submit/>
</html:form>

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

</body>
</html>


My Action Class : RegisterAction.java


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 java.io.*;

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;
firstName=f.getFirstName();
request.setAttribute("firstName", firstName.toUpperCase());

lastName=f.getLastName();
request.setAttribute("lastName", lastName.toUpperCase());




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


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

} catch(Exception e)
{
//out.println(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"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">


<struts-config>

<data-sources>
<data-source>
<set-property property="driverClass"
value="sun.jdbc.odbc.JdbcOdbcDriver"
<set-property property="url"
value="jdbc dbc:Church" />
</data-source>
</data-sources>


<form-beans>
<form-bean name="registerForm"
type="reg.RegisterForm"/>
</form-beans>

<global-exceptions>

</global-exceptions>

<global-forwards>
<forward name="welcome" path="/Welcome.do"/>
</global-forwards>

<action-mappings>
<action path="/submit"
type="reg.RegisterAction"
name="registerForm"
input="/Registration.jsp"
scope="request"
validate="true">
<forward name="success" path="/Success.html"/>
<forward name="error" path="/Error.html"/>
</action>

</action-mappings>

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


<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>

</struts-config>

Note:

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
Bartender

Joined: Oct 29, 2001
Posts: 8866
    
    8

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
NIRMAL YARAM
Greenhorn

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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<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" />
</head>

<body>
<br>
<h1><p>Registration Details</p></h1>
<hr></hr>
<div id="body2"><form action="./reguser.do" 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" />
</form>
</div>
</body>
</html>

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: http://aspose.com/file-tools
 
subject: Database connection with struts