0

No suitable driver found for jdbc:mysql://mudfoot.doc.stu.mmu.ac.uk/

this is the error i am receiving. i am trying to connect to my database to add new information about courses. Below is the code for my CourseAdd.java. It takes parameters in from an html file then attempts to add the information. Please help !!! I do have the JDBC driver in the library

**package course_14024632;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
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;
@WebServlet("/CourseAdd")
public class CourseAdd extends HttpServlet {
    private static final long serialVersionUID = 1L;

 public CourseAdd() {
     super();

 }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //this adds a new recording into the database
        //under the table called Music_Recordings

        response.setContentType("text/html;charset=UTF-8");
     PrintWriter out = response.getWriter();

     out.println("<header> <link rel = \"stylesheet\" href= \"style1.css\" />  </header>");
     //links to the stylesheet

        String r = request.getParameter("courseid");
        String a = request.getParameter("coursename");
        String t = request.getParameter("coursecredits");
        String c = request.getParameter("courseduration");
        String i = request.getParameter("coursetutor");

        //gets all the parameters 

        String insertSQL = "insert into Course values('+r+','+a+','"+t+"','"+c+"','+i+')";

        //creates an SQL statement


        Connection conn =null; // Create connection object so it can connect to the database
        String database = "*database name*"; // Name of the database
        String user = "*username*"; //name of the username
        String password = "*password*"; //and password
        String url = "jdbc:mysql://mudfoot.doc.stu.mmu.ac.uk/" + database;

        try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch(Exception e) { System.out.println(e); }


        // connecting to database
        try{
            conn = DriverManager.getConnection(url, user, password);

        }
        catch(SQLException se) {
            System.err.println(se);
        }
        // Create select statement and execute it

        try{

            Statement stmt = conn.createStatement();
            int success = stmt.executeUpdate(insertSQL);
            if(success<1){
                System.out.println("update failed!");
            }

            //executes the statement created earlier and closes
            //conneciton to the database

            conn.close();
        }catch(SQLException se) {
            System.err.println(se);
        }


        doGet(request, response);
    }
}**
  • Unrelated - use PreparedStatement and DataSources. – Gurwinder Singh Jan 11 '17 at 17:58
  • The problem with the code is that, _Class.forName("")_ is in another try-catch block and the _getConnection_ is another try-catch block. So even if your class.forName fails, the getConnection would get called anyway. Try to change the code and put all the statements in one try-catch block and run program again. – S R Chaitanya Jan 11 '17 at 18:00
  • Agreeing with the above. Your exception handling is problematic. You're catching, printing, and continuing on a number of fatal exceptions (driver load, connection creation). Also, e.printStackTrace() will be more informative than println(e) – Seth Jan 11 '17 at 20:01
  • 1
    I don't know what your _I do have the JDBC driver in the library_ means, but the JDBC driver has to be either in the `WEB-INF/lib` folder of your application, or in the `lib` folder of your servlet container (Tomcat? Jetty?). I also agree with the others that both your exception handling is terrible, and you have to use (at least) PreparedStatements. – Jozef Chocholacek Jan 12 '17 at 08:01

0 Answers0