当前位置: 技术问答>java相关
为什么总是得到“Cannot load driver ‘org.gjt.mm.mysql.Driver’”
来源: 互联网 发布时间:2015-09-28
本文导语: 我的环境是win2000,apache1.3.23+tomcat4.03。我从网上下载了mm.mysql-2.0.4-bin.jar驱动,用他来访问我的mysql数据库,我设置的classpath为;C:j2sdk1.4.0_01libdt.jar;C:j2sdk1.4.0_01libtools.jar;C:j2sdk1.4.0_01libhtmlconverter.jar;C:Program FilesApache Tomcat...
我的环境是win2000,apache1.3.23+tomcat4.03。我从网上下载了mm.mysql-2.0.4-bin.jar驱动,用他来访问我的mysql数据库,我设置的classpath为;C:j2sdk1.4.0_01libdt.jar;C:j2sdk1.4.0_01libtools.jar;C:j2sdk1.4.0_01libhtmlconverter.jar;C:Program FilesApache Tomcat 4.0commonlibservlet.jar;c:j2sdkee1.3.1libj2ee.jar;C:j2sdk1.4.0_01libmm.mysql-2.0.4-bin.jar;c:CoreJavaBook;C:j2sdk1.4.0_01libecs.jar;c:class;C:Program FilesApache Tomcat 4.0lib;C:Program FilesApache Tomcat 4.0classes。
现在的问题是有一个servlet程序,编译为.class后放到正确的位置,通过http://dreamer/examples/servlet/EmployeesTableQuery访问,可他总是得到“Cannot load driver ‘org.gjt.mm.mysql.Driver’”,可我把他改成application后,却能得到正确的结果,那位大虾帮帮我吧!小第向您致敬!!
servlet源程序如下:
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class EmployeesTableQuery extends HttpServlet
{
public void doGet(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, IOException
{
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String driverName = "org.gjt.mm.mysql.Driver";
resp.setContentType("text/html");
PrintWriter outhtml = resp.getWriter();
try
{
Class.forName(driverName);
con = DriverManager.getConnection(
"jdbc:mysql://dreamer/TestDB", "root", "nmgzwrzwd");
stmt = con.createStatement();
rs = stmt.executeQuery(
"select * from employees");
outhtml.println("");
outhtml.println("");
outhtml.println("The Employees Table Query Program
");
while(rs.next())
{
outhtml.println("Employee ID = " + rs.getString(1) +
" Employee Name = " + rs.getString(2) + "
");
}
outhtml.println("");
outhtml.println("");
}
catch(ClassNotFoundException e)
{
outhtml.println("Cannot load driver:" + e.getMessage());
}
catch(SQLException e)
{
outhtml.println("SQLException: " + e.getMessage());
}
finally
{
try
{
if (con != null)
con.close();
}
catch (SQLException ignored)
{}
}
}
}
修改后的application为:
import java.io.*;
import java.sql.*;
public class EmployeesTableQuery
{
public static void main(String[] args)
{
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try
{
Class.forName("org.gjt.mm.mysql.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://dreamer/TestDB", "root", "nmgzwrzwd");
stmt = con.createStatement();
rs = stmt.executeQuery("select * from employees");
while(rs.next())
{
System.out.println(rs.getString(1) + " Employee Name = " + rs.getString(2));
}
}
catch(ClassNotFoundException e)
{
System.out.println("Cannot load driver:" + e.getMessage());
}
catch(SQLException e)
{
System.out.println("SQLException: " + e.getMessage());
}
finally
{
try
{
if (con != null)
con.close();
}
catch (SQLException ignored)
{}
}
}
}
现在的问题是有一个servlet程序,编译为.class后放到正确的位置,通过http://dreamer/examples/servlet/EmployeesTableQuery访问,可他总是得到“Cannot load driver ‘org.gjt.mm.mysql.Driver’”,可我把他改成application后,却能得到正确的结果,那位大虾帮帮我吧!小第向您致敬!!
servlet源程序如下:
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class EmployeesTableQuery extends HttpServlet
{
public void doGet(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, IOException
{
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String driverName = "org.gjt.mm.mysql.Driver";
resp.setContentType("text/html");
PrintWriter outhtml = resp.getWriter();
try
{
Class.forName(driverName);
con = DriverManager.getConnection(
"jdbc:mysql://dreamer/TestDB", "root", "nmgzwrzwd");
stmt = con.createStatement();
rs = stmt.executeQuery(
"select * from employees");
outhtml.println("");
outhtml.println("");
outhtml.println("The Employees Table Query Program
");
while(rs.next())
{
outhtml.println("Employee ID = " + rs.getString(1) +
" Employee Name = " + rs.getString(2) + "
");
}
outhtml.println("");
outhtml.println("");
}
catch(ClassNotFoundException e)
{
outhtml.println("Cannot load driver:" + e.getMessage());
}
catch(SQLException e)
{
outhtml.println("SQLException: " + e.getMessage());
}
finally
{
try
{
if (con != null)
con.close();
}
catch (SQLException ignored)
{}
}
}
}
修改后的application为:
import java.io.*;
import java.sql.*;
public class EmployeesTableQuery
{
public static void main(String[] args)
{
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try
{
Class.forName("org.gjt.mm.mysql.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://dreamer/TestDB", "root", "nmgzwrzwd");
stmt = con.createStatement();
rs = stmt.executeQuery("select * from employees");
while(rs.next())
{
System.out.println(rs.getString(1) + " Employee Name = " + rs.getString(2));
}
}
catch(ClassNotFoundException e)
{
System.out.println("Cannot load driver:" + e.getMessage());
}
catch(SQLException e)
{
System.out.println("SQLException: " + e.getMessage());
}
finally
{
try
{
if (con != null)
con.close();
}
catch (SQLException ignored)
{}
}
}
}
|
在系统classpath中指定了mm.mysql-2.0.4-bin.jar,所以application没问题。但你的tomcat中并未取到该classpath。所以找不到。把该jar拷到tomcat中lib下就没问题了。
|
mm.mysql-2.0.4-bin.jar还应该拷贝到tomcatlib下面
|
还是环境变量的问题,把驱动程序加如classpath了吗?
还有就是按楼上的方法。
还有就是按楼上的方法。