todivefor
todivefor

Reputation: 121

sql delete based on date

I am a total noob to java and sqlite. This should be simple, but I have tried and searched and can't get it to work. I have a date field in SQL that I am formatting as a sql date (MM/dd/yy). I want to delete based on a date passed. For the moment, I am only trying to display rows based on a passed date.

My code to run the query is:

String query = "select * from Peter1Score where    DateSort='"+convertSQLDate("09/20/15")+"'";
PreparedStatement pst = connection.prepareStatement(query);
ResultSet rs = pst.executeQuery();

My converSQLDate() is:

public static java.sql.Date convertSQLDate (String sqlDateIn)
{
    java.sql.Date returnDate = null;

    SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy");
    Date parsed;
    try 
    {
        parsed = formatter.parse(sqlDateIn);
        java.sql.Date sqlDate = new java.sql.Date(parsed.getTime());
        returnDate = sqlDate;
    }
    catch (ParseException e) 
    {
        e.printStackTrace();
    }
    return returnDate;
}

I am passing 09/20/15 just for test. I have a record with that date, but it doesn't get selected.

Upvotes: 0

Views: 590

Answers (1)

Mick Mnemonic
Mick Mnemonic

Reputation: 7956

You're not binding any variables into the statement (using the setXY() methods of PreparedStatement). Instead, you're concatenating a string value (result of Date.toString()) into the query literal.

Try this instead:

String query = "select * from Peter1Score where DateSort=?";
PreparedStatement pst = connection.prepareStatement(query);
pst.setDate(1, convertSQLDate("09/20/15"));

Upvotes: 2

Related Questions