我正在使用Struts2开发一个web应用程序,试图在本地的MySQL数据库中插入一些值。
用于连接和访问数据库的代码在控制台应用程序中运行良好。
但是当我在Struts中运行相同的代码时,它会给出java.lang.ClassNotFoundException: com.mysql.jdbc.Driver异常。
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "testdatabase";
String driver = "com.mysql.jdbc.Driver";
try
{
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,"root","root");
Statement statement=conn.createStatement();
System.out.println("HelloWorld.execute()");
int i= statement.executeUpdate("INSERT INTO testTable VALUES('15','Lucky')");
System.out.println("res: "+i);
}
catch(Exception e)
{
System.out.println(e);
//setMessage(e.getMessage());
}发布于 2012-04-01 03:09:54
该错误意味着它无法在类路径上找到JDBC驱动程序。你有JDBC驱动程序吗?您需要具有MySQL JDBC驱动程序。如果你没有,可以从MySQL网站下载。
如果您确实有驱动程序或已下载驱动程序,请确保它位于正确的位置。它需要放在你的类路径上。这通常意味着两个地方中的一个
WEB-INF/lib文件夹下的Struts应用程序,<TOMCAT FOLDER>/lib中
发布于 2012-04-01 01:43:00
https://stackoverflow.com/questions/9958049
复制相似问题