Hello i need to save score on application close and when will start up to show the last score is entered... i have not idea how do i start i have searched on google but nothing found which can help me, please show me some ways/codes of doing it.
Thanks ! :)

Dani AI

Generated

Short summary for : the crash (java.lang.ClassNotFoundException for com.mysql.jdbc.Driver) and the approach of embedding a MySQL connector inside the APK are both common signals that this path won't work well. As hinted, direct DB access from a client app is fragile and unsafe; as noted, local storage is the simplest way to show a value quickly. Combine the two: cache the last-known score locally and fetch the canonical value from your server via a tiny web API.

Recommended flow (practical, secure, and robust)

  • Put all MySQL access on your server (PHP/Node/Python/Java). Expose a simple HTTPS endpoint that returns the score as JSON.
  • In the app, read the cached score immediately and display it.
  • Start a background HTTP request to the server, parse JSON, update the UI, and overwrite the local cache.
  • Save the current score to local storage whenever the app is paused/stopped so the next startup shows the last value instantly.

Example: store/load using SharedPreferences (save in onPause/onStop, load in onCreate)

SharedPreferences prefs = getSharedPreferences("game_prefs", MODE_PRIVATE);
int stefan = prefs.getInt("score_stefan", 0);
int bojan  = prefs.getInt("score_bojan", 0);
stefanRes.setText(String.valueOf(stefan));
bojanRes.setText(String.valueOf(bojan));

// save when leaving
prefs.edit()
     .putInt("score_stefan", currentStefanScore)
     .putInt("score_bojan", currentBojanScore)
     .apply();

Troubleshooting & cautions

  • Never hardcode DB credentials or put a DB driver in the APK; ClassNotFoundException often means the driver jar is incompatible or not on the runtime path, and even if added it creates security and size problems.
  • Do all network I/O off the main thread (modern options: Retrofit/OkHttp, coroutines, WorkManager). Check Logcat for the full stack trace while debugging.
  • Use HTTPS in production and validate server responses before updating the cache.

Recommended Answers

All 5 Replies

Actualy the score is taken from mysql database and all i want to make is to read the score on application start up, im sorry i forgot to write this..

I have this code but it doesnt work application is crashing or not showing the results...

try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://DATABASE", "USERNAME", "PASSWORD");
            Statement stmt = conn.createStatement();
            ResultSet rset = stmt.executeQuery("select * from BackgammonResults");
            while (rset.next()) {
                asd = rset.getInt("Stefan"); // local variable
                asd1 = rset.getInt("Bojan"); // local variable

            }
            stefanRes.setText(asd);
            bojanRes.setText(asd1);
        } catch (Exception e) {
            e.printStackTrace();
        }

I'm not a huge fan of connecting a user-end application to a remote database like that. I would suggest making a TCP server that does the database access for you.

What does the error look like?

Theres much error lines i cannot copy and paste it all that.. but the first line is: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver but i have the MySQL-Connector...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.