I'm new to JSP. I'm trying to do a basic program of JSP & MySQL connectivity using sublime and xwampp. On running the login.jsp page, I get the following:
-
do not use JDBC directly in a jsp. Learn about 3Tier achitecture – Jens Aug 31 '18 at 10:23
3 Answers
A ClassNotFoundException
thrown by your web application means that a class you referenced was not found in the runtime classpath. In your case, that class is com.mysql.jdbc.Driver
. This is a bigger risk when you load a class explicitly by name
at runtime, because the Java compiler cannot then check whether the wanted class is present. Of course, for JSP, compile time is not necessarily well distinguished from runtime.
Of course, you cannot just name a class and expect Java to automagically locate it in some unspecified place, retrieve it, and load it. You need to include the needed class and all its dependencies in your web application, or among the classes that your application server (i.e. Tomcat) provides to every web application.
For a third-party class such as the one in question, the usual approach is to obtain one or more Jar files containing the class and its dependencies, and to drop them in the lib/
directory of your web application's War file or deployment directory. Make sure that their ownership and permissions allow the application server to read them. You can find the needed Jar file at MySQL's web site. You're looking for their "Connector/J".
There is a separate issue of how to ensure that your build / packaging system automatically includes the needed Jar in your War or otherwise deploys it, but you've not presented anything in the question that allows us to address that.

- 160,171
- 8
- 81
- 157
Possible errors:
1) You do not have access to the class com.mysql.jdbc.driver. You need to import it in Maven: https://mvnrepository.com/artifact/mysql/mysql-connector-java/5.1.6
2) You are accessing to the request directly in JSP. As @Jens mentioned, this is not a good practice.
I guess you may have some code which actually render the JSP, something like this:
@Controller
public class IndexController {
@RequestMapping("/login")
public ModelAndView myindex() {
return new ModelAndView("login.jsp");
}
}
You may do all the heavy logic before rendering the JSP and pass to it just a map with the values. Something like this:
@Controller
public class IndexController {
@RequestMapping("/login")
public ModelAndView myindex(HTTPServletRequest request) {
Map<String, Object> data = new HashMap<>();
data.put("userId", request.getParameter("userId"));
...
// call mysql driver here for whatever reason...
...
return new ModelAndView("login.jsp", data);
}
}
Then in your login.jsp you may just display the values of the map, not doing any logic like calling the SQL driver

- 430
- 3
- 6
-
I added the whole simple project link... Can you just check it for me? – Ayesha Fernando Aug 31 '18 at 11:42
-
and when I use try catch, it doesnt show any error.. it goes to jsp page which is blank and the db is not updated too – Ayesha Fernando Aug 31 '18 at 11:44
-
-
Version 5.1.6 of MySQL Connector/J is ancient and should not be used. Recent versions are 5.1.47 or 8.0.12. – Mark Rotteveel Aug 31 '18 at 16:18
You are using request.getParameter() in the JSP.You have to use it in java file by taking HTTPServletRequest as a parameter in that particular method. And also you will be needing MysqlConnector.jar file in your classpath if you are connecting to MySql

- 160
- 1
- 2
- 16
-
Thanks for helping me but I dont understand how to use it in java file.... Can you pleasehelp me – Ayesha Fernando Aug 31 '18 at 11:08
-
You can take some basic code example of servlet implementation from javatpoint, tutorials point.and also for connecting to MySQL you will be needing MysqlConnector.jar in your classpath. – Harsh Kumrawat Aug 31 '18 at 11:31
-
I added the whole simple project link... Can you just check it for me? – Ayesha Fernando Aug 31 '18 at 11:42
-
-
-
Now I use try catch.. It dissapear the error and loaded to jsp page(blank page)but the db is not changed – Ayesha Fernando Sep 01 '18 at 04:42
-
I got the notification about my access to your drive.I'll provide u once I'm done with it in proper way – Harsh Kumrawat Sep 01 '18 at 04:50
-