2011-01-20 33 views
1

我已經成功地將數據插入到數據庫中,只需寫入我需要的數據即可。現在我試圖插入將保存數據的變量和數組。這是在黑暗中拍攝的,因爲我不知道該怎麼做,我只是猜測。我沒有得到語法錯誤,所以我認爲我做得很好,但它不能編譯...我只需要知道確切的語法來做到這一點。使用Java將變量,數組插入MySQM數據庫

for(int i = 0; i < ReadingFile.altitudeList.size(); i++){ 
for(int j = 0; j < ReadingFile.temperatureList.size(); j++){ 
    for(int k = 0; k < ReadingFile.velocityList.size(); k++){ 
    for(int x = 0; x < ReadingFile.latList.size(); x++){ 
    for(int y = 0; y < ReadingFile.longList.size();y++){ 
stat 
    .execute("INSERT INTO TrailTracker VALUES(id,ReadingFile.date,ReadingFile.distance, ReadingFile.timeElapsed, ReadingFile.startTime," 
       + "ReadingFile.temperatureList[j], ReadingFile.velocityList[k], ReadingFile.altitudeList[i], ReadingFile.latList[x]," 
       + "ReadingFile.longList[y])"); 
     }}}}} 

回答

1

這將是無效的查詢。

你需要去PreparedStatement

+1

這是一個好主意 - 你可以準備一次語句,只需在每次執行時更改值 - 效率特別高,尤其是考慮到這些循環的深度... – Freddie 2011-01-20 09:13:48

2

您不能將變量或數組插入到數據庫中。您只能插入數據,即您的變量或數組的

A PreparedStatement是要走的路。它看起來像這樣;

int a = 1; 
Date b = new Date(); 
String c = "hello world"; 

PreparedStatement stmt = conn.prepareStatement("INSERT INTO MyTable VALUES (?,?,?)"); 
stmt.setInt(1, a); 
stmt.setDate(2, new java.sql.Date(b.getTime()); 
stmt.setString(3, c); 
stmt.execute(); 

請注意,它看起來並不像你正確設計你的表來匹配你的數據。您的ReadingFile似乎有5 List s,您需要弄清楚這些列表中的值是如何相互關聯的。你當前的邏輯有5個嵌套循環幾乎肯定是而不是你想要的。它導致高度非規範化的結構。

例如,假設您有一個ID爲1,日期爲20/1/2011,距離爲10,時間爲20,開始時間爲30的ReadingFile對象,則每個列表都有兩個值;
- 溫度21,23
- 速度51,52
- 海拔1000,2000
- LAT 45.1,47.2
- 長52.3,58.4

然後,你的嵌套循環將數據插入到表格等這個;

 
+--+---------+--------+-----------+---------+-----------+--------+--------+----+----+ 
|id|  date|distance|timeElapsed|startTime|temperature|velocity|altitude| lat|long| 
+--+---------+--------+-----------+---------+-----------+--------+--------+----+----+ 
| 1|20.1.2011|  10|   20|  30|   21|  51| 1000|45.1|52.3| 
| 1|20.1.2011|  10|   20|  30|   21|  51| 1000|45.1|58.4| 
| 1|20.1.2011|  10|   20|  30|   21|  51| 1000|47.2|52.3| 
| 1|20.1.2011|  10|   20|  30|   21|  51| 1000|47.2|58.4| 
| 1|20.1.2011|  10|   20|  30|   21|  52| 1000|45.1|52.3| 
| 1|20.1.2011|  10|   20|  30|   21|  52| 1000|45.1|58.4| 
| 1|20.1.2011|  10|   20|  30|   21|  52| 1000|47.2|52.3| 
| 1|20.1.2011|  10|   20|  30|   21|  52| 1000|47.2|58.4| 
| 1|20.1.2011|  10|   20|  30|   23|  51| 1000|45.1|52.3| 
| 1|20.1.2011|  10|   20|  30|   23|  51| 1000|45.1|58.4| 
| 1|20.1.2011|  10|   20|  30|   23|  51| 1000|47.2|52.3| 
| 1|20.1.2011|  10|   20|  30|   23|  51| 1000|47.2|58.4| 
| 1|20.1.2011|  10|   20|  30|   23|  52| 1000|45.1|52.3| 
| 1|20.1.2011|  10|   20|  30|   23|  52| 1000|45.1|58.4| 
| 1|20.1.2011|  10|   20|  30|   23|  52| 1000|47.2|52.3| 
| 1|20.1.2011|  10|   20|  30|   23|  52| 1000|47.2|58.4| 
| 1|20.1.2011|  10|   20|  30|   21|  51| 2000|45.1|52.3| 
| 1|20.1.2011|  10|   20|  30|   21|  51| 2000|45.1|58.4| 
| 1|20.1.2011|  10|   20|  30|   21|  51| 2000|47.2|52.3| 
| 1|20.1.2011|  10|   20|  30|   21|  51| 2000|47.2|58.4| 
| 1|20.1.2011|  10|   20|  30|   21|  52| 2000|45.1|52.3| 
| 1|20.1.2011|  10|   20|  30|   21|  52| 2000|45.1|58.4| 
| 1|20.1.2011|  10|   20|  30|   21|  52| 2000|47.2|52.3| 
| 1|20.1.2011|  10|   20|  30|   21|  52| 2000|47.2|58.4| 
| 1|20.1.2011|  10|   20|  30|   23|  51| 2000|45.1|52.3| 
| 1|20.1.2011|  10|   20|  30|   23|  51| 2000|45.1|58.4| 
| 1|20.1.2011|  10|   20|  30|   23|  51| 2000|47.2|52.3| 
| 1|20.1.2011|  10|   20|  30|   23|  51| 2000|47.2|58.4| 
| 1|20.1.2011|  10|   20|  30|   23|  52| 2000|45.1|52.3| 
| 1|20.1.2011|  10|   20|  30|   23|  52| 2000|45.1|58.4| 
| 1|20.1.2011|  10|   20|  30|   23|  52| 2000|47.2|52.3| 
| 1|20.1.2011|  10|   20|  30|   23|  52| 2000|47.2|58.4| 
+--+---------+--------+-----------+---------+-----------+--------+--------+----+----+ 
+0

所以,如果我有一個arrayList velocityList [22, 25]中的數據。我可以做(int i = 0; velocityList.size(); i ++){stmt.setInt(1,velocityList [i]); }?它仍然看起來不正確,我敢肯定我錯過了什麼...... – t0x13 2011-01-20 20:17:39

+0

我確定不希望我的數據庫看起來像你的表:),它總是感覺如何從變量插入到數據庫的數據,但仍然不知道我的arrayLists .. – t0x13 2011-01-20 20:20:06

0

所以我想通了,做我需要使用什麼while循環

while(!(sampleSize == temp)){ 
     conn.prepareStatement(insertStr); 
     prstat.setInt(1, id); 
     prstat.setInt(7, v.get(temp)); 
     temp++; 
     prstat.executeUpdate(); 
     } 

臨時最初設置爲零,和增量同時插入從ArrayList的元素融入到數據庫中,直到它等於最簡單的方法sampleSize(sampleSize = v.size();),以便它知道它已到達列表的末尾。感謝大家的幫助!