なので、ストックプレイを使ってみました!2.2MySqlデータベース接続の構成。残念ながら、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の下にこれがあります。
"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();
}
ヘルプ?
ありがとう!