christopher1234
Jan 13th, 2008, 04:30 PM
I am getting a user input from 3 textfields. itemnumber, id, quantity. I have written an insert query method. My problem is that there are 6 columns in my sql table and i am only getting 3 inputs from the user just for the item, id and quantity. Date, timestamp and one more string should be entered automatically in the table.
This is how i created my method
private static final class InsertQuantityOnHandInformation extends
SqlUpdate{
private static final String INSERT_SQL = "insert into qoh(qh_item_no, qh_vendor_id," +
"qh_inv_date, qh_quantity, qh_item_uom, qh_upd_datetime" +
"values('?','?','?','?','?','?')";
public InsertQuantityOnHandInformation(final DataSource ds, final String sql) {
super(ds, (sql == null) ? INSERT_SQL : sql);
declareParameter(new SqlParameter(Types.INTEGER));
declareParameter(new SqlParameter(Types.VARCHAR));
declareParameter(new SqlParameter(Types.DATE));
declareParameter(new SqlParameter(Types.INTEGER));
declareParameter(new SqlParameter(Types.VARCHAR));
declareParameter(new SqlParameter(Types.TIMESTAMP));
compile();
}
}
public InsertQuantityOnHandInformation getInsertQuantityOnHandInformation() {
if(this.insertQuantityOnHandInformation == null ) {
this.insertQuantityOnHandInformation = new InsertQuantityOnHandInformation(dataSource, null);
}
return this.insertQuantityOnHandInformation;
}
public void insertQuantityOnHand(final InsertQuantityOnHand insertQuantityOnHand)
throws DataAccessException {
if(Logger.isInfoEnabled()) {
Logger.info("Handling insertQuantityOnHand in InsertQuantityOnHandDaoImpl ");
}
try {
// checkVendorItemNumber();
Logger.info("Inside insertQuantityOnHand method ");
System.out.println("Inside addinsertquantityonhand");
SimpleDateFormat ts = new SimpleDateFormat("MM/dd/yyyy");
System.out.println(ts);
Date invDate =
new java.sql.Date(
ts.parse(insertQuantityOnHand.getInventoryDate()). getTime());
final Object[] objs = new Object[] {
insertQuantityOnHand.getItemNumber(),
insertQuantityOnHand.getVendorId(),
invDate,
insertQuantityOnHand.getQuantity(),
ITEM_UOM,
new Timestamp(System.currentTimeMillis())
};
System.out.println("updating");
getInsertQuantityOnHandInformation().update(objs);
System.out.println("updated");
} catch(DataAccessException ex) {
System.out.println(ex.getMessage());
Logger.info("Data Access Exception occured " + ex.getMessage());
} catch ( ParseException e) {
if (Logger.isErrorEnabled()) {
Logger.error("ParseException occurred in insertCarrierHolidays()" + e.getMessage());
Logger.error(e.getMessage());
}
}
}
Itemnumber = Integer(IN sql table)
id = char
quantity = integer
Date = date
update_date_time = datetime
.
.
and here is my Controller
packages .
.
.
.
public class InsertQuantityOnHandController extends SimpleFormController{
private Log Logger = LogFactory.getLog(InsertQuantityOnHandController.c lass);
private static final String ITEM_NUMBER = "itemNumber";
private static final String VENDOR_ID = "vendorId";
private static final String QUANTITY = "quantity";
private InsertQuantityOnHandService insertQuantityOnHandService;
private InsertQuantityOnHandValidator insertQuantityOnHandValidator;
public InsertQuantityOnHandController() {
setCommandClass(InsertQuantityOnHand.class);
}
@SuppressWarnings("deprecation")
public Object formBackingObject(final HttpServletRequest request)
throws Exception {
if (Logger.isInfoEnabled()) {
Logger.info("Handling formBackingObject of InsertQuantityOnHandController");
}
final InsertQuantityOnHand insertQuantityOnHand = new InsertQuantityOnHand();
final String itemNumber = request.getParameter(ITEM_NUMBER);
final String vendorId = request.getParameter(VENDOR_ID);
// final String quantity = ServletRequestUtils.getStringParameter(request, QUANTITY);
final String quantity = request.getParameter(QUANTITY);
// insertQuantityOnHand.getItemNumber();
insertQuantityOnHand.setItemNumber(itemNumber);
insertQuantityOnHand.setVendorId(vendorId);
insertQuantityOnHand.setQuantity(quantity);
// insertQuantityOnHand.getVendorId();
// insertQuantityOnHand.getQuantity();
return insertQuantityOnHand;
}
public ModelAndView onSubmit(
HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors)
throws Exception {
if (Logger.isInfoEnabled()) {
Logger.info("Handling onSubmit of InsertQuantityOnHandController.java");
}
ModelAndView mav = null;
System.out.println("INside mav");
String now = (new Date()).toString();
Logger.info("Returning hello view" + now);
Map myModel = new HashMap();
List list = new ArrayList();
try {
System.out.println("Inside try and catch block");
// String userId = request.getSession(false).getAttribute("user").toString().trim();
InsertQuantityOnHand insertQuantityOnHand = (InsertQuantityOnHand)command;
Logger.info("This is what inside " + insertQuantityOnHand);
/*boolean valid = getInsertQuantityOnHandService().validateItemNumbe rQuery(insertQuantityOnHand.getItemNumber(), insertQuantityOnHand.getVendorId());
if(!valid) {
errors.reject("item number is invalid");
mav = showForm(request, response, errors);
}*/
//else {
insertQuantityOnHandService.addQuantityOnHand(inse rtQuantityOnHand);
System.out.println("Inside modelview method and database updated");
myModel.put("insertQuantityOnHand", insertQuantityOnHand);
myModel.put("insertQuantityOnHandService", insertQuantityOnHandService);
mav = new ModelAndView(getSuccessView(), myModel);
Logger.info("This is what inside " + mav);
//}
}
//insertQuantityOnHandDao.addInsertQuantityOnHand(in sertQuantityOnHand, userId);
catch (DataIntegrityViolationException e) {
myModel.put("statusResult", "IntegrityViolation");
} catch (Exception e) {
myModel.put("statusResult", "Failure");
}
//return showForm(request, response, errors, myModel);
return mav;
//return new ModelAndView("addInsertQuantityOnHand", myModel);
}
public void setInsertQuantityOnHandService(InsertQuantityOnHan dService insertQuantityOnHandService) {
this.insertQuantityOnHandService = insertQuantityOnHandService;
}
public InsertQuantityOnHandService getInsertQuantityOnHandService() {
return insertQuantityOnHandService;
}
public void setInsertQuantityOnHandValidator(InsertQuantityOnH andValidator insertQuantityOnHandValidator) {
this.insertQuantityOnHandValidator = insertQuantityOnHandValidator;
}
public InsertQuantityOnHandValidator getInsertQuantityOnHandValidator() {
return insertQuantityOnHandValidator;
}
}
This is how i created my method
private static final class InsertQuantityOnHandInformation extends
SqlUpdate{
private static final String INSERT_SQL = "insert into qoh(qh_item_no, qh_vendor_id," +
"qh_inv_date, qh_quantity, qh_item_uom, qh_upd_datetime" +
"values('?','?','?','?','?','?')";
public InsertQuantityOnHandInformation(final DataSource ds, final String sql) {
super(ds, (sql == null) ? INSERT_SQL : sql);
declareParameter(new SqlParameter(Types.INTEGER));
declareParameter(new SqlParameter(Types.VARCHAR));
declareParameter(new SqlParameter(Types.DATE));
declareParameter(new SqlParameter(Types.INTEGER));
declareParameter(new SqlParameter(Types.VARCHAR));
declareParameter(new SqlParameter(Types.TIMESTAMP));
compile();
}
}
public InsertQuantityOnHandInformation getInsertQuantityOnHandInformation() {
if(this.insertQuantityOnHandInformation == null ) {
this.insertQuantityOnHandInformation = new InsertQuantityOnHandInformation(dataSource, null);
}
return this.insertQuantityOnHandInformation;
}
public void insertQuantityOnHand(final InsertQuantityOnHand insertQuantityOnHand)
throws DataAccessException {
if(Logger.isInfoEnabled()) {
Logger.info("Handling insertQuantityOnHand in InsertQuantityOnHandDaoImpl ");
}
try {
// checkVendorItemNumber();
Logger.info("Inside insertQuantityOnHand method ");
System.out.println("Inside addinsertquantityonhand");
SimpleDateFormat ts = new SimpleDateFormat("MM/dd/yyyy");
System.out.println(ts);
Date invDate =
new java.sql.Date(
ts.parse(insertQuantityOnHand.getInventoryDate()). getTime());
final Object[] objs = new Object[] {
insertQuantityOnHand.getItemNumber(),
insertQuantityOnHand.getVendorId(),
invDate,
insertQuantityOnHand.getQuantity(),
ITEM_UOM,
new Timestamp(System.currentTimeMillis())
};
System.out.println("updating");
getInsertQuantityOnHandInformation().update(objs);
System.out.println("updated");
} catch(DataAccessException ex) {
System.out.println(ex.getMessage());
Logger.info("Data Access Exception occured " + ex.getMessage());
} catch ( ParseException e) {
if (Logger.isErrorEnabled()) {
Logger.error("ParseException occurred in insertCarrierHolidays()" + e.getMessage());
Logger.error(e.getMessage());
}
}
}
Itemnumber = Integer(IN sql table)
id = char
quantity = integer
Date = date
update_date_time = datetime
.
.
and here is my Controller
packages .
.
.
.
public class InsertQuantityOnHandController extends SimpleFormController{
private Log Logger = LogFactory.getLog(InsertQuantityOnHandController.c lass);
private static final String ITEM_NUMBER = "itemNumber";
private static final String VENDOR_ID = "vendorId";
private static final String QUANTITY = "quantity";
private InsertQuantityOnHandService insertQuantityOnHandService;
private InsertQuantityOnHandValidator insertQuantityOnHandValidator;
public InsertQuantityOnHandController() {
setCommandClass(InsertQuantityOnHand.class);
}
@SuppressWarnings("deprecation")
public Object formBackingObject(final HttpServletRequest request)
throws Exception {
if (Logger.isInfoEnabled()) {
Logger.info("Handling formBackingObject of InsertQuantityOnHandController");
}
final InsertQuantityOnHand insertQuantityOnHand = new InsertQuantityOnHand();
final String itemNumber = request.getParameter(ITEM_NUMBER);
final String vendorId = request.getParameter(VENDOR_ID);
// final String quantity = ServletRequestUtils.getStringParameter(request, QUANTITY);
final String quantity = request.getParameter(QUANTITY);
// insertQuantityOnHand.getItemNumber();
insertQuantityOnHand.setItemNumber(itemNumber);
insertQuantityOnHand.setVendorId(vendorId);
insertQuantityOnHand.setQuantity(quantity);
// insertQuantityOnHand.getVendorId();
// insertQuantityOnHand.getQuantity();
return insertQuantityOnHand;
}
public ModelAndView onSubmit(
HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors)
throws Exception {
if (Logger.isInfoEnabled()) {
Logger.info("Handling onSubmit of InsertQuantityOnHandController.java");
}
ModelAndView mav = null;
System.out.println("INside mav");
String now = (new Date()).toString();
Logger.info("Returning hello view" + now);
Map myModel = new HashMap();
List list = new ArrayList();
try {
System.out.println("Inside try and catch block");
// String userId = request.getSession(false).getAttribute("user").toString().trim();
InsertQuantityOnHand insertQuantityOnHand = (InsertQuantityOnHand)command;
Logger.info("This is what inside " + insertQuantityOnHand);
/*boolean valid = getInsertQuantityOnHandService().validateItemNumbe rQuery(insertQuantityOnHand.getItemNumber(), insertQuantityOnHand.getVendorId());
if(!valid) {
errors.reject("item number is invalid");
mav = showForm(request, response, errors);
}*/
//else {
insertQuantityOnHandService.addQuantityOnHand(inse rtQuantityOnHand);
System.out.println("Inside modelview method and database updated");
myModel.put("insertQuantityOnHand", insertQuantityOnHand);
myModel.put("insertQuantityOnHandService", insertQuantityOnHandService);
mav = new ModelAndView(getSuccessView(), myModel);
Logger.info("This is what inside " + mav);
//}
}
//insertQuantityOnHandDao.addInsertQuantityOnHand(in sertQuantityOnHand, userId);
catch (DataIntegrityViolationException e) {
myModel.put("statusResult", "IntegrityViolation");
} catch (Exception e) {
myModel.put("statusResult", "Failure");
}
//return showForm(request, response, errors, myModel);
return mav;
//return new ModelAndView("addInsertQuantityOnHand", myModel);
}
public void setInsertQuantityOnHandService(InsertQuantityOnHan dService insertQuantityOnHandService) {
this.insertQuantityOnHandService = insertQuantityOnHandService;
}
public InsertQuantityOnHandService getInsertQuantityOnHandService() {
return insertQuantityOnHandService;
}
public void setInsertQuantityOnHandValidator(InsertQuantityOnH andValidator insertQuantityOnHandValidator) {
this.insertQuantityOnHandValidator = insertQuantityOnHandValidator;
}
public InsertQuantityOnHandValidator getInsertQuantityOnHandValidator() {
return insertQuantityOnHandValidator;
}
}