PDA

View Full Version : Date problem and null modelandview



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;
}
}

christopher1234
Jan 13th, 2008, 04:35 PM
DEBUG DataSourceUtils - Fetching JDBC Connection from DataSource
DEBUG DriverManagerDataSource - Creating new JDBC Connection to [jdbc:mysql://localhost:3306/audit]
DEBUG StatementCreatorUtils - Setting SQL statement parameter value: column index 1, parameter value [172008], value class [java.lang.Integer], SQL type 4
DEBUG StatementCreatorUtils - Setting SQL statement parameter value: column index 2, parameter value [12345], value class [java.lang.String], SQL type 12
DEBUG DataSourceUtils - Returning JDBC Connection to DataSource
INFO InsertQuantityOnHandDaoImpl - Item Number 172008 Is VALID
DEBUG ValidationUtils - Validator found no errors
DEBUG InsertQuantityOnHandController - No errors -> processing submit
INFO InsertQuantityOnHandController - Handling onSubmit of InsertQuantityOnHandController.java
INside mav
INFO InsertQuantityOnHandController - Returning hello viewSun Jan 13 16:32:19 CST 2008
Inside try and catch block
INFO InsertQuantityOnHandController - This is what inside com.vendornet.model.InsertQuantityOnHand@1ab11b0[Item NUmber=172008,Vendor Id=12345,Quantity=12]
INFO InsertQuantityOnHandDaoImpl - Handling insertQuantityOnHand in InsertQuantityOnHandDaoImpl
INFO InsertQuantityOnHandDaoImpl - Inside insertQuantityOnHand method
Inside addinsertquantityonhand
java.text.SimpleDateFormat@7c669100
DEBUG DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'VendornetMassUpdate': assuming HandlerAdapter completed request handling
DEBUG DispatcherServlet - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@15e207 5
DEBUG DispatcherServlet - Successfully completed request
DEBUG XmlWebApplicationContext - Publishing event in context [org.springframework.web.context.support.XmlWebAppl icationContext@b8f8eb]: ServletRequestHandledEvent: url=[/VendornetMassUpdate/insertQuantityOnHandForm.htm]; client=[127.0.0.1]; method=[POST]; servlet=[VendornetMassUpdate]; session=[4FE5AE1F683BEC07DA841FBAE768F2E4]; user=[null]; time=[691ms]; status=[OK]


What might be the problem? I mean i am sure to insert the date, timsestamp automatically in the table looks good to me. because of this my validationQuery works fine but my insert query fails. The only thing i can figure out is that :

ModelAndView mav = null;

this might be causing problem. But i have seen couple of other MVc example in the book and was trying to adopt their methadology. Let me know what i am doing wrong. Thanks for the tips and advices atlast i am able to get the SQL connection through the transactions and with the help of springframework forums.

nyte3k
Jan 14th, 2008, 08:15 AM
For timestmap you could just set your column in your table to insert/update the timestamp automatically.



-- MySQL
ALTER TABLE something ADD my_timestamp Timestamp default current_timestamp on update current_timestamp


If instead you wanted to insert the timestamp I usually handle mine like so...




Date date = new Date();
Timestamp ts = new Timestamp(date.getTime());

//And then you would insert the ts into the DB.




As far as the model and view being null, i'm not really sure, but I typically handle mine like so...



mav = new ModelAndView(getSuccessView(), "data", myModel);


not sure if that will make a difference or not...I simple have an identifier for the 'myModl' map...so in the JSP page it's accessed like...



<c:out value="${data.someModelObject}"/>

hope some of this helps

Suleman Zia
Jan 14th, 2008, 12:12 PM
Nice one dude.

christopher1234
Jan 14th, 2008, 07:10 PM
Nope that was not the problem. My Syntax is right i believe, when i tried your scenario i get the same output null. I wonder why is it not storing in the database? I've searched it everywhere but could'nt find anything except this:



NFO InsertQuantityOnHandDaoImpl - Item Number 172008 Is VALID
INFO InsertQuantityOnHandDaoImpl - Handling insertQuantityOnHand in InsertQuantityOnHandDaoImpl
INFO InsertQuantityOnHandDaoImpl - Inside insertQuantityOnHand method
Inside addinsertquantityonhand
java.text.SimpleDateFormat@7c669100
DEBUG DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'abcMassUpdate': assuming HandlerAdapter completed request handling


Everything works fine. Validation works, my validation query works after that when its time to insert the values in the database this is what i get. anyone?