我正在開發一個使用Struts 2的Web應用程序,我試圖在我的本地MySQL數據庫中插入一些值。當在Strcat中使用JDBC在Tomcat 7中時遇到問題
用於連接和訪問數據庫的代碼在控制檯應用程序中正常工作。
但是,當我在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());
}
謝謝..其工作正常... – Chethu2288 2012-04-01 17:59:54