0

Super new to JSP and just trying to access mysql

public static Connection connect(){
try{
    Class.forName("con.mysql.jdbc.Driver").newInstance();
    return DriverManager.getConnection("jdbc:mysql://localhost/db_name", "root", "******");
}catch(Exception e){
    throw new Error(e);
}   

I continue to get this error: ClassNotFoundException: con.mysql.jdbc.Driver

I understand that I need to change the "classpath" and/or that I need to move the mysql-connector jar file to tomcat7/lib but I have 2 problems:

  1. I have absolutely no idea what the classpath is and how to change it using ubuntu and dreamweaver.
  2. my tomcat7 folder doesn't have a lib directory. I don't know where to put the jar file.
user2998553
  • 1,278
  • 1
  • 8
  • 8

2 Answers2

0

Assuming that you have the jar on your classpath, I think that yuor problem is a misspelt fully qualified classname: con.mysql.jdbc.Driver should be com.mysql.jdbc.Driver

Romski
  • 1,912
  • 1
  • 12
  • 27
0

This seems to be a J2EE application. Within the project should be WEB-INF directory. Within that directory, create (if necessary) a lib directory and place the mysql-connector.jar file within lib. This will cause the driver to be placed on the runtime classpath.

Also as Romski pointed out change the class name to com.mysql.jdbc.Driver, however I'm not even sure this code is necessary with newer version of the driver and JDBC. If your using Tomcat7 the JDK is most likely recent enough to not require this code. See: https://stackoverflow.com/a/12933246/714969

Community
  • 1
  • 1
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • im not using eclipse, I just made a .jsp file and placed it in the tomcat7/webapps/ROOT folder. Should i make a WEB-INF folder? and if so, where should I put it. I am NOT using eclipse btw. – user2998553 Jan 08 '14 at 01:50
  • also removing the class.forname changes the error to: No suitable driver found for jdbc:mysql://localhost/bitzooka – user2998553 Jan 08 '14 at 01:55
  • @user2998553 You can make a `WEB-INF\lib` folder in the root of your application. I think the issues you receive from removing `class.forname` is due to the fact that its not finding the jar on the runtime classpath – Kevin Bowersox Jan 08 '14 at 02:00
  • 'No suitable driver' found means that the jar is not on your classpath. Start with a really basic class with a main method and try to load the driver there first and when working, you can move to a webapp. If you don't know how to set the classpath to include the jar, then checkout the Java tutorials. – Romski Jan 08 '14 at 02:01