This week's giveaway is in the Beginning Java forum.
We're giving away four copies of Bad Programming Practices 101 (e-book) and have Karl Beecher on-line!
See this thread for details.
Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem in inserting values to database from action class  RSS feed

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.


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

<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
<data-source type="org.apache.tomcat.dbcp.dbcp.BasicDataSource"
<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" />
<form-bean name="UserRegistrationForm" type="com.tutorial.ActionForm.UserRegistrationForm"/>
<action path="/userRegistration"
<forward name="success" path="/regSuccess.jsp" />
<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
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" />

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

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;
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')";

PreparedStatement statement = conn.prepareStatement(
"insert into USER " +
" 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();

/* String emailD = userForm.getEmail();

Statement stmt = conn.createStatement(); */


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

any help will be apprecited..Thank you
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.
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Merrill
hem kum
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.
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.
It is difficult to free fools from the chains they revere - Voltaire. tiny ad:
Why should you try IntelliJ IDEA ?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!