简体   繁体   English

Openshift-找不到适用于jdbc:mysql的驱动程序

[英]Openshift - No suitable driver found for jdbc:mysql

I am new to Java and Servlet Programming. 我是Java和Servlet编程的新手。 I am trying to host a simple application which is working successfully in localhost. 我正在尝试托管一个简单的应用程序,该应用程序可以在localhost中成功运行。 but when i host it to Openshift, it says No suitable driver found for jdbc:mysql://127.12.204.2:3306/shifar . 但是当我将其托管到Openshift时,它说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 我无法弄清错误:(。应用程序的部署是通过Git作为.WAR

Live Preview - (Enter something in the edittext and submit) 实时预览 -(在编辑文本中输入内容并提交)

Your servlet container needs access to the jar file its way. 您的Servlet容器需要以这种方式访问​​jar文件。 For instance Tomcat might want something like mysql-connector-java-5.1.35-bin.jar in the web-inf folder under the application. 例如,Tomcat可能在应用程序下的web-inf文件夹中想要类似mysql-connector-java-5.1.35-bin.jar You need to focus on your classpath and the setup of your servlet container, regardless of what that is. 无论是什么,您都需要关注类路径和servlet容器的设置。

If you need further assistance hang a question under this with more details. 如果您需要进一步的帮助,请在此下悬垂更多的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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