• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unable to enter values in the database using struts.

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
reply
    Bookmark Topic Watch Topic
  • New Topic