在线程上运行带有返回值的查询

时间:2014-06-19 03:15:42

标签: java sql multithreading bukkit

public String getCurrentGuild(final String playername) {

        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    conn = DriverManager.getConnection(DB_URL, USER, PASS);
                    DatabaseMetaData dbm = conn.getMetaData();
                    stmt = conn.createStatement();

                    ResultSet set = stmt.executeQuery("SELECT CurrentGuild FROM Players WHERE Username='" + playername + "';");
                    if (set.next()) {
                        guildss = set.getString("CurrentGuild");
                    }
                } catch (SQLException se) {
                    //Handle errors for JDBC
                    se.printStackTrace();
                } catch (Exception e) {
                    //Handle errors for Class.forName
                    e.printStackTrace();
                } finally {
                    //finally block used to close resources
                    try {
                        if (stmt != null) {
                            conn.close();
                        }
                    } catch (SQLException se) {
                    }// do nothing
                    try {
                        if (conn != null) {
                            conn.close();
                        }
                    } catch (SQLException se) {
                        se.printStackTrace();
                    }//end finally try
                }//en

            }
        });
        t.start();
        try {
            t.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return guildss;
    }

返回值在此行返回null:

if(!gs.getCurrentGuild(p.getName()).equalsIgnoreCase("-"))

无论如何我能更快地得到结果吗?

1 个答案:

答案 0 :(得分:0)

在您的查询中尝试Like而不是=

这样的事情 -

ResultSet set = stmt.executeQuery("SELECT CurrentGuild FROM Players WHERE Username like '" + playername + "';");