I am asking the same question,because i didn't find the answer.Here is my problem. I've been trying to use jdbc driver to connect to mysql database. Here is my main sql handler class :
package com.arslanjava.model;
import java.sql.*;
public class SqlHandler {
private Connection myConnection;
// final private String serverName = "localhost" ;
//final private String port = "3306" ;
//final private String user = "root" ;
// final private String password = "password" ;
public SqlHandler ( String username , String password , String fname , String lName ) throws SQLException {
if ( username != null && password != null && fname != null && lName != null ) {
//Establish connection to the server.
this.establishConnection();
//execute the addUser method
this.addUser(username,password,fname,lName);
//close this connection
this.close();
} else {
throw new NullPointerException () ;
}
}
public SqlHandler() {
}
public void addUser (String us , String pas , String fName , String lName ) throws SQLException {
PreparedStatement statement = myConnection.prepareStatement("INSERT INTO user_info (username, password, first_name, last_name) VALUES (?,?,?,?)");
statement.setString(1,us);
statement.setString(2,pas);
statement.setString(3,fName);
statement.setString(4,lName);
statement.executeUpdate();
}
public void establishConnection () throws SQLException {
myConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/app1","root","password") ; ;
}
public void close() {
if (myConnection != null) {
try {
myConnection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Everything works fine,when i use it in plain java code.For example,this works fine :
package com.arslanjava.model;
import com.arslanjava.model.SqlHandler;
import java.io.IOException;
import java.net.ServerSocket;
import java.sql.SQLException;
public class Test {
public static void main(String[] args) {
SqlHandler handler = new SqlHandler() ;
try {
handler.establishConnection();
handler.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
But,when i try to use it in my servlet classes i get an error that says :
No suitable driver found for jdbc:mysql://localhost/name_of_my_database.
I checked the answer in the previous question,it was saying that this was caused by 2 things:
- Wrong url,which i checked and it was fine.(jdbc:mysql://localhost:3306/app1)
- I didn't add jar file
in tomcat's lib folder,which i did,but i still get the same
error.(screenshots:https://prnt.sc/k2qlxa ,https://prnt.sc/k2qm1e,http://prntscr.com/k2qm5h)
Also i added this jar file to the WEB-INF/lib folder,which ,also,didn't work.
Is there anything else i can try?Please,help me understand the problem here,because i can't fix this....
Here is my servlet class :
package com.arslanjava.web;
import com.arslanjava.model.SqlHandler;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.sql.SQLException;
public class MyRegServlet extends HttpServlet {
@Override
public void doPost (HttpServletRequest request , HttpServletResponse response) {
String username = request.getParameter("us") ;
String password = request.getParameter("psw") ;
String first_name = request.getParameter("fname");
String last_name = request.getParameter("lname") ;
try {
SqlHandler handler = new SqlHandler(username,password,first_name,last_name) ;
} catch (SQLException e) {
request.setAttribute("errorCode","5xx");
request.setAttribute("errorMessage",e.toString());
e.printStackTrace();
RequestDispatcher dispatcher = request.getRequestDispatcher("customErroPage.jsp") ;
try {
dispatcher.forward(request,response);
} catch (ServletException | IOException e1) {
e1.printStackTrace();
}
}
}
}
lib
folder. Keep your driver jar file inside that folder and then run your project. – Ravinder Reddy