【发布时间】:2012-03-31 17:24:21
【问题描述】:
我正在使用 Struts 2 开发一个 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());
}
【问题讨论】:
标签: mysql jdbc struts2 tomcat7