2014年8月4日月曜日

データベースを使う(2)

テーブルを作るかどうかを判定するには、データを取ってみたらいいようだ。
ResultSet rs = null;
try {
final Statement s = conn.createStatement();
rs = s.executeQuery("select count(1) from Horse");
} catch (SQLException se) {
final String state = se.getSQLState();
if (state.equals("42X05")) {
makeTable(conn);
} else {
for (SQLException e = se; e != null; e = e.getNextException()) {
TLog.e("%s: %s%n", e.getSQLState(), e.getMessage());
}
}
} finally {
if (rs != null) try {rs.close();} catch (Exception e) {};

}

makeTableの先は、データベースを使うで書いたcreate tableを呼び出している。

0 件のコメント:

コメントを投稿