0

Hello i'm having trouble creating a MySQL "Event" through JAVA.JDBC. Here's the code:

public void skapaAuktionsEvent(String produktnamn, String slutdatum) throws SQLException{

    String sql = "CREATE EVENT "+ produktnamn +" ON SCHEDULE AT '"+ slutdatum +"' DO UPDATE Auktion SET AvslutadAuktion = 1 WHERE produktnamn = "+ produktnamn;

    System.out.println("Skapar event");


    PreparedStatement stm = conn.prepareStatement("CREATE EVENT ? ON SCHEDULE AT ? DO UPDATE Auktion SET AvslutadAuktion = 1 WHERE produktnamn = ?",
            ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    stm.setString(1, produktnamn);
    stm.setString(2, slutdatum);
    stm.setString(3, produktnamn);

    PreparedStatement stm2 = conn.prepareStatement(sql,
            ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);

    PreparedStatement stm3 = conn.prepareStatement("CREATE EVENT TestProdukt ON SCHEDULE AT '2015-02-15 15:15:00' DO UPDATE Auktion SET AvslutadAuktion = 1 WHERE produktnamn = TestProdukt",
            ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);


    stm.execute();
    System.out.println("Skapat Skiten");

    stm.close();
}

So i've gotten both "stm2" and "stm3" works, but for some reason when i try and use stm which uses the .setString it all goes fubar.

I've tried removing the stm.setString(2, slutdatum) in case it was moaning about it wanting a Date formated value instead of string but that didnt help. It seems fail the moment the ? gets introduced. So i'm wondering what i'm doing wrong, and if there is another workaround except for using the stm2 variant since i doubt it's SQL safe.

1 Answer 1

2

I think event name cant set as parameter.Do

String eventName="myEventName";//set event name
PreparedStatement stm = conn.prepareStatement("CREATE EVENT "+eventName+" ON SCHEDULE AT ? DO UPDATE Auktion SET AvslutadAuktion = 1 WHERE produktnamn = ?",

Always remember when you think you are having sql problem print the query in console and copy the query and run it in your database gui then you can check whats wrong with the query.

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

1 Comment

Yupp, that was it. The eventName cant be any variable apparently.

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.