簡體   English   中英

Play框架中的MySQL數據庫連接問題(找不到驅動程序)

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

因此,我嘗試使用股票Play! 2.2配置MySql數據庫連接。 不幸的是,與MySql一起使用股票數據庫(h2)時,那里的指南沒有多大幫助。 因此,我編寫了一個單獨的模型來處理MySql連接。 它間歇性地工作,我試圖弄清為什么它一直都不工作。

這是“連接”功能

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

我所有的憑據都是正確的(顯然這里已更改)接下來,在我的lib文件夾中,

 mysql-connector-java-5.1.21-bin.jar
到位。

接下來,在我的Build.scala中,在appDependencies下有以下內容:

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

當我嘗試驗證連接時,請使用:

SQLException: Before start of result set

連接失敗(間歇性地),然后給我:

SQLException: No Suitable driver found for mysql ...

有時:

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

這是我的查詢執行方式:

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

救命?

謝謝!

mysql錯誤告訴你什么嗎?

第一個錯誤“ SQLException:結果集開始之前”看起來不完整。 錯誤日志中可能包含完整的消息,或者您可以

第二個“ SQLException:找不到適合mysql的驅動程序”明確指示類路徑問題。

通常,建議將連接池(例如c3p0或BoneCP)建議使用驗證查詢來確定連接是否有效(對於mysql來說,類似於“ select 1”)。 這可能有助於確保連接正常並且不依賴驅動程序?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM