我正在尝试使用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;
}
}