my dog learned polymorphism
The moose likes Struts and the fly likes Unable to enter values in the database using struts. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Unable to enter values in the database using struts." Watch "Unable to enter values in the database using struts." New topic

Unable to enter values in the database using struts.

partha sama

Joined: Sep 07, 2012
Posts: 2

I was trying to insert a row in the database using struts. Problem is that, null values are being inserted in the database. For some reason, the values which i entered in the form are not coming up in the database. These are the code i have written.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<form action = "">
ProductId: <input type ="text" name = "ProductId"><br>
ProductName:<input type ="text" name ="ProductName"><br>
ProductPrice:<input type ="text" name="ProductPrice"><br>
ProductDescription:<input type ="text" name ="ProductDescription"><br>
<input type ="submit" value = "Register">


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">

<!-- Standard Action Servlet Configuration -->

<!-- Standard Action Servlet Mapping -->


struts-config file:

<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
<form-bean name = "ProductRegistration" type = "com.form.ProductForm">

<action path = "/productProcess"
name ="ProductRegistration"
scope ="request"
type = "com.Action.ProductAction"
input ="/Product.jsp"

<forward name ="success" path ="/welcome.jsp"></forward>

Action class:

package com.Action;

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

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dao.ProductDAO;

import com.form.ProductF;
import com.form.ProductForm;

public class ProductAction extends Action{

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// TODO Auto-generated method stub

ProductForm productForm = (ProductForm)form;

ProductF product = new ProductF();
//BeanUtils beanUtils = new BeanUtils();
BeanUtils.copyProperties(product, productForm);


ProductDAO productDAO = new ProductDAO();

return mapping.findForward("success");




package com.form;

import org.apache.struts.action.ActionForm;

public class ProductF extends ActionForm{

public ProductF()

int ProductId;
String ProductName;
float ProductPrice;
String ProductDescription;
public int getProductId() {
return ProductId;
public void setProductId(int productId) {
ProductId = productId;
public String getProductName() {
return ProductName;
public void setProductName(String productName) {
ProductName = productName;
public float getProductPrice() {
return ProductPrice;
public void setProductPrice(float productPrice) {
ProductPrice = productPrice;
public String getProductDescription() {
return ProductDescription;
public void setProductDescription(String productDescription) {
ProductDescription = productDescription;



package com.dao;

import java.sql.Connection;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.form.ProductF;
import com.form.ProductForm;
public class ProductDAO {

public void addProduct(ProductF product) throws SQLException

try {

//load the drivers
//establish the connection

Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test","root","root");

//prepare query and prepared statement.
String query ="insert into test.product(ProductId,ProductName,ProductPrice,ProductDescription)values(?,?,?,?)";
PreparedStatement prepareStatement = con.prepareStatement(query);


int i = prepareStatement.executeUpdate();
System.out.println("Record inserted successfully");


catch (ClassNotFoundException e) {
// TODO Auto-generated catch block



I tried checking this forum whether the question was answered before, but apparently, its not. I would really appreciate if you guys helped me.

I agree. Here's the link:
subject: Unable to enter values in the database using struts.
It's not a secret anymore!