Java - 用于MySQL的PHP​​ $ _GET方法的URLConnection

时间:2010-09-01 23:41:06

标签: java php mysql

好的,这有什么问题。我尝试使用MySQL JConnector for Java,人们说没有,因为它可以包含你的applet细节,所以我告诉他们我使用PHP脚本使用PHP $ _GET方法URL。他们说会没事的。但是,我发现了两个问题。

1。)他们很慢。 URLConnection发生至少需要4-5秒。即使我把它指向localhost。

2。)这是很多代码。也许我只是做错了?

这是我的 - 有效!

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

以下是回复:

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

它有效,但它很慢而且笨重 - 我该如何解决这个问题呢?

2 个答案:

答案 0 :(得分:1)

我会确保您的MySQL数据库具有正确的索引设置。没有正确的MySQL indexes设置可以减慢您的代码速度!

但是如果你的PHP代码导致了这个问题,你应该把它作为appose发布到Java端。由于PHP代码处理数据库,我的赌注是保持的位置。

答案 1 :(得分:1)

问题是URLConnection实际上很慢。您可以尝试将Proxy.NO_PROXY参数传递给openConnection,但它不会使其超高速。 (每次通话1到3秒,所以你还在用很多时间) HttpConnection的替代方案可以是Jakarta Commons HttpClient

更新你的等级的网址实际上也很有趣:如果有人发现这一点,你将会有很多骗子使用你的游戏。