0

我的 sqlConnexion 与 android 存在性能问题。问题是,我为我在服务器上发出的每个请求打开连接。我宁愿在用户第一次登录时打开它一次。

这是一些代码:

    public void onClick(View v) {
    // Perform action on click
    new Thread(new Runnable() {
        public void run() {
            Statement statement;
            try {
             *//This would be better to instantiate connexion at first...*
                connexion =   DriverManager.getConnection(url,     "login",    
                        "pass");//Not true login of course...
                statement = connexion.createStatement();
                ResultSet resultat = statement
                        .executeQuery("SELECT name  FROM users;");

                while (resultat.next()) {
                    resultId = resultat.getString("name");
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }).start();

如果我将它DriverManager放在一个方法中,我会从服务器收到一个错误。在另一堂课上,我得到NullPointerException.

我知道使用 json 解析器可能是一种更好的方法,但我开始学习了,Java/Android 也足以让我现在头疼......

4

4 回答 4

0

将连接代码移动到Singleton或简单地将连接存储在单独类的静态属性中。更多详细信息,请参阅存储应用程序全局数据

于 2013-06-05T17:53:59.030 回答
0

执行以下操作:当您声明 connexion 时将其分配为 null,并在 try catch 检查 connexion 是否为 null。如果你得到null,则获取新连接,否则不会

connexion = null; 

//In try block
if( connexion == null)
     connexion = DriverManager.getConnection(url, "login", "pass");
于 2013-06-05T17:58:38.617 回答
0

不要试图重用连接!!!对于与数据库的每个逻辑交互,获取一个连接、语句或其他任何内容,并确保在函数结束之前将它们全部关闭。

但是,为了提高性能,请使用连接“池”,例如 Commons BasicDataSource。使用它对您来说是不可见的。您可以正常使用连接和语句,但 BasicDataSource 将汇集连接(和 PreparedStatements)以提高性能。

http://commons.apache.org/proper/commons-dbcp/

于 2013-06-05T18:45:20.167 回答
0

谢谢大家的帮助 !

我设法解决了这个问题。我做了什么,如果这可以帮助我在这里发布的人:

首先,为我创建另一个类,我在其中创建 DataSource 的 Driver() :

public class Driver implements DataSource {

    public static Connection connection = null;
    public static final String url = "jdbc:mysql://url/dataBaseName";
    protected String user = "userName";
    protected String passwd = "password";
        //Andoid related, haven't tested the log so, i don't know if this work.
    private String state = Environment.getExternalStorageState();

    public Driver(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        PrintWriter logWriter = null;

        if (Environment.MEDIA_MOUNTED.equals(state)) {
            // We can read and write the media
            try {
                logWriter = new PrintWriter(state + "/com.me.appName/Logfile.log");
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return logWriter;
    }

    @Override
    public int getLoginTimeout() throws SQLException {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void setLogWriter(PrintWriter arg0) throws SQLException {
        // TODO Auto-generated method stub
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            // We can read and write the media
            try {
                DriverManager.setLogWriter(new PrintWriter(
                        state + "/com.me.appName/Logfile.log"));
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean isWrapperFor(Class<?> arg0) throws SQLException {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public <T> T unwrap(Class<T> arg0) throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Connection getConnection() throws SQLException {
        if (connection != null) {
            System.out.println("Cant create  Connection");
        } else {
            connection = DriverManager.getConnection(
                    url, user, passwd);
        }
        return connection;
    }

    @Override
    public Connection getConnection(String userName, String password)
            throws SQLException {
        // TODO Auto-generated method stub
        if (connection != null) {
            System.out.println("Cant craete a Connection");
        } else {
            connection = DriverManager.getConnection(
                    url, userName,
                    password);
        }
        return connection;
    }

}

最后在我的主要课程中:

在 onCreate() 我实例化驱动程序然后我可以毫不拖延地提出所有需要的请求:)

public void onClick(View v) {
                // Perform action on click
                new Thread(new Runnable() {
                    public void run() {
                        try {
                            Statement statement;
                            //The .getConnection is where i handle the DriverManager.
                            connexion = driver.getConnection();
                            statement = connexion.createStatement();

                            ResultSet resultat = statement
                        .executeQuery("SELECT name  FROM users;");

                        while (resultat.next()) {
                            resultId = resultat.getString("name");
                        }
                        driver.setLoginTimeout(5);
                    } catch (SQLException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }

                }
            }).start();

这真的很有用。

再次感谢大家,对不起,我不能投票给你的答案,希望有一天能帮助你!

于 2013-06-06T10:14:49.653 回答