Openshift - 无法使用Java连接到mysql

时间:2014-08-06 06:53:41

标签: mysql java openshift

我是Harold,我是Openshift的新手,我使用下面的代码通过示例在这里用java连接到MySQL https://www.openshift.com/forums/openshift/no-suitable-driver-found-error

不幸的是,我无法使其发挥作用。

首先,它说"找不到合适的司机"所以我将mysql-connector添加到WEB-INF / lib文件夹并添加Class.forName(" com.mysql.jdc.Driver");

然后,它不工作,也没有显示错误。

任何帮助都会受到高度赞赏

import java.lang.String;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

public class someClass {

    public String databaseCall() {
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        String myVersion = "";

        String url = "jdbc:mysql://127.x.xxx.x:3306/testdb"; //make sure that this database name exists;
        String user = "admin";
        String password = "xxxxxxxxxxxxxxx";

        try {
            con = DriverManager.getConnection(url, user, password);
            st = con.createStatement();
            rs = st.executeQuery("SELECT VERSION()");

            if (rs.next()) {
                System.out.println(rs.getString(1));
                myVersion = rs.getString(1);
            }

        } catch (SQLException ex) {
            myVersion = ex.getMessage();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (st != null) {
                    st.close();
                }
                if (con != null) {
                    con.close();
                }

            } catch (SQLException ex) {
                myVersion = ex.getMessage();
            }
        }
        return myVersion;
    }
}

1 个答案:

答案 0 :(得分:0)

尝试阅读这篇知识库文章,了解如何使用默认的mysql&每个Servlet或Application容器提供的postgresql连接:https://www.openshift.com/kb/kb-e1086-how-to-use-the-pre-configured-mysqlds-and-postgresqlds-data-sources-in-the-java