Cannot connect to mysql via servlet

Posted by JBoy on Stack Overflow See other posts from Stack Overflow or by JBoy
Published on 2011-01-29T07:06:24Z Indexed on 2011/01/29 7:25 UTC
Read the original article Hit count: 162

Filed under:
|
|

Hi all I have been since 2 days trying to figure out why my servlet does not connect to the database MySql. I have mysql installed and working properly and eclipse. Whenever i try to etabilish a connection i get the ClassNotFoundException for the com.mysql.jdbc.Driver, which is actually properly imported, the connector i'm using is the mysql-connector-java5.1.14 added properly as external jar so everything seems fine. here's my code

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

  String dbUrl="jdbc:mysql://localhost:3306/test";
  String username="root";
  String password="";



  try {
   Class.forName("com.mysql.jdbc.Driver").newInstance();
   conn=DriverManager.getConnection(dbUrl);
   System.out.println("Connected!");

  } catch (SQLException e) {
   e.printStackTrace();
   System.out.println("not connected");

  } catch(ClassNotFoundException x){
   x.printStackTrace();

  } catch(Exception e){

   e.printStackTrace();
  }


 }

The stacktrace(part of):

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1645)
 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1491)
 at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:375)
 at java.lang.Class.forName0(Native Method)
 at java.lang.Class.forName(Class.java:164)

i'm following the connection steps from a published java book and also in forums and tutorials i just see the same code, cannot figure out why that Exception comes. On a normal application which does not run on the server the Exception isn't thrown and the connection (in the exact same way) its successfull. Do you have any advice?

© Stack Overflow or respective owner

Related posts about java

Related posts about jsp