Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in inserting values to database from action class

 
hem kum
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all, the followings are all the codes. Problem is when i use DriverManger i am able to insert into database but when i use struts datasource configuration i am not able to insert into database. It is not showing any error but data isnt inserting into database.

I am using struts 1.2.9 netBeans 6.1 mysql database and appropriate driver.

1)struts-config.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!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 type="org.apache.tomcat.dbcp.dbcp.BasicDataSource"
key="userDB">
<set-property property="driverClassName"
value="com.mysql.jdbc.Driver" />
<set-property property="url"
value="jdbc:mysql://localhost:3306/NewUser" />
<set-property property="username" value="root" />
<set-property property="password" value="root" />
<set-property property="defaultAutoCommit"
value="false" />
<set-property property="defaultReadOnly"
value="false" />
<set-property property="validationQuery"
value="SELECT COUNT(*) FROM user" />
</data-source>
</data-sources>
<form-beans>
<form-bean name="UserRegistrationForm" type="com.tutorial.ActionForm.UserRegistrationForm"/>
</form-beans>
<global-exceptions>
</global-exceptions>
<global-forwards>
</global-forwards>
<action-mappings>
<action path="/userRegistration"
type="com.tutorial.Action.UserRegistrationAction"
name="UserRegistrationForm"
input="/userRegistration.jsp">
<forward name="success" path="/regSuccess.jsp" />
</action>
</action-mappings>
<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
<message-resources parameter="com/myapp/struts/ApplicationResource"/>

<!-- ========================= Tiles plugin ===============================-->
<!--
This plugin initialize Tiles definition factory. This later can takes some
parameters explained here after. The plugin first read parameters from
web.xml, thenoverload them with parameters defined here. All parameters
are optional.
The plugin should be declared in each struts-config file.
- definitions-config: (optional)
Specify configuration file names. There can be several comma
separated file names (default: ?? )
- moduleAware: (optional - struts1.1)
Specify if the Tiles definition factory is module aware. If true
(default), there will be one factory for each Struts module.
If false, there will be one common factory for all module. In this
later case, it is still needed to declare one plugin per module.
The factory will be initialized with parameters found in the first
initialized plugin (generally the one associated with the default
module).
true : One factory per module. (default)
false : one single shared factory for all modules
- definitions-parser-validate: (optional)
Specify if xml parser should validate the Tiles configuration file.
true : validate. DTD should be specified in file header (default)
false : no validation

Paths found in Tiles definitions are relative to the main context.
-->
<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>

<!-- ========================= Validator plugin ================================= -->
<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>





2)This is the action class

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.tutorial.Action;

import com.tutorial.ActionForm.UserRegistrationForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.sql.*;
import javax.sql.*;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
*
* @author
*/
public class UserRegistrationAction extends org.apache.struts.action.Action {

private DataSource dataSource;
/* forward name="success" path="" */
private final static String SUCCESS = "success";

/**
* This is the action called from the Struts framework.
* @param mapping The ActionMapping used to select this instance.
* @param form The optional ActionForm bean for this request.
* @param request The HTTP Request we are processing.
* @param response The HTTP Response we are processing.
* @throws java.lang.Exception
* @return
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {


Connection conn = null;
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/NewUser","root", "root");

UserRegistrationForm userForm = (UserRegistrationForm) form;

System.out.println("userForm firstName" + userForm.getFirstName());

/*
dataSource = (DataSource)servlet.getServletContext().getAttribute("userDB");

Connection conn = dataSource.getConnection();
*/

/* dataSource = getDataSource(request,"userDB"); */

Statement statement = conn.createStatement();

String command = "INSERT INTO user (EMAIL, FIRST_NAME, LAST_NAME, PASSWORD, PHONE, FAX) VALUES ('myo','drf','bcb','hss','ppp','fff')";
statement.executeUpdate(command);





/*
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());

System.out.println("---" + userForm.getFax());
int num = statement.executeUpdate();

System.out.println(num);
*/
/* String emailD = userForm.getEmail();

Statement stmt = conn.createStatement(); */


conn.close();


return mapping.findForward(SUCCESS);
}
//System.out.println("userForm firstName: = " + userForm.getFirstName());
}



any help will be apprecited..Thank you
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would recommend that you not use the Struts DataSource utility. It's listed as deprecated in version 1.2.9, and has been removed entirely from version 1.3. For more information on what to use instead, see question 7 of our FAQ.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Merrill
 
hem kum
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Merril. Anyway i tried the above things with JNDI Connection yesterday and it worked. So i dont think i will need this thing ever.
 
Satchidananda Mohanty
Ranch Hand
Posts: 78
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hem !!

Can you say me what is the procedure to used the jndi configuration to connect the database..

Thanks in advance.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic