0

I am getting the error when compiling my project-

Exception in thread "main" java.lang.StackOverflowError
    at sun.reflect.Reflection.getCallerClass(Native Method)
    at java.lang.ClassLoader.getCallerClassLoader(Unknown Source)
    at java.lang.Class.forName(Unknown Source)
    at testpackage.DriverManager.getConnection(DriverManager.java:14)
    at testpackage.DriverManager.getConnection(DriverManager.java:20)
    at testpackage.DriverManager.getConnection(DriverManager.java:20)

Here is my first file code-

package testpackage;

import java.sql.*;

import javax.swing.JOptionPane;

class DriverManager {
       static Connection dbConnection = null;

       public static Connection getConnection(String String_url, String USER, String PASS) throws SQLException 
       {
          try {

            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            //JOptionPane.showMessageDialog(null, "driver load successfully"); 
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            //JOptionPane.showMessageDialog(null, "driver load failed"); 
        }
          dbConnection = DriverManager.getConnection(String_url,USER,PASS);
          return dbConnection;
       }

    } 

I called this method in another file-

package testpackage;
import java.awt.Rectangle;
import org.openqa.selenium.firefox.FirefoxDriver;
public class testclass {
public static void main (String[] args) throws Exception
{   
DriverManager Connection_getConnection = new DriverManager();
        Connection_getConnection.getConnection("database string url","username","password");
}
}

Note- I have used alert to debug the issue and find function calls recursively because i am getting alert one after one.

user3274718
  • 3
  • 1
  • 4
  • 1
    Why are you trying to reinvent a wheel that seems to do the exact same thing as the original implementation? – Paul Samsotha Feb 07 '14 at 08:16
  • possible duplicate of [What is a stack overflow error?](http://stackoverflow.com/questions/214741/what-is-a-stack-overflow-error) –  Mar 27 '14 at 05:50

4 Answers4

1

Your DriverManager.getConnection() method consists in calling DriverManager.getConnection(), which consists in calling DriverManager.getConnnection(), ...

That's because you chose to name your class and method the same way as the standard java.sql.DriverManager.getConnection().

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

It looks like you're calling:

dbConnection = DriverManager.getConnection(String_url,USER,PASS);

before ever returning a value, and since that is the method you're currently in you are recursively calling this method causing an infinite loop.

The getConnection(...) method is calling the getConnection(...) method which will never end and thus the StackOverflowError

leigero
  • 3,233
  • 12
  • 42
  • 63
0

The code will end up in an infinite loop. !!! That's why the your stack get filled.!!

Fix :

Remove the

DriverManager.getConnection(String_url,USER,PASS);

called from the getConnection function will again call the same function. So remove that from getConnection function

Dileep
  • 5,362
  • 3
  • 22
  • 38
0

Your call

dbConnection = DriverManager.getConnection(String_url,USER,PASS);

creates an endless recursion. You are importing java.sql.* but nonetheless your call calls getConnection() in your class.

Change to

dbConnection = java.sql.DriverManager.getConnection(String_url,USER,PASS);
Dirk Lachowski
  • 3,121
  • 4
  • 40
  • 66