Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to enter values in the database using struts.

partha sama
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.

  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic