0

There is a VERY similar question to mine but in my case I don't have any duplicate jars in my build path, so the solution does not work for me. I've searched google for a couple of hours now, but none of the solutions I've found there actually resolve my issue. I'm creating a web site with some database connectivity for a homework. I'm using a MySQL database, developing in Eclipse and running on linux. I keep getting java.lang.ClassNotFoundException: com.mysql.jdbc.Driver with the following JSP code:

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>      
<%@page import="java.sql.*" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01  
Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>query the Leader(mysql)   </title></head><body>   
<%
Connection conn = null;
Statement stat = null;
ResultSet rs = null;

//加载mysql数据库驱动类
Class.forName("com.mysql.jdbc.Driver").newInstance();
//数据库连接URL
String url = "jdbc:mysql://localhost/fams_db";
//数据库用户名
String user="root";
//数据库密码
String pwd="123456";
conn = DriverManager.getConnection(url,user,pwd);
stat=conn.createStatement();
String sql="SELECT * FROM Leader";
rs=stat.executeQuery(sql);
    out.print("<table border=2>");
    out.print("<tr>");
    out.print("<th width=100>"+"LeaderID");
    out.print("<th width=100>"+"LeaderName");
    out.print("<th width=100>"+"LeaderPwd");
    out.print("<th width=100>"+"Type");
    out.print("</tr>");
    while(rs.next()){
    out.print("<tr>");
    out.print("<td>"+rs.getInt("LeaderID")+"</td>");
    out.print("<td>"+rs.getString("LeaderName")+"</td>");
    out.print("<td>"+rs.getString("LeaderPwd")+"</td>");
    out.print("<td>"+rs.getInt("Type")+"</td>");
    out.print("</tr>");
}
if(rs!=null)
{
    rs.close();
}
if(stat!=null)
{
    stat.close();
}
if(conn!=null)
{
    conn.close();
}
out.print("</table>"); %>
</body> </html>    

I can't figure out why! Here is what I did:

1).Download mysql-connector-java-5.1.27-bin.jar.

2) tomcat url:/usr/local/tomcat7 eclipse url:/usr/local/eclipse jdk url:/usr/local/java

3).Opened the project properties in Eclipse.

4).run the jsp file Every time I attempt to use the servlet I get the same error regardless if I have the jar in there or if I don't. Could you help me figure this out? Where should I put the mysql-connector-java-5.1.27.jar???

user1288145
  • 91
  • 1
  • 11
  • why did you even do 2) ? You should not be touching the runtime to get stuff on the classpath. I seriously wonder what resource is telling people to dump stuff in the ext folder, that resource needs to be destroyed. – Gimby Dec 03 '13 at 13:23
  • set up tomcat in folder:/usr/local/tomcat7 and eclipse in folder:/usr/local/eclipse and jdk in folder:/usr/local/java/ – user1288145 Dec 03 '13 at 13:41
  • I delete the .jar in folder /jre/lib/ext and information in /etc/profile,but it still not work, what should I do? – user1288145 Dec 03 '13 at 13:58

4 Answers4

3

Copy the .jar file in the WEB-INF\lib folder of your project.

Sergi
  • 579
  • 3
  • 14
  • That works if the application creates connections itself; it a server datasource is used, the jar needs to be put on the classpath of the server itself and not the web application. – Gimby Dec 03 '13 at 13:24
  • Looks like he/she is connecting through the application. So maybe my solution could work. – Sergi Dec 03 '13 at 13:28
  • I copy .jar file in the WebContent/WEB-INF/lib folder of my project and I got erre:HTTP Status 500 - javax.servlet.ServletException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver – user1288145 Dec 03 '13 at 13:49
  • Error 500 is from your server. Delete all the .jar files you copied in the bin folder of your server or the JDK/JRE runtime folder. Maybe that is the problem. I did several JSP applications that connect to an Oracle DB and I never had a problem. Also check the build path too (right click on your project -> Build Path -> Configure Build Path) and add only the .jar files copied in the WEB-INF/lib folder. – Sergi Dec 04 '13 at 09:04
0

I am guessing you are using the tomcat server, you can but it in the lib folder in there to make all your projects in eclipse worth with sql or you can put it in the in the Web content/WEB-INF/lib, this wont work if you have more than one project.

Ashish
  • 1,943
  • 2
  • 14
  • 17
0

Either use the -cp flag:

java -cp /path/to/somefolder/.jar:/path/to/otherfolder/.jar com.YourMainClass

Or add a Class-Path: header to your jar's manifest

or

If you ever encounter a problem like this and try to solve it inside eclipse, go to

eclipse -> window -> preferences -> java -> buildpath -> classpath

and add the

"mysql-connector-java-5.1.22-bin.jar"

as new variable. Name it whatever you want. Hope this will help you helps.

Ashish
  • 1,943
  • 2
  • 14
  • 17
0

I solved it.I got wrong jdk directory.

user1288145
  • 91
  • 1
  • 11