Hi All!
I'm having some trouble getting a date from a
Java application into a mySQL table. I have 2 fields on a form Donation Date and Donation Amount. The dates get all screwed up because the mysql format is YYYY-MM-DD and I'm entering the date MM/DD/YYYY and attempting to insert it into the database. Below is my code, can someone show me how to fix?
public void addDonation()
{
//test for values in both fields
if (txtDonationDate.getText().equals("") || txtDonationAmount.getText().equals(""))
{
lblStatus.setText("Incomplete Values! You must enter values in the Date and Amount field");
}
else
{
//test for a valid date
if (dateIsValid(txtDonationDate.getText())==false )
{
lblStatus.setText("No Donation Values!! You must enter values in the Date and Amount field");
}
else
{
try
{
//if both conditions pass insert rec
//TO DO ADD INSERT
Statement state = m_DBCnn.createStatement();
//build the sql statement to insert
String strSQL = "INSERT INTO donations (";
strSQL += "donorID, amount, donation_date ) VALUES ('";
//first name
strSQL += lblID.getText() + "', '" ;
//last Name
strSQL += txtDonationAmount.getText() + "', '" ;
//address1
strSQL += txtDonationDate.getText() + "')" ;
//give the user some feedback
lblStatus.setText("Sending query: " + m_DBCnn.nativeSQL(strSQL));
//exec the SQL String
int result = state.executeUpdate(strSQL);
//test for sucess
if (result == 1)
{
lblStatus.setText("Submitted - Insertion successful");
}
else
{
lblStatus.setText("Submitted Insertion unsuccessful");
}
//clear the fields
txtDonationAmount.setText("");
txtDonationDate.setText("");
//reget the donations
getDonations();
//display them
displayDonations();
}
catch(SQLException sqlx)
{
sqlx.printStackTrace();
}
}//end else date is valid
}//end else blank fields
}//end add donation
private static boolean dateIsValid(String p_Date)
{
boolean result = true;
try
{
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
df.setLenient(false);
final String dateString = p_Date;
df.parse(dateString);
}
catch (Exception e)
{
result = false;
}
return result;
}//end date is valid