6

I have read all possible answer here and here and here. I believe I have a problem somewhere else and it may useful to others also.

I have a Java Servlet and it was executing well in eclipse IDE but later I shifted the project to Netbeans. I imported MySQL JDBC driver in Libraries and using the code below for connection

connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/bolsms", "root", "@#$5869@#$");

but I am getting this error No suitable driver found for jdbc:mysql://localhost:3306/bolsms

I looking for answer for two days and community will appreciate that I am not repeating the question.

Edit 1:

MySQL JDBC driver in Libraries Folder

Community
  • 1
  • 1
Gaurav Agarwal
  • 18,754
  • 29
  • 105
  • 166

4 Answers4

6

When you use it in Tomcat you have to explicitly load jdbc driver. I experienced the same error in a servlet in Tomcat. And adding Class.forName("com.mysql.jdbc.Driver"); solve it.

The other thing I think, is to put the jar file in your WEB-INF/lib, then in your project properties, in Libraries, add the JAR you store in WEB-INF/lib.

To create the lib directory, right click on WEB-INF and choose New -> Folder... If you don't see Folder choose Other then in the new windows choose Other again and Folder.

The Tomcat / JDBC issue is referenced here for more information.

alain.janinm
  • 19,951
  • 10
  • 65
  • 112
  • I tried loading class with Class.forName("com.mysql.jdbc.Driver") but it does not work. Also I quote from Murach's Java Servlet & JSP (IInd Edition) Page 445 **With JDBC 4.0, the database drive is loaded automatically. This feature is known as automatic driver loading.** There is no WEB-INF/lib folder in Netbeans. I reiterate the problem is specific to Netbeans IDE, it was working fine on Eclipse. – Gaurav Agarwal Apr 10 '12 at 13:59
  • 2
    Once again JDBC is loaded automatically yes but not (always) in Tomcat. And my solution works on Netbeans (I don't use Eclipse). If you made a Tomcat project you have a WEB-INF directory for sure. Create the lib directory yourself and add the jar. It's how I do on Netbean. See this link https://issues.apache.org/bugzilla/show_bug.cgi?id=48214 – alain.janinm Apr 10 '12 at 14:17
  • 1
    Thanks. I will appreciate if you could edit the answer to include creation of lib folder and bug link in your answer. – Gaurav Agarwal Apr 10 '12 at 14:31
1

Include the following piece of code in your code:

Class.forName("com.mysql.jdbc.Driver");//load driver
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/bolsms", "root", "@#$5869@#$");//connect to the database
mykey
  • 575
  • 2
  • 10
  • 24
0

you could also import it using the jsp tag <%@page import='com.mysql.jdbc.*'%> or the usual way Class.forName("com.mysql.jdbc.Driver").newInstance(); in the try catch of the jsp page. Good luck

0

Check to make sure that your mysql-connector-java-(version)-bin.jar is here:

C:\Murach\Servlets and JSP\jsp2_allfiles\servlet_jsp\netbeans\ex_starts\ch14sqlGateway\build\web\WEB-INF\lib

Also check to see if it is in here:

C:\Murach\Servlets and JSP\jsp2_allfiles\servlet_jsp\netbeans\ex_starts\ch14sqlGateway\web\WEB-INF\lib

If it's not, paste your mysql driver jar into those directories and see if that works.

After that, make sure you're explicitly loading the driver:

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

JDJ
  • 4,298
  • 3
  • 25
  • 44