2011-01-20 76 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();),以便它知道它已到达列表的末尾。感谢大家的帮助!