简体   繁体   English

Openshift-无法使用Java连接到mysql

[英]Openshift - Cannot connect to mysql with Java

I am Harold and Im new in Openshift, Im using below code to connect to MySQL with java through example here https://www.openshift.com/forums/openshift/no-suitable-driver-found-error , 我是Harold和Im,是Openshift的新手,Im使用下面的代码通过示例通过https://www.openshift.com/forums/openshift/no-suitable-driver-found-error与Java连接到MySQL,

Unfortunately, I was'nt able to make it work. 不幸的是,我无法使其工作。

At first, it says "No suitable driver found" so I added the mysql-connector to WEB-INF/lib folder and add Class.forName("com.mysql.jdc.Driver"); 首先,它说“找不到合适的驱动程序”,因此我将mysql-connector添加到WEB-INF / lib文件夹并添加Class.forName(“ com.mysql.jdc.Driver”);

Then, its not working and not showing error either. 然后,它不起作用,也没有显示错误。

Any help would highly appreciated 任何帮助将不胜感激

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

尝试通读这篇有关如何使用每个Servlet或应用程序容器提供的默认mysql和postgresql连接的知识库文章: https : //www.openshift.com/kb/kb-kb1086-how-to-use-the-pre-在Java中配置mysqlds和postgresqlds数据源

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

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