所以我尝试使用股票播放! 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();
}帮帮我?
谢谢!