在Java中使用不必要的值执行了两次查询(错误) [英] Query executed twice (by error) in Java with unwanted values
问题描述
我正在使用JFreeChart在Java和MySQL中创建图表.
I'm using JFreeChart to create a chart in Java and MySQL.
当我尝试将值插入到另一个表中时,查询似乎被执行了两次,因为我多次获得相同的时间戳记...
When I try to insert my values in another table the query seems to be executed twice since I end up with the same timestamps multiple times...
这是我的代码的一部分:
Here's a part of my code :
private JDBCXYDataset createDataset() {
try {
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:bd?serverTimezone=UTC","MySQL", "MySQL");
conn.setAutoCommit(false);
SQLException savedException = null;
Statement st = conn.createStatement();
st.execute("DROP TABLE IF EXISTS test ");
st.execute("create table test(Table timestamp, Table float,Table float)");
String Date_Debut = "2020-06-25 00:00:00";
String Date_Fin = "2020-06-26 00:00:00";
String sql1 = "INSERT INTO test (Table ,Table ,Table ) "
+ "SELECT Table ,Table ,Table "
+ "FROM Table "
+ "WHERE Table BETWEEN ? AND ? ";
try ( PreparedStatement ps = conn.prepareStatement(sql1)){
ps.setString(1,Date_Debut);
ps.setString(2, Date_Fin);
ps.executeUpdate();
ps.close();
JDBCXYDataset jds = new JDBCXYDataset(conn);
st.close();
jds.executeQuery("SELECT Table ,Table ,Table FROM test");
conn.commit();
return jds;
} catch (SQLException ex) {
savedException = ex;
conn.rollback();
} finally {
conn.setAutoCommit(true);
if(savedException != null) {
throw savedException;
}
}
} catch (SQLException ex1) {
}
return null;
}
实际上,从数据库直接发出错误消息,主持人可以根据需要删除此帖子.但是,我让Trashgod的回复得到了验证,因为它的帮助不止于此. 对于可能遇到类似问题的每个人,请先详细检查数据库,以查看它是否不是从那里来的,而不是从您的代码来的.
EDIT : Actually it seems like the errors where comming directly from the database, the moderators can delete this post if they want. However I keep Trashgod's response validated as it was more than helpful. For everyone that might come here with a similar issue, inspect in detail your database first to see if it isn't comming from there instead of your code.
推荐答案
追踪数据异常是艰巨的事情,但是JFreeChart
至少可以使结果更容易可视化.一些启发式测试:
Chasing down anomalies in data is arduous, but JFreeChart
can at least make the result easier to visualize. Some heuristics for testing:
-
要验证表格列表中的假定重复项确实是重复项,请格式化时间戳以包含毫秒,例如将
S
添加到SimpleDateFormat
或A
添加到DateTimeFormatter
.
为了进行研究,请将查询直接直接传递到JDBCXYDataset
,并添加一个ORDER BY
子句(未经测试):
For study, temporarily pass the query directly to JDBCXYDataset
, and add an ORDER BY
clause (untested):
jds.executeQuery(
"SELECT Date_Heure, PV, SV FROM cmd3 "
+ "WHERE Date_Heure BETWEEN "
+ "2020-06-25 00:00:00 AND 2020-06-26 00:00:00 "
+ "ORDER BY Date_Heure");
像在此处一样,在ChartFactory
中启用工具提示,以查看数据值原地.这可能会为您的WHERE
子句建议其他条件,例如PV BETWEEN 5.1 AND 5.9
.
Enable tooltips in your ChartFactory
, as you did here, to see data values in situ. This may suggest additional conditions for your WHERE
clause, e.g. PV BETWEEN 5.1 AND 5.9
.
使用交互式JFreeChart
平移/缩放控件(在此处中进行了讨论)来检查数据;添加合适的按钮,如此处所示,以方便同事查看您的发现.
Use the interactive JFreeChart
pan/zoom controls, discussed here to examine the data; add suitable buttons, shown here, if it will make it easier for colleagues to see your findings.
根据设计, JDBCXYDataset
执行由String
定义的查询.如果您的设计需要显示PreparedStatement
定义的查询中的数据,则可以使用现有的
By design, JDBCXYDataset
executes a query defined by a String
. If your design needs to display data from a query defined by a PreparedStatement
, you can use the existing implementation as a guide.
public class PreparedDataset extends AbstractXYDataset
implements XYDataset, TableXYDataset, RangeInfo {
private final PreparedStatement ps;
public PreparedDataset(PreparedStatement ps) {
this.ps = ps;
}
…
}
这篇关于在Java中使用不必要的值执行了两次查询(错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!