gpt4 book ai didi

java - 使用 Java 将变量、数组插入 MySQM 数据库

转载 作者:行者123 更新时间:2023-11-29 06:16:59 24 4
gpt4 key购买 nike

我已经通过写入我需要的数据成功地将数据插入到数据库中。现在我正在尝试插入将保存数据的变量和数组。这是在黑暗中拍摄的,因为我不知道该怎么做,我只是猜测。我没有语法错误,所以我认为我做得很好,但它无法编译...我只需要知道执行此操作的确切语法。

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])");
}}}}}

最佳答案

您不能将变量或数组插入数据库。您只能插入数据,即变量或数组的

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,您需要弄清楚这些列表中的值如何相互关联。您当前具有 5 个嵌套循环的逻辑几乎肯定不是您想要的。它导致高度非规范化的结构。

例如,假设您有一个 ReadingFile 对象,其 ID 为 1,日期为 20/1/2011,距离为 10,耗时为 20,开始时间为 30。那么每个列表都有两个值;< br/> - 温度 21、23
- 速度 51、52
- 海拔 1000、2000
- 纬度 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|+--+---------+--------+-----------+---------+-----------+--------+--------+----+----+

关于java - 使用 Java 将变量、数组插入 MySQM 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4745186/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com