0
votes

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,

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");

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;
    }
}
1
Do you have a print statement that print out myVersion? If yess what is the content of myVersion? You should not put the exception inb your result variable. You should log this exception and rethrow it. - Jens
By the way it is com.mysql.jdbc.Driver Not com.mysql.jdc.Driver :) Just mentioning. :) - Arun Subramanian

1 Answers

0
votes

Try reading through this KB article on how to use the default mysql & postgresql connections provided by each Servlet or Application container: https://www.openshift.com/kb/kb-e1086-how-to-use-the-pre-configured-mysqlds-and-postgresqlds-data-sources-in-the-java