0

OK so what's the problem with this. I tried using MySQL JConnector for Java and people say not to because it can comprise your applet details so then I told them I'd use PHP $_GET method URLs using PHP scripts. They said it would be fine. However, I find two problems with that.

1.) They are slow. It takes at least 4-5 seconds for the URLConnection to happen. Even when I point it to localhost.

2.) It's a lot of code. Maybe I'm just doing it wrong?

Here is what I have -- which works!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.Applet;
import java.awt.TextArea.*;
import java.util.*;
import java.net.*;
import java.applet.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class test extends JApplet
{
    public JTextArea c;

    public void init()
    {
        c = new JTextArea();
        add(c);
        c.append("Let's change the level!");
        try
        {
            URL game = new URL("http://localhost/mystikrpg/game.php?act=stats&username=Dan");
            URLConnection connection = game.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            while ((inputLine = in.readLine()) != null)
            {
                String command = inputLine;
                System.out.println(command);
                String[] temp = command.split("\\|");
                c.append("\nYou're Lvl is: " + temp[1]);
            }
            in.close();

            c.append("\nTrying to update level...");
            String newLevel = "777";
            URL newGame = new URL("http://localhost/mystikrpg/game.php?act=updateLvl&username=Dan&lvl=" + newLevel);
            URLConnection levelConnection = newGame.openConnection();
            BufferedReader level_BR = new BufferedReader(new InputStreamReader(levelConnection.getInputStream()));

            URL updateLevelURL = new URL("http://localhost/mystikrpg/game.php?act=stats&username=Dan");
            URLConnection up_lvl_conn = updateLevelURL.openConnection();
            BufferedReader up_lvl_br = new BufferedReader(new InputStreamReader(up_lvl_conn.getInputStream()));
            String getLvl;

            while ((getLvl = up_lvl_br.readLine()) != null)
            {
                String[] newLvl = getLvl.split("\\|");
                c.append("\nYou're NEW Lvl is: " + newLvl[1]);
                // newLvl[1] == newLevel
            }
            c.append("\nLevel update done!");

            level_BR.close();
            up_lvl_br.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

and here is the response:

Let's change the level!
You're Lvl is: 123456
Trying to update level...
You're NEW Lvl is: 777
Level update done!

It works but it's slow and bulky - how do I fix this problem?

2 Answers 2

1

I would make sure your MySQL database has proper indexes setup. Not having proper MySQL indexes setup can slow your code down a ton!

But if it is your PHP code that is causing the issue, you should post that as appose to the Java side. Since the PHP Code deals with the database, my bet is that is where the hold up is.

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

9 Comments

OK here it is. I'll try indexing. Can I index ALL of my databse?
It is not recommended to index the full database. Given you are only using username you should index username and see how that works out for you.
How do i UNindex something then?
OK I found out it's DROP INDEX username ON user;... but ANWYAYS should I just index the level because thats what I am changing?
You can try it, but you want to keep the index on username.
|
1

The problem is that URLConnection is actually pretty slow. You could try passing the Proxy.NO_PROXY parameter to the openConnection, but it won't make it superfast. (1 to 3 seconds per call, so you're still using up a lot of time) An alternative to the HttpConnection could be the Jakarta Commons HttpClient.

It's actually also quite funny that the url's to update your level are left open: if someone finds out about that, you're going to have a whole lot of cheaters using your game.

1 Comment

Besides what else can I do? :\ The MySQL java thing is vulnerable and the URLConnection is slow... so yeah... :|

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.