For above code, when the servlet is called for the first time, exception is thrown that No suitable driver found. But It runs and connection established successfully for the second time nd onwards.

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at controller.FetchSuggestion.doGet(FetchSuggestion.java:44)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
null
com.mysql.jdbc.JDBC4Connection@1b83048
com.mysql.jdbc.JDBC4Connection@1455d1c
com.mysql.jdbc.JDBC4Connection@1f51e5c

The above code works fine if I remove the comment from Class.forName(), but it won't work without it for first time and able to establish connection for the second time onwards.
FYI : I already have MySQL JAVA connector in my class path.

Registering the DriverClassName is a vital step. Kindly do not skip it.

EDIT: I think I found your problem. Here's the analysis:

The reason your application worked with Class.forName is because you had specifically asked the DriverManager to load this Driver for you.

The reason that your application did not work for the first time is because you did not specify the Driver to be used.

Let me elaborate on the second point:

You are absolutely right in saying that the application should work without Class.forName. But aaahhh... There's a catch.

When you don't specify Class.forName , the DriverManager will try to load as many drivers as it can find and then for any given connection request, it will ask each driver in turn to try to connect to the target URL.

This means that you might have multiple Drivers in your application. The DriverManager found these drivers and tried to establish a connection to the database. The connection was null because the first driver found wasn't the right one. But wait, DriverManager is smart. It knows it could not connect to the database, so now it uses the other driver found. And presto, the connection gets established.

I already have MySQL Connector in class path. My question is Without Class.forName(), why it didn't work for the first time?
–
Vimal BeraOct 1 '13 at 11:17

I fail to understand. Do you mean to ask Why can you connect when you include Class.forName and not when I don't include it..?. Meaning with Class.forName you are able to connect to the DB ..?
–
user2339071Oct 1 '13 at 11:24

Yes,Without Class.forName() it SHOULD work fine. Because DriverManager has capability to search and load the driver by itself. See my stacktrace. First time it returns null. Why?
–
Vimal BeraOct 1 '13 at 11:27

By looking at the source code for java.sql.DriverManager I see that the code for loading “initial” drivers (i.e. not explicitly loaded ones) is triggered by the first attempt to get a driver. If another thread tries to get a driver then, the initialize() method does not wait for the ongoing initialization but returns immediately:

So it’s a race condition. The second thread will skip the initialization without finding a driver but surely complete before the first thread as not doing the initialization takes lesser time. So it appears as if the first attempt fails but it is the second.