简体   繁体   中英

MySql database connection issues in play framework (driver not found)

So I have tried using the stock Play! 2.2 configuration for the MySql database connection. Unfortunately the guides out there are less than helpful when using the stock database (h2) alongside a MySql. SO, I coded a separate model to handle the MySql connection. It works intermittently, and I'm trying to figure out why it doesn't work all of the time.

this is the "connect" function

String sourceSchema = "db";
    String databaseHost = "host";
    String databaseURLSource = "jdbc:mysql://" + databaseHost + "/" + sourceSchema;
    String databaseUserIDSource = "userid";
    String databasePWDSource = "password";

    try {

        Class.forName("com.mysql.jdbc.Driver").newInstance();
        conn = DriverManager.getConnection(databaseURLSource,
                databaseUserIDSource, databasePWDSource);

        return true;

    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException e) {
        Logger.error("SQLException: " + e.getMessage());
    }

All of my credentials are correct (here obviously they are changed) Next, in my lib folder, I have the

 mysql-connector-java-5.1.21-bin.jar
in place.

Next, in my Build.scala, I have this under appDependencies:

public boolean isConnected() {
    return conn != null;
}

when I try to validate the connection, using:

SQLException: Before start of result set

The connection fails (intermittantly) and then gives me:

SQLException: No Suitable driver found for mysql ...

and sometimes:

String qs = String.format("SELECT * FROM community_hub.alert_journal LIMIT("+ from +","+ to +")");

This is how my query is executed:

    String qscount = String.format("SELECT COUNT(*) AS count FROM community_hub.alert_journal");

try {

        if (isConnected()) {

            Statement stmt = conn.createStatement();

            //obtain count of rows
            ResultSet rs1 = stmt.executeQuery(qscount);
            //returns  the number of pages to draw on index
            int numPages = returnPages(rs1.getInt("count"),rpp);
            NumPages(numPages);
            ResultSet rs = stmt.executeQuery(qs);

            while (rs.next())
            {
                AlertEntry ae = new AlertEntry(
                        rs.getTimestamp("date"),
                        rs.getString("service_url"),
                        rs.getString("type"),
                        rs.getString("offering_id"),
                        rs.getString("observed_property"),
                        rs.getString("detail")
                );

                list.add(ae);
            }

            rs.close();
            disconnect();

        } else {
            System.err.println("Connection was null");
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

  String qscount = String.format("SELECT COUNT(*) AS count FROM community_hub.alert_journal"); try { if (isConnected()) { Statement stmt = conn.createStatement(); //obtain count of rows ResultSet rs1 = stmt.executeQuery(qscount); //returns the number of pages to draw on index int numPages = returnPages(rs1.getInt("count"),rpp); NumPages(numPages); ResultSet rs = stmt.executeQuery(qs); while (rs.next()) { AlertEntry ae = new AlertEntry( rs.getTimestamp("date"), rs.getString("service_url"), rs.getString("type"), rs.getString("offering_id"), rs.getString("observed_property"), rs.getString("detail") ); list.add(ae); } rs.close(); disconnect(); } else { System.err.println("Connection was null"); } } catch (Exception e) { e.printStackTrace(); } 

Help?

Thanks!

does the mysql error tell you anything?

the first error "SQLException: Before start of result set" looks like its incomplete. Maybe the error log contains the full message or you can

the second one "SQLException: No Suitable driver found for mysql" clearly indicates a classpath issue.

usually connection pools like c3p0 or BoneCP recommed to use a validation query to determine if a connection is valid (something like "select 1" for mysql). That may help to make sure the connection is ok and not rely on the driver?

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