简体   繁体   中英

Cannot find JDBC driver, want to add new details to MySQL database

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 !!!

**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);
    }
}**

Have you downloaded the MySQL JDBC driver? It can be downloaded here

https://dev.mysql.com/downloads/connector/j/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM