Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I am using below mentioned code to insert values in MSAccess 2000 which having table structure as mentioned below:-

Field Name	Data Type
TodaysDate	Date/Time
Cart ID	Number
Client Name	Text
Team & Segment	Text
Duration	Number
Tape ID	Text
Start Date	Date/Time
End Date	Date/Time



Code:-

Java
private boolean enterDataIntoMSAccessDatabaseusingPreparedStatement() 
    {
        try {
            
            ps = connection.prepareStatement("INSERT INTO [Cart ID Details] VALUES (?,?,?,?,?,?,?,?)");
            
            System.out.println("After Query");
            
        } 
        
        catch (SQLException se) {
            
            generateErrorMessage("Error in Prepared Statement \n " + se.getMessage() );
            
            return false;
            
        }
        
        catch (Exception e)
        {
            generateErrorMessage("Unexpected Error Occured \n " + e.getMessage());
        }
        
        String todaysDate = cartIDApplicationAddCartIDDatejTextField.getText().trim();
        
        String cartID = cartIDApplicationAddCartIDCartIDjTextField.getText().trim();
        
        String clientName = cartIDApplicationAddCartIDClientNamejTextField.getText().trim();
        
        String teamSegment = cartIDApplicationAddCartIDTeamAndSegmentjTextField.getText().trim();
        
        String duration = cartIDApplicationAddCartIDDurationjTextField.getText().trim();
        
        String tapeID = cartIDApplicationAddCartIDTapeIDjTextField.getText().trim();
        
        String startDate = cartIDApplicationAddCartIDStartDatejTextField.getText().trim();
        
        String endDate = cartIDApplicationAddCartIDEndDatejTextField.getText().trim();
        
        try {
        
            //System.out.println("Before ps.setString()");
            
            ps.setString(1, todaysDate);
            ps.setString(2, cartID );
            ps.setString(3, clientName);
            ps.setString(4, teamSegment);
            ps.setString(5, duration);
            ps.setString(6, tapeID);
            ps.setString(7, startDate);
            ps.setString(8, endDate);


            //System.out.println("After ps.setString()");
          
                ps.executeUpdate();
           
            
       } 
        catch (SQLException se) {
            
            generateErrorMessage("Error while inserting data in database \n " + se.getMessage());
            
            return false;
        }
        
        catch (Exception e)
        {
            generateErrorMessage("Unexpected Error Occured \n" + e.getMessage() );
        }
        
        return true;
    }     


The above boolean function is called in "Save Button" action event, but when I click Save I am getting as Runtime Exception.

Kindly help me to sort out this issue.
Posted
Updated 7-Nov-13 6:59am
v2
Comments
ZurdoDev 7-Nov-13 12:32pm    
What's the error?
E.F. Nijboer 7-Nov-13 12:53pm    
step through with the debugger to see what happens.
Sergey Alexandrovich Kryukov 7-Nov-13 15:52pm    
You did not provide any exception information, so what would you expect from us? Use the debugger.
—SA
Homero Rivera 7-Nov-13 23:20pm    
You are using ps.SetString, and you actually use string values. Where are the apostrohpes for the strings? Are they automatically set by ps.SetString? I don't think so.
Nagy Vilmos 8-Nov-13 4:39am    
Yes, the prepared statement class does all that for you, it prevents the old Bobby Tables problem.

1 solution

Without even seeing the exception, you are setting the values using the wrong methods!

Dates are set using setDate, integers setInt, etc.

You need something like:

// formatter for converting the dates:
SimpleDateFormat df = new SimpleDateFormat("dd/mm/yyyy", Locale.ENGLISH);
// the date required is a java.sql.Date
    // not a hava.util.Date so it must be converted:
ps.setDate(1, new Date(df.parse(todaysDate).getTime()));
ps.setInt(2, Integer.parseInt(cartID));
ps.setString(3, clientName);
ps.setString(4, teamSegment);
ps.setInt(5, Integer.parseInt(duration));
ps.setString(6, tapeID);
ps.setDate(7, new Date(df.parse(startDate).getTime()));
ps.setDate(8, new Date(df.parse(endDate).getTime()));

//System.out.println("After ps.setString()");
ps.executeUpdate();


Personally, I'd extract the data directly to the correct data types as there may be parsing errors which your code will only catch under general exceptions.
 
Share this answer
 
Comments
Sushil 07101990 11-Nov-13 11:51am    
Dear Nagy,

As per your above code, I implemented the same in my code which looks likes below:-

private boolean enterDataIntoMSAccessDatabaseusingPreparedStatement()
{
try {

ps = connection.prepareStatement("INSERT INTO Cart ID Details VALUES (?,?,?,?,?,?,?)");

System.out.println("After Query");

}

catch (SQLException se) {

generateErrorMessage("Error in Prepared Statement \n " + se.getMessage() );

return false;

}

catch (Exception e)
{
generateErrorMessage("Unexpected Error Occured \n " + e.getMessage());
}

String todaysDate = cartIDApplicationAddCartIDDatejTextField.getText().trim();

String cartID = cartIDApplicationAddCartIDCartIDjTextField.getText().trim();

String clientName = cartIDApplicationAddCartIDClientNamejTextField.getText().trim();

String teamSegment = cartIDApplicationAddCartIDTeamAndSegmentjTextField.getText().trim();

String duration = cartIDApplicationAddCartIDDurationjTextField.getText().trim();

String tapeID = cartIDApplicationAddCartIDTapeIDjTextField.getText().trim();

String startDate = cartIDApplicationAddCartIDStartDatejTextField.getText().trim();

String endDate = cartIDApplicationAddCartIDEndDatejTextField.getText().trim();

try {

System.out.println("Before ps.setString()");

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd", Locale.ENGLISH);
System.out.println("Simple Date Format");

/*ps.setString(1, todaysDate);
ps.setString(2, cartID );
ps.setString(3, clientName);
ps.setString(4, teamSegment);
ps.setString(5, duration);
ps.setString(6, tapeID);
ps.setString(7, startDate);
ps.setString(8, endDate);*/

ps.setDate(1, new java.sql.Date(simpleDateFormat.parse(todaysDate).getTime()));
ps.setString(2, cartID);
ps.setString(3, clientName);
ps.setString(4, teamSegment);
ps.setString(5, duration);
ps.setString(6, tapeID);
ps.setDate(7, new java.sql.Date(simpleDateFormat.parse(startDate).getTime()));
ps.setDate(8, new java.sql.Date(simpleDateFormat.parse(endDate).getTime()));

System.out.println("After ps.setString()");

ps.executeUpdate();


}
catch (SQLException se) {

generateErrorMessage("Error while inserting data in database \n " + se.getMessage());

return false;
}

catch (Exception e)
{
generateErrorMessage("Unexpected Error Occured \n" + e.getMessage() );
}

return true;
}

I got below error after implementing the above code:-

Unexpected Error Occured
Unparseable date: "11-Nov-2013"

Kindly help me for the same.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900