我想在一个 SQL 表中插入 5 个数据,其中最后四个已经可以正常工作了,但第一个应该来自另一个表,一个字符串,给我插入它的表一个 Int。到目前为止,这是我的解决方案。但是,我仍然收到错误消息:“索引 5 超出范围。”
PreparedStatement stmt = connection.prepareStatement("INSERT INTO RECORDS (LocationId, RecId, RecValues, YearTime, HourTime) VALUES " +
"((SELECT LocationId from Locations where Location_name = 'Mic HR1'), ?, ?, ?, ?)");
stmt.setInt(1,1);
stmt.setInt(2, recid);
stmt.setInt(3, inputData);
stmt.setDate(4, sqlDate);
stmt.setTime(5, Time.valueOf(dtf.format(now)));
stmt.executeUpdate();
实际上,您在查询中只定义了四个参数,因为 LocationId 是由您提供的内部选择计算的。这就是您收到错误的原因。
试试这个:
PreparedStatement stmt = connection.prepareStatement("INSERT INTO RECORDS (LocationId, RecId, RecValues, YearTime, HourTime) VALUES " +
"((SELECT LocationId from Locations where Location_name = 'Mic HR1'), ?, ?, ?, ?)");
stmt.setInt(1, recid);
stmt.setInt(2, inputData);
stmt.setDate(3, sqlDate);
stmt.setTime(4, Time.valueOf(dtf.format(now)));
stmt.executeUpdate();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句