简体   繁体   中英

Query executed twice (by error) in Java with unwanted values

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;
    }

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.

Chasing down anomalies in data is arduous, but JFreeChart can at least make the result easier to visualize. Some heuristics for testing:

  • To verify that the the presumed duplicates in your tabular listing are indeed duplicates, format the timestamps to include milliseconds, eg add an S to a SimpleDateFormat or A to a DateTimeFormatter .

  • 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");
  • Enable tooltips in your ChartFactory , as you did here , to see data values in situ . This may suggest additional conditions for your WHERE clause, eg PV BETWEEN 5.1 AND 5.9 .

  • 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.

  • 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; } … }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM