mardi 7 juillet 2015

Unable to connect to mySQL database using JSP

I'm trying to establish a connection to my database using the following servlet: (the work is done in the doGet method)

package demo;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

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("/Connect")
public class Connect extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public Connect() {
    super();
    // TODO Auto-generated constructor stub
}

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

    PrintWriter out = response.getWriter();

    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        out.println("Unable to load database.");
        return;
    }

    Connection conn = null;

    try {
        conn = DriverManager.getConnection("jdbc.mysql://localhost:3306/jakesdb", "root", "sixTeen58");
    } catch (SQLException e) {
        out.println("Unable to connect to the database.");
        return;
    }

    //// Connected to database.  do some work here ////////////
    out.println("Connected to the database!");
    ///////////////////////////////////////////////////////////

    try {
        conn.close();
    } catch (SQLException e) {
        out.println("Unable to close connection to database.");
    }       
}
}

However, I am always getting hung up at

try {
        conn = DriverManager.getConnection("jdbc.mysql://localhost:3306/jakesdb", "root", "sixTeen58");
    } catch (SQLException e) {
        out.println("Unable to connect to the database.");
        return;
    }

I have verified using sql workbench that the username (root) and password (sixTeen58) are correct, and the port is indeed 3306. I have mysql-connector-java-5.1.36.zip in my libraries and it's linked properly. I'm not sure what is going on but every time I just see "Unable to connect to the database" when I run the page.

Any idea what I'm doing wrong?

Aucun commentaire:

Enregistrer un commentaire