error: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver? - java

I am trying simple login authentication using servlets and jdbc connection to MySQL database. After executing it on tomcat server I am getting following error:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.leader.prog.Validate.checkUser(Validate.java:13)
at com.leader.servlet.Login1.doPost(Login1.java:42)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2466)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2455)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
I am sharing the content of all files:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.leader.servlet.Login1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>welcome</servlet-name>
<servlet-class>com.leader.servlet.Welcome</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>welcome</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>Home.html</welcome-file>
</welcome-file-list>
</web-app>
Login1.java
package com.leader.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.leader.*;
import com.leader.prog.Validate;
#WebServlet("/Login1")
public class Login1 extends HttpServlet {
/**
* #see HttpServlet#HttpServlet()
*/
public Login1() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String user = request.getParameter("userName");
String pass = request.getParameter("password");
if(Validate.checkUser(user.toString(),pass.toString()))
{
RequestDispatcher rs = request.getRequestDispatcher("Welcome");
rs.forward(request, response);
}
else
{
out.println("Username or Password incorrect");
RequestDispatcher rs = request.getRequestDispatcher("Home.html");
rs.include(request, response);
}
}
}
validate.java
package com.leader.prog;
import java.sql.*;
public class Validate
{
public static boolean checkUser(String user,String pass)
{
boolean st =false;
try{
//loading driver
Class.forName("com.mysql.jdbc.Driver");
//creating connection with the database
Connection con=DriverManager.getConnection
("jdbc:mysql://localhost:3306/person","root","password");
PreparedStatement ps =con.prepareStatement
("select * from person where pid=? and password=?");
ps.setString(1, user);
ps.setString(2, pass);
ResultSet rs =ps.executeQuery();
st = rs.next();
}catch(Exception e)
{
e.printStackTrace();
}
return st;
}
}
Please help me to get out of this issue.

Check the availability of mysql driver is in classpath. If you are using IDE(eclipse) try to add it to build path.

You should add Mysql connector library you your project buildpath
You can download library from
http://dev.mysql.com/downloads/connector/j/5.0.html
or
http://www.java2s.com/Code/Jar/c/Downloadcommysqljdbc515jar.htm

Related

HTTP 500 while uploading java web on heroku

This is my project:
#LoginServlet:
package Controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Uti.Connect2DB;
/**
Servlet implementation class LoginServlet
**/
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = Connect2DB.getConnection();
String n= request.getParameter("txtName");
String p= request.getParameter("txtPassword");
PreparedStatement ps = con.prepareStatement("select username from login where username=? and password=?");
ps.setString(1, n);
ps.setString(2, p);
ResultSet rs = ps.executeQuery();
if(rs.next())
{
RequestDispatcher rd = request.getRequestDispatcher("admin.jsp");
rd.forward(request, response);
}
else
{
out.print("<a href=login.jsp font-size:33pt margin-left:auto margin-right:auto>TRY AGAIN</a>");
}
}catch(ClassNotFoundException e){
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
#web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>Controller.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
</web-app>
After deploying successfully on heroku, when I login, I got this error message:
HTTP Status 500 – Internal Server Error
Type Exception Report
Message Error instantiating servlet class [Controller.LoginServlet]
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
javax.servlet.ServletException: Error instantiating servlet class [Controller.LoginServlet]
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1598)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:750)
Root Cause
java.lang.UnsupportedClassVersionError: Controller/LoginServlet has been compiled by a more recent version of the Java Runtime (class file version 60.0), this version of the Java Runtime only recognizes class file versions up to 52.0 (unable to load class [Controller.LoginServlet])
org.apache.catalina.loader.WebappClassLoaderBase.findClassInternal(WebappClassLoaderBase.java:2424)
org.apache.catalina.loader.WebappClassLoaderBase.findClass(WebappClassLoaderBase.java:865)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1334)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1188)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1598)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:750)
Note The full stack trace of the root cause is available in the server logs.
Im using tomcat version 8.5
p.s: when deploying on heroku, it was successfully deploy but I show some message:
Exception in thread "Thread-0" java.lang.RuntimeException: Failed to delete C:\Users\DABEII~1\AppData\Local\Temp\heroku-deploy13190833225245223655
at com.heroku.sdk.deploy.DeleteDirectoryRunnable.run(DeleteDirectoryRunnable.java:46)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.nio.file.FileSystemException: C:\Users\DABEII~1\AppData\Local\Temp\heroku-deploy13190833225245223655\heroku\app\.heroku-deploy: The process cannot access the file because it is being used by another process
at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:92)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
at java.base/sun.nio.fs.WindowsFileSystemProvider.implDelete(WindowsFileSystemProvider.java:275)
at java.base/sun.nio.fs.AbstractFileSystemProvider.delete(AbstractFileSystemProvider.java:105)
at java.base/java.nio.file.Files.delete(Files.java:1152)
at com.heroku.sdk.deploy.DeleteDirectoryRunnable$1.visitFile(DeleteDirectoryRunnable.java:21)
at com.heroku.sdk.deploy.DeleteDirectoryRunnable$1.visitFile(DeleteDirectoryRunnable.java:16)
at java.base/java.nio.file.Files.walkFileTree(Files.java:2812)
at java.base/java.nio.file.Files.walkFileTree(Files.java:2883)
at com.heroku.sdk.deploy.DeleteDirectoryRunnable.run(DeleteDirectoryRunnable.java:44)
... 1 more
Is this the reason of the error I get or something else?
Thank you guys, Im so new for this.

Servlet Error - Checking Multiple Variables

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mysql.jdbc.PreparedStatement;
/**
* Servlet implementation class Login
*/
#WebServlet("/Login")
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public Login() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
Connection conn = null;
String url="jdbc:mysql://localhost:3306/";
String dbName="Tamir";
String driver="com.mysql.jdbc.Driver";
try{
request.setAttribute("message", "");
String nick = request.getParameter("check_name");
String password = request.getParameter("check_password");
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,"root", "tamir");
//admin check
/*
java.sql.PreparedStatement st2 = conn.prepareStatement("SELECT * FROM users WHERE nickname = ? or email = ? and password = ? and admin = T");
st2.setString(1, nick);
st2.setString(2, nick);
st2.setString(3, password);
ResultSet r2=st2.executeQuery();
if(r2.next()) {
request.getSession().setAttribute("admin", nick);
}
*/
//regular check
java.sql.PreparedStatement st = conn.prepareStatement("SELECT * FROM users WHERE nickname = ? or email = ? and password = ?");
st.setString(1, nick);
st.setString(2, nick);
st.setString(3, password);
ResultSet r1=st.executeQuery();
if(r1.next()) {
request.getSession().setAttribute("user", nick);
request.getRequestDispatcher("/tf2main.jsp").forward(request, response);
} else {
request.setAttribute("message", "Username or password are incorrect.");
request.getSession().setAttribute("user", null);
request.getRequestDispatcher("/Login.jsp").forward(request, response);
}
}catch (Exception ex) {
System.out.println("Error");
}
}
}
Hello. So I've made the code above and it is working perfectly, but when I add the comment [which I really need, in order to check if he's an admin... Cruical for me] it just throws the exception for some reason.
The only other souloution I've thought about was redricting to another servlet, doing the same action somehow but that's impossible so I'm kinda stuck, you also can't use ERB.
I've tried everything and nothing works.
Edit: The error being throwed is:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'T' in 'where clause'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.Util.getInstance(Util.java:408)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:943)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2487)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1966)
at Login.doPost(Login.java:68)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Update the query to be something like:
java.sql.PreparedStatement st2 =
conn.prepareStatement("SELECT * FROM users WHERE nickname = ? or email = ? and password = ? and admin = 'T'");
Note the single quote marks around the T in the query.

Error while submitting a simple form

Error while submitting a simple form:
package mvc.controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import mvc.bean.PollBean;
import mvc.dao.PollDAO;
#WebServlet("/pollControll")
public class PollController extends HttpServlet {
private static final long serialVersionUID = 1L;
private PollDAO pd;
public PollController() {
pd = new PollDAO();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
if (action.equalsIgnoreCase("SelectALL")) {
request.setAttribute("polls", pd.SelectAll());
}
response.getWriter().append("Served at: ").append(request.getContextPath());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
if (action.equalsIgnoreCase(("insert"))) {
PollDAO pp = new PollDAO();
PollBean p = new PollBean();
int note = Integer.parseInt(request.getParameter("note"));
String email = request.getParameter("email");
p.setNote(note);
p.setEmail(email);
pp.insert(p);
RequestDispatcher direct = request.getRequestDispatcher("/home.jsp");
direct.forward(request, response);
} else if (action.equalsIgnoreCase("ResetALL")) {
PollBean p = new PollBean();
pd.ResetALL(p);
} else if (action.equalsIgnoreCase("Activated")) {
PollBean p = new PollBean();
pd.Activated(p);
RequestDispatcher direct = request.getRequestDispatcher("/PollAdmin.jsp");
direct.forward(request, response);
} else if (action.equalsIgnoreCase("Deactivated")) {
PollBean p = new PollBean();
pd.Deactivated(p);
RequestDispatcher direct = request.getRequestDispatcher("/PollAdmin.jsp");
direct.forward(request, response);
}
doGet(request, response);
}
}
And here the Stacktrace:
Servlet.service() for servlet [mvc.controller.PollController] in context with path [/Enquete_eduCAPES] threw exception [Servlet execution threw an exception] with root cause
java.lang.ClassNotFoundException: java.time.temporal.TemporalField
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1858)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1701)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:320)
at org.postgresql.Driver.makeConnection(Driver.java:406)
at org.postgresql.Driver.connect(Driver.java:274)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at mvc.util.ConexaoDAO.getConnection(ConexaoDAO.java:29)
at mvc.dao.PollDAO.insert(PollDAO.java:40)
at mvc.controller.PollController.doPost(PollController.java:60)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
What version of Java are you using?
Because according to Java specifications the time/temporal/TemporalField was introduced in version 8, below this version it will not work.

java.lang.ClassNotFoundException: com.mysql.jdbc.driver error with jar file already imported

I have been searching all around for a solution to my problem and the only solution i have found is to add the jar file that i already have. This is done in eclipse EE and is useing tomcat.
I am running a different HTML file which links to this piece of code once a button is pressed, if you want that code feel free to ask, but there is almost nothing on it.
I keep getting the error "java.lang.ClassNotFoundException: com.mysql.jdbc.driver" along with 100 other lines which i'm not sure are conencted.
Here is what i have:
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class HelloForm
*/
#WebServlet("/test5")
public class test5 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public test5() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
String url = "jdbc:mysql://localhost:3306/";
String dbName = "test";
String driver = "com.mysql.jdbc.driver";
String userName = "root";
String password = "games10";
try {
Class.forName(driver).newInstance();
Connection conn = (Connection) `enter code here`DriverManager.getConnection(url+dbName,userName,password);
Statement stat = conn.createStatement();
//stat.execute("CREATE TABLE test (Name CHAR(20))");
stat.execute("INSERT INTO TEST VALUES('"+request.getParameter("first_name")+"')");
conn.close();
System.out.println("Working");
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
I apolagize if i have posted the code wrong, ive never used this site before and am tired.
As i have said i have imported the "mysql-connector-java-5.1.30-bin.jar" file. All help is accepted with open arms and critisism also.
Thank you for your time
Errors in case it's not actually related to the driver:
java.lang.ClassNotFoundException: com.mysql.jdbc.driver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at test5.doGet(test5.java:59)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2441)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2430)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
After importing the jar, you have to add it to the build path of your project.
right click on jar file -> build path ->add to build path
You've misspelt the class name, but you don't need it. Just remove that line. It's been obsolete for years, since JDBC 4.0.
I found the problem. I needed a uppercase D in String driver = "com.mysql.jdbc.driver";
God I feel stupid now

HTTP 400 and 500 error messages

when I run my application, I'm having a HTTP error:
HTTP Status 404 - /ProjectName/
This is the URL of my project:
localhost:8080/Initiation1/
When I type this string to the address bar:
localhost:8080/Initiation1/CarteIdentite
, I'm having this HTTP 500 error message:
javax.servlet.ServletException: Error instantiating servlet class ca.web.CarteIdentite
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:269)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:300)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)
root cause
java.lang.ClassNotFoundException: ca.web.CarteIdentite
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:269)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:300)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)
This is my servlet code:
package ca.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
/**
* Servlet implementation class CarteIdentite
*/
#WebServlet("/CarteIdentite")
public class CarteIdentite extends HttpServlet
{
// paramètres d’instance
private String defaultNom = null;
private String defaultAge = null;
private String defaultCourriel = null;
//init
public void init()
{
// on récupère les paramètres d’initialisation de la servlet
ServletConfig config = getServletConfig();
defaultNom = config.getInitParameter("defaultNom");
if(defaultNom==null)
{
defaultNom="XXXXXXXXXXXXXXXX";
}
defaultAge = config.getInitParameter("defaultAge");
if(defaultAge==null)
{
defaultAge="AAA";
}
defaultCourriel = config.getInitParameter("defaultCourriel");
if(defaultCourriel==null)
{
defaultCourriel="XXXXXX#XXXXXXXX.XX";
}
}
//GET
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
IOException, ServletException
{
// on récupère les paramètres du formulaire
String nom = request.getParameter("txtNom");
if (nom == null)
{
nom = defaultNom;
}
String age = request.getParameter("txtAge");
if (age == null)
{
age = defaultAge;
}
String courriel = request.getParameter("txtCourriel");
if (courriel == null)
{
courriel = defaultCourriel;
}
// on affiche le formulaire
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println
(
"<html>"+
"<head>"+
"<title>Carte d’identité</title>"+
"</head>"+
"<body>"+
"<center>"+
"<h2>Carte d’identité</h2>"+
"<form action=’’ method=’post’>"+
"<table>"+
"<tr>"+
"<td>Nom:</td>"+
"<td><input name=’txtNom’ value=’"+nom+"’ type=’text’ size=’30’></td>"+
"</tr>"+
"<tr>"+
"<td>Age:</td>"+
"<td><input name=’txtAge’ value=’"+ age +"’ type=’text’ size=’3’></td>"+
"</tr>"+
"<tr>"+
"<td>Courriel:</td>"+
"<td><input name=’txtCourriel’ value=’"+courriel+"’ type=’text’ size=’30’></td>"+
"</tr>"+
"</table>"+
"<table>"+
"<tr>"+
"<td><input type=’submit’ value=’Envoyer’></td>"+
"<td><input type=’reset’ value=’Effacer’></td>"+
"</tr>"+
"</table>"+
"</form>"+
"</center>"+
"</body>"+
"</html>"
);
}
/**
* #param request la requête HTTP du client
* #param response la réponse HTTP qu’on va construire
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
//on passe la main au GET
doGet(request, response);
}
}
This is my web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app SYSTEM "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>CarteIdentite</servlet-name>
<servlet-class>ca.web.CarteIdentite</servlet-class>
<init-param>
<param-name>defaultNom</param-name>
<param-value>inconnu</param-value>
</init-param>
<init-param>
<param-name>defaultAge</param-name>
<param-value>XXX</param-value>
</init-param>
<init-param>
<param-name>defaultCourriel</param-name>
<param-value>inconnu</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>CarteIdentite</servlet-name>
<url-pattern>/CarteIdentite</url-pattern>
</servlet-mapping>
</web-app>
Here is my project structure:
Maybe Eclipse isn't treating src as a source folder. Try right clicking on it and choosing Build Path -> Use as source folder.
Check if the class ca.web.CarteIdentite has compiled correctly and made way into the WAR of your project (under WEB-INF/classes). If not, check/correct the project buildpath settings.

Categories