Question

i am getting bellow error

[Microsoft][ODBC SQL Server Driver]Connection is busy with results for another hstmt

my code

private static final String DRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static final String URL = "jdbc:odbc:sql2008";
private String[] entity = {"TABLE","VIEW"};
private ResultSet tables,columns,resultSet;

public void getData() throws SQLException, ClassNotFoundException{
    Class.forName(DRIVER);
    Connection  connection=DriverManager.getConnection(URL,USERNAME,PASSWORD);
    DatabaseMetaData data = connection.getMetaData();
     resultSet =  data.getCatalogs();

     while (resultSet.next()) {
         String dbName = resultSet.getString("TABLE_CAT");
        if(dbName.toString().equals("db")){
            tables=data.getTables(dbName, "%", "%", entity);
            while (tables.next()) {
                           System.out.println(tables.getString("TABLE_NAME"));
            }
        }
    }
}

URL is DSN connection(connction using dsn name ) and I am getting this error while trying to fetch tables which is again ResultSet but its work fine in DSN-less connection that is directly (using address)

Was it helpful?

Solution

You're iterating over two resultsets at the same time:

  1. while (resultSet.next()) {
  2. while (tables.next()) {

According to the error message, this is not allowed.

You could fix it by putting the result of the first iteration in a List<String> databaseNames and then iterating over that list to list the tables.

Alternatively, if you're using SQL Server 2005, you might want to try enabling a feature in SQL Server called "Multiple Active Result Sets".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top