Hello,
I have built a little web application and it works without any problems. In the next step I tried to add a database connection but I have already worked on it for at least one day and I could not fix the problem with the database connection.
I downloaded the MySQL Server 5.0 and installed it, furthermore I have downloaded the jdbc driver mysql-connector-java-3.1.11.zip and extracted it to the MySQL Server 5.0 directory. Furthermore I have added under system settings - system - etended - system variables the whole path to the driver -> C:\Programme\MySQL\MySQL Server 5.0\mysql-connector-java-3.1.11\mysql-connector-java-3.1.11-bin.jar to the CLASSPATH systemvariable.
Then I have startet the SQL Server 5.0 with mysqld --console from the command line.
Furthermore I have added under Project-Properties-Java Build Path - Libraries - Add External JARs in Eclipse the mysql-connector-java-3.1.11-bin.jar to my Struts WebProject.
To test the whole thing I have added to the existing test-database from mysql a table named owner and wanted to test the database connection in the Action-class with the following statements - I quote the whole execute method of the OwnerAction class:
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
OwnerForm ownerForm = (OwnerForm) form;
String greet = ownerForm.getGreet();
String name = ownerForm.getName();
request.setAttribute("name", name);
request.setAttribute("greet", greet);
String address = ownerForm.getAddress();
String email = ownerForm.getEmail();
int tel = ownerForm.getTel();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","sa");
try {
//conn = dataSource.getConnection();
stmt = conn.createStatement();
int id = 0;
rs = stmt.executeQuery("select max(id) as counter from owner");
while(rs.next()){
id = rs.getInt("counter");
}
id += 1;
stmt.executeUpdate("insert into owner values("+id+", '"+greet+"', '"+name+"', '"+email+"', '"+address+"', "+tel+")");
rs.close();
stmt.close();
conn.close();
}
catch(SQLException e){
throw new SQLException("database error");
}
// Forward control to the specified success target
return (mapping.findForward("success"));
}
But when I execute the WebApplication and the Action will be called the following error message appears in the browser:
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Servlet execution threw an exception
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
root cause
java.lang.NoClassDefFoundError: org/gjt/mm/mysql/Driver
com.asprise.struts.action.OwnerAction.execute(OwnerAction.java:72)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5 logs.
It must depend with anything that the driver was not found but I have defined it in the CLASSPATH variable and added it to the Web-Project in eclispe.
Has anybody an idea what I have else made wrong or have forgotten??:(:(:(
thanks in advance
lg
pat