0

I am wanting to save the highscore from my game to a database rather than a .dat file and was hoping if anyone could give me some help on how i could do it.

I have created the class for the database, im just not sure how i can implement the highscore to be saved in the database.

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());
        }
    }

2 Answers2

3

you can do as follows

    public void Add(int highScore) throws SQLException {
       String query = "INSERT INTO ScoresDB VALUES(highScore)";
       try (Statement stmt = getConn().createStatement()) {
          stmt.executeUpdate(query);
    }
}

I do not know how many field you have in your table, but if you want to use INSERT command in general case you should follow this

INSERT INTO table_name VALUES (value1,value2,value3,...);

Note: your ScoresDB has one filed which is high score

The Second way is using PreparedStatement

For example:

public void Add(int highScore){
 String sql = "INSERT INTO ScoresDB  (highScore) VALUES (?)";
        try (PreparedStatement ps = getConn().prepareCall(sql)) {
            ps.setString(1, highScore);
            ps.executeUpdate();
  }
}    

To learn the difference between using statement and preparedstatement, you should take a look at this link as well. Difference between Statement and PreparedStatement

Community
  • 1
  • 1
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
  • Thanks, but i dont want to insert the values myself, i want them to be inserted automatically via the users score – user3673033 May 25 '14 at 21:58
  • K, I added the second way to my answer. But in anyway, you should calculate or read the highscore from somewhere and pass it on to this method so it is going to be added to your ScoresDB. Hope it helps. If it does, please vote up – Kick Buttowski May 25 '14 at 22:20
  • Cool thanks will try that way. Unfortunately i cannot vote you up because i dont have enough reputatuion. do you have a email i can contact you on if i run into other difficulties ? – user3673033 May 25 '14 at 22:30
  • if you downvoted me , i am ok with it but could you tell me why? – Kick Buttowski Sep 16 '14 at 00:49
2

The easiest way would be to use SQL. Oracle offers nice examples in combination Oracle. In additional you need some knowledge about SQL a good starting point is W3Schools. Alterantive approach is to use a OR approach over Hibernate or EclipseLink and more. Hibernate.

Useing the links above. I would highly recommand you to read at least the Oracle link.

The SQL part is quite easy:

1) You need to create a Table with the Highscore information you need. Create Table - W3Schools

2) You need to create entries Insert entries - W3Schhols

3) Read entries / Highscores SELECT - W3Schhols

MortalFool
  • 1,065
  • 2
  • 14
  • 26
  • I have created the table, i just want to enter entries to be inserted when the score is higher than the current high score. – user3673033 May 26 '14 at 07:44