2014年8月6日水曜日

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

データベースを使う(1)で作ったテーブルに、馬のデータを差し込んで行く。普通にSQLのinsertを使えばいいだけ。executeUpdate()を使えば、Primary Keyが同じ場合はエラーでなく、上書きされる。これをYahoo競馬から取得したデータをすべてデータベースに登録し直し。これで、使いやすくなったはず。

public boolean insert(Horse horse, int term, int place, int day, int rnum, Date date) {
try {
final String sql = "insert into Horse values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
final PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, horse.name);
ps.setInt(2, term);
ps.setInt(3, place);
ps.setInt(4, day);
ps.setInt(5, rnum);
ps.setDate(6, new java.sql.Date(date.getTime()));
ps.setInt(7, horse.frame);
ps.setInt(8, horse.number);
ps.setInt(9, horse.order);
ps.setInt(10, horse.time);
ps.setInt(11, horse.popularity);
ps.setInt(12, horse.weight);
ps.setFloat(13, horse.loadweight);
ps.setFloat(14, horse.threefurlong);
ps.setFloat(15, horse.odds);
ps.setString(16, horse.getPassorder());
ps.executeUpdate();
} catch (SQLException se) {
for (SQLException e = se; e != null; e = e.getNextException()) {
TLog.e("%s: %s%n", e.getSQLState(), e.getMessage());
}
}
return true;

}

0 件のコメント:

コメントを投稿