0

我正在尝试使用 jdbc 连接从数据库中获取结果。我的结果集由超过 60,000 行组成,然后我将对其进行迭代以构建一个列表,以便与另一个类似大小的列表对象进行比较。问题是虽然这种方法给出了正确的结果,但它非常慢。两个如何加快速度的任何想法?代码示例如下:

public class PerformanceTest {

    //create a connection
    Connection getConnection(String uName, String pwd, String url) throws ClassNotFoundException, SQLException
    {
        Properties info = new Properties();
        info.setProperty("user", uName);
        info.setProperty("password", pwd);
        try
        {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            return DriverManager.getConnection(url, info);
        } 
        catch (ClassNotFoundException e)
        {
            throw e;
        }
        catch (SQLException e)
        {
           throw e;
        }
    }

    //Fetch the records and put them in a map with schema name as key and table name as value
    public List<String> fetchRecords(Connection conn, String sql) throws SQLException
    {
        Statement stmt;
        ResultSet rs;
        String tableName;
        List<String> tableList;
        long startTime;
        int i=0;
        if(conn!=null)
        {
            try
            {
                tableList = new ArrayList<String>();
                stmt = conn.createStatement();
                rs = stmt.executeQuery(sql);
                startTime = System.currentTimeMillis();
                while(rs.next())
                {
                    System.out.println(++i);
                    tableName = rs.getString(1);
                    tableList.add(tableName);
                }

                System.out.println("Running Time: " + (System.currentTimeMillis() - startTime)/1000 + " seconds");
                return tableList;
            }
            catch(SQLException e)
            {
                throw e;
            }
        }
        else
        {
            return null;
        }
     }

    public boolean main() throws ClassNotFoundException, SQLException
    {
        String url = "jdbc:oracle:thin:@xxxxxx:1521:xxxxx";
        Connection conn = getConnection("", "", url);
        String sql = "SELECT  table_name FROM user_tables";
        long startTime;
        boolean result;
        List<String> l1 = fetchRecords(conn, sql);
        List<String> l2 = new ArrayList<String>(l1);
        //l2.add("1");
        startTime = System.currentTimeMillis();
        result = l2.containsAll(l1);
        System.out.println("Running Time: " + (System.currentTimeMillis() - startTime)/1000 + " seconds");
        return result;
    }
}
4

0 回答 0