简体   繁体   中英

Openshift - No suitable driver found for jdbc:mysql

I am new to Java and Servlet Programming. I am trying to host a simple application which is working successfully in localhost. but when i host it to Openshift, it says No suitable driver found for jdbc:mysql://127.12.204.2:3306/shifar . All i want to do is to save a string into the database. Here is my code

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

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private static final String
     HOST = System.getenv("OPENSHIFT_MYSQL_DB_HOST"),
     PORT = System.getenv("OPENSHIFT_MYSQL_DB_PORT"),
     USERNAME = System.getenv("OPENSHIFT_MYSQL_DB_USERNAME"),
     PASSWORD = System.getenv("OPENSHIFT_MYSQL_DB_PASSWORD"),
     DB_NAME = "shifar";

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

        String name = request.getParameter("userName");
        PrintWriter pw = response.getWriter();

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            pw.println("Error while loading driver: "+e.getMessage());
        }

        try {
            String url = "jdbc:mysql://" + HOST + ":" + PORT +
                    "/" + DB_NAME;

            Connection con = DriverManager.getConnection(url, USERNAME, PASSWORD);
            PreparedStatement prep = con.prepareStatement("INSERT INTO names (name) VALUE (?)");
            prep.setString(1, name);
            int rc = prep.executeUpdate();
            pw.println("Name saved !:"+name+" @ "+ rc);
        } catch (SQLException e) {
            pw.println("Error while connecting: "+e.getMessage());
        }


    }

}

I can't figure out the error :(. The deployment of the application is done through Git as .WAR

Live Preview - (Enter something in the edittext and submit)

Your servlet container needs access to the jar file its way. For instance Tomcat might want something like mysql-connector-java-5.1.35-bin.jar in the web-inf folder under the application. You need to focus on your classpath and the setup of your servlet container, regardless of what that is.

If you need further assistance hang a question under this with more details.

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