2

My Insert is failing via preparedStatment, I've run the statement through MySQL manually and it works, and I've double checked my params, but it's failing when run through the app.:

        try {
            myConn = DriverManager.getConnection(url, username, password);
            preStmt = myConn.prepareStatement(
            "Insert Into games(HomeTeamID, AwayTeamID, HomeTeamGoals, AwayTeamGoals, GameMonth, GameDay, GameYear, Overtime, Shootout) 
            Values(?, ?, ?, ?, '?', ?, ?, ?, ?);");
            preStmt.setInt(1,  game.getHomeID());
            preStmt.setInt(2,  game.getAwayID());
            preStmt.setInt(3,  game.getHomeGoals());
            preStmt.setInt(4,  game.getAwayGoals());
            preStmt.setString(5,  game.getGameMonth());
            preStmt.setInt(6,  game.getGameDay());
            preStmt.setInt(7,  game.getGameYear());
            preStmt.setBoolean(8,  game.isOvertime());
            preStmt.setBoolean(9,  game.isShootout());
            preStmt.executeUpdate();
            myConn.commit();
        }
        catch(SQLException sqlex) { // FAILING ON THIS CATCH
        JOptionPane.showMessageDialog(null, "Error - Game Update Failed!\n\t-> SQL Error.");
        }

I've scoured numerous docs online, but couldn't find any reason why it wouldn't work, and as mentioned, I run the same statement through MySQL with mock data, and it works...

Any suggestions?

3
  • What does sqlex.getMessage() say? Commented Jul 26, 2015 at 4:10
  • Updated Exception: Data truncation: Data too long for column 'GameMonth' at row 1 - Month being passed is string "January", db is set to vchar(9) Commented Jul 26, 2015 at 4:13
  • Issue Resolved: I was passing comboBox.toString(), should have been passing comboBox.getSelectedItem().toString() Commented Jul 26, 2015 at 4:28

1 Answer 1

2

Issue Resolved:

  1. In values(), '?' doesn't fly, was just ?
  2. I was passing comboBox.toString(), should have been passing comboBox.getSelectedItem().toString()
  3. commit failed after as I'd not set myConn.setAutoCommit(false);, removed myConn.commit() and now works...

Thank you for directing to stackTrace, I'd been manually debugging and spaced on checking that :/ Sorry

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.