• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Database connection servlet

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm running a sample from a book that uses a servlet to connect to MySQL, the relevant code is:


Even if I try a simple SQL statement like SELECT * FROM User; I get the error message


SQL result:
Error executing the SQL statement:
No suitable driver found for jdbc:mysql://localhost:3306/murach


I am using a type 4 connection. According to the book all you need to do is download the Connector/J from MySQL (mysql-connector-java-5.0.8-bin.jar)
put it into the jdk/jre/lib/ext directory and the connector will be available to any app (in java 1.6 and up) and will be loaded automatically. I even added the jar
file to the project, but that had the same result.

Anyone have any answers?
Thanks in advance

Mike
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Before you get a Connection, you need to load the Driver class.Try this:
Class.forName("com.mysql.jdbc.Driver");
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

According to the book all you need to do is download the Connector/J from MySQL (mysql-connector-java-5.0.8-bin.jar)
put it into the jdk/jre/lib/ext directory


Not really recommended. You'd better put the jar in your WEB-INF/lib directory.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, this part is servlet related: if you are eventually going to use container-managed connection pooling (recommended) with Tomcat, the driver jar needs to be in Tomcat's lib folder.
 
Mike Tabak
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all,

I did try Class.forName("com.mysql.jdbc.Driver"); and it started working. I think this initiates the driver instead of loading it? Anyway, I will also try putting it int Tomcats lib folder.
 
Ranch Hand
Posts: 530
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jason cooper wrote:Before you get a Connection, you need to load the Driver class.Try this:
Class.forName("com.mysql.jdbc.Driver");



This is no longer needed, since JDBC 4.0 (Java 6). Just include Connector/J jar file to classpath and the driver manager will load the suitable driver automatically.
 
Mike Tabak
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To Jason:

I did read that in java 6+ all you need to do is put the Connector/J jar in the classpath and it loads automatically. I did so (in NetBeans) but this didn't work. Using the forName() worked, I still need to try putting it in Tomcat's WEB-INF/lib directory. I guess my question to you is does the forName() call load the Driver or just initialize it? I read it initializes it which would mean it is being loaded automatically.

Thanks
Mike
 
Mike Tabak
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried putting the Connector/J jar file in the jdk/jre/lib/ext, in the library path of my project and in Tomcat's WEB-INF/lib directory. None of these worked. I still needed the Class.forName("com.mysql.jdbc.Driver"); statement for the app to work. It should load by itself, I don't understand why it isn't. I read somewhere that the Class.forName("..."); initializes a class, could it
be that it's loading but not initializing?

Anyway, I'll have to keep it in for now until I find the answer. By the way, I'm running on Windows 7 32 bit Pro. under bootcamp on a MacBook pro. Don't see where that should matter.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic