3

I am trying to insert a date object into the database but it says im trying to insert an Integer. Here is my code:

public void insertAssignment(long studentId, String newAssignment, int ptsTotal, int ptsRecieved, String category, Date dueDate, String term, int yr) {
    java.sql.Date temp = new java.sql.Date(dueDate.getTime());
    try{
        s.execute("INSERT INTO Assignments " +
                  "VALUES (" + studentId + ",'" + newAssignment + "'," + ptsTotal +
                  "," + ptsRecieved + ",'" + category + "'," + temp
                  + ",'" + term + "'," + yr + ")");
        System.out.println("Assignment inserted.");
    }
    catch(SQLException error){
        System.err.println("Unable to insertAssignment.");
        error.printStackTrace(System.err);
        System.exit(0);
    }
} 

My error: java.sql.SQLSyntaxErrorException: Columns of type 'DATE' cannot hold values of type 'INTEGER'.

1
  • It is because the column matching your date column is actually an int value. Also use PreparedStatement. In your sql add the column names. Commented Jul 20, 2011 at 23:17

1 Answer 1

4

To do this, use a prepared statement instead and use a setter.

Currently you flatten your date to a string value (which needs to be in the right format for the database for this to work) and lose that it is a date so the database needs to parse it. You avoid this using the prepared statement.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.