jQuery in Action, 2nd edition*
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
Author

Unable to enter values in the database using struts.

partha sama
Greenhorn

Joined: Sep 07, 2012
Posts: 2
Hello,

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.

Form:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<center><h3>ProductDetails</h3>
<form action = "productProcess.do">
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">
</form>
</center>
</body>
</html>

web.xml-file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

<!-- Standard Action Servlet Configuration -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>


<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>


</web-app>

struts-config file:

<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name = "ProductRegistration" type = "com.form.ProductForm">

</form-bean>
</form-beans>
<action-mappings>
<action path = "/productProcess"
name ="ProductRegistration"
scope ="request"
type = "com.Action.ProductAction"
input ="/Product.jsp"
>

<forward name ="success" path ="/welcome.jsp"></forward>
</action>
</action-mappings>
</struts-config>

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{

@Override
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);

System.out.println(product.getProductId());
System.out.println(product.getProductName());
System.out.println(product.getProductPrice());
System.out.println(product.getProductDescription());

ProductDAO productDAO = new ProductDAO();
productDAO.addProduct(product);




return mapping.findForward("success");




}



}

ActionForm:

package com.form;

import org.apache.struts.action.ActionForm;

@SuppressWarnings("serial")
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;
}


}

DAO:

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
Class.forName("org.gjt.mm.mysql.Driver");
//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);

prepareStatement.setInt(1,productForm.getProductId());
prepareStatement.setString(2,productForm.getProductName());
prepareStatement.setFloat(3,productForm.getProductPrice());
prepareStatement.setString(4,productForm.getProductDescription());

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



}

catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



}

}

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: http://aspose.com/file-tools
 
subject: Unable to enter values in the database using struts.
 
Similar Threads
how the value in the form pass into Action class in Struts
Once more: javax.servlet.jsp.JspException: Cannot find bean
Dynamic drop down in struts
org.apache.jasper.JasperException: An exception occurred processing JSP page /jsps/ActionForms/Displ