user3673033
user3673033

Reputation: 23

JDBC Derby highscore implementation

so i am creating a game and have implemented a database to store my highscore. I think the database is working, but when i go to view the highscore in the menu i am seeing the value 0, its not saving the highscore. Here is what i have done so far, please let me know how i can solve this.

public class ScoreDB {
    Connection conn = null;
    String url = "jdbc:derby:ScoresDB;create=true";
    String username = "hello";
    String pass = "hello";


    public void connectScoreDB(){
        try{

            conn= DriverManager.getConnection(url,username,pass);

        }catch(SQLException ex){
            System.err.println("SQLException: " + ex.getMessage());
        }
    }

    public void createScoreTable(){
        try{
            Statement statement = conn.createStatement();

            String newTable ="ScoresDB";

            String createTable="CREATE TABLE " + newTable + "(Score INT)";
            statement.execute(createTable);
        }catch(SQLException ex){
            System.err.println("SQLException: " + ex.getMessage());
        }
    }

    public void addHighScore(){
        int highScore = FlappyBird.highScore;
        String add = "INSERT INTO ScoresDB(Score) VALUES (?)";
              try(PreparedStatement ps = conn.prepareCall(add)){
                ps.setInt(1, highScore);
                ps.execute();
            }catch(SQLException ex){
            System.err.println("SQLException: " + ex.getMessage());
        }
    }

    public void update(String update){
        try(Statement statement = conn.createStatement()){
        statement.executeUpdate(update);
        statement.close();
        } catch (SQLException ex) {
            Logger.getLogger(ScoreDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void main(String[] args){
        ScoreDB db = new ScoreDB();
        db.connectScoreDB();
        db.createScoreTable();
    }
}


public static int highScore = 0;

Upvotes: 1

Views: 83

Answers (1)

Dyre
Dyre

Reputation: 547

Do you call the addHighScore() method anywhere?

Upvotes: 1

Related Questions