所以我尝试使用股票 Play!2.2 配置MySql数据库连接。不幸的是,在将股票数据库 (h2) 与 MySql 一起使用时,那里的指南没有帮助。所以,我编写了一个单独的模型来处理 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 下有这个:
"mysql" % "mysql-connector-java" % "5.1.21"
当我尝试验证连接时,使用:
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();
}
帮助?
谢谢!