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