-1
我想從一個ngrams(HashMap)中提取數據庫中的所有值。我的問題是如何更好地實現提取功能。這段代碼的執行速度很慢,因爲如果表中存在HashMap,那麼java程序必須檢查每次。如果存在,則必須從表格中選擇前一個頻率,並將其添加到新的頻率中。在單個查詢中插入,選擇和更新 - SQL
爲了使這個過程更好,我可以更改三個函數(更新,插入,檢查)嗎?
這裏是與數據庫
public boolean insert(String w1, String w2, String w3, String w4, Integer f, float rf) throws SQLException{
...
String sql = "INSERT INTO fourgrams " +
"VALUES("+w1 + "," + w2 + "," + w3 + "," + w4 + "," + f + ',' + rf + ")"
...
}
public boolean update(String w1, String w2, String w3, String w4, Integer f) throws SQLException{
...
String sql = "UPDATE fourgrams " +
"SET frequency =" + f + " WHERE gram1=" + w1 +
" AND gram2=" + w2 + " AND gram3=" + w3 +
" AND gram3=" + w3;
...
}
public int check(String w1, String w2, String w3, String w4) throws SQLException{
...
String sql = "SELECT frequency FROM fourgrams " +
"WHERE gram1=" + w1 +
" AND gram2=" + w2 + " AND gram3=" + w3 +
" AND gram3=" + w3;
...
}
連接的代碼,這裏是代碼,我有問題
public void extract(Database myDB) throws SQLException{
Iterator it = ngrams.entrySet().iterator();
while(it.hasNext()){
...
int fr=myDB.check(...);
if (fr == -1)
{
myDB.insert(...);
}else{
myDB.update(...);
}
}
...
}
謝謝您的回答。我想LOAD INFILE就是我需要的。 –