0

I've recently been have lots of db connection problems. The current situation is, the driver has been updated to mysql-connector-java-8.0.22.jar for linux.

The code was working and broke when I did a clean install of the OS to 20.x this caused too many problems and I reverted to 18.0.4

The class load is :

   try { // The newInstance() call is a work around for some
              // broken Java implementations
//Class.forName("com.mysql.jdbc.Driver").newInstance();
Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
        } catch (Exception ex) {
            if (out != null) {
                out.println("<p>Error while defining database driver: " +
                        "Class.forName: " + ex.getMessage());
                out.println("<p>" + ex.toString() + "<p>");
          //  SKYPE_DEBUG.Log(DatabaseInterface.class.getName(), "mysql v[0] = "+ "\n",ex);
            }
       
        }

The actual connection code is :

  query = "jdbc:mysql://localhost/bloddpressure?user=" + dbUserid + "&password=" + dbPassword;
        
         //  query = "jdbc:mysql://192.168.0.12:3307/?user=" + dbUserid + "&password=" + dbPassword;
        

Either of the query options give the same error.

When using:

Class.forName("com.mysql.jdbc.Driver").newInstance();

It started throwing the following error on the line

conn = DriverManager.getConnection(query);          

The exception logged is:

 Timestamp
    
Nov 12, 2020 08:38:50.273
Log Level
    
SEVERE
Logger
    
Name-Value Pairs
    
{levelValue=1000, timeMillis=1605170330273}
Record Number
    
631
Message ID
    
Complete Message
    
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

I have changed the name of the driver to :

//    Class.forName("com.mysql.jdbc.Driver").newInstance();
Class.forName("com.mysql.cj.jdbc.Driver").newInstance();

But still get the same exception as above. Either call creates the same exception.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Jeff Baker
  • 1
  • 1
  • 5
  • It does also throw : type Exception report messageInternal Server Error descriptionThe server encountered an internal error that prevented it from fulfilling this request. exception java.lang.NoClassDefFoundError: Could not initialize class com.mysql.cj.util.Util – Jeff Baker Nov 12 '20 at 09:34

1 Answers1

1
  1. It is not an exception. It is a warning message.

  2. A "deprecation" warning tells you that you shouldn't be doing something like that anymore. You should read it and pay attention to what it actually says.

  3. In this case, it is telling you that (under most circumstances) you should not use Class.forName(SOME_CLASS_NAME) to load JDBC drivers. This hasn't been necessary for a very long time.

  4. Even you do use Class.forName, you shouldn't call newInstance() on the class. This comment:

     // The newInstance() call is a work around for some
     // broken Java implementations
    

    is out of date. It might have been true 20 years ago ...

  5. The so-called "connection code" that you showed us is not that at all. It is just generating a connection URL.


I think that the best thing you could do is to read the JDBC Basics > Establishing a Connection from the Oracle Java Tutorial which describes the 2 correct ways to get a JDBC connection. Then rewrite your code to establish JDBC connections one of those ways.

Among other things, you will get rid of fragile references to specific JDBC driver classes in your code, and get rid of the pesky deprecation message from your logs.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216