Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java SQL
Following is the code of my page.....
 
the problem is that the value of variable ip becomes "zero" every time...i want to stop it from being "zero"
the value of i should be equal to the value of production.....
 
Code:
package org.dst.db;
 
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import org.dst.db.conn.pool.DBConnection;
import org.dst.db.conn.pool.Pool;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 

 
public class LOG implements DBEntity {
 
    private static Logger logger = LoggerFactory.getLogger(LOG.class);
    private String shift;
    private int machine_id;
    private double production;
    private int warpBreak;
    private int weftBreak;
    private int pickMissing;
    private int weftBar;
    private int floating;
    private int other;
    private int pickPerInches;
    private double ip=0;    
    private double pro1 =0;
    
protected LOG(String shift, int machine_id, double production, int warpBreak,
            int weftBreak, int picMissing, int weftBar, int floating, int other,
            int picksPerInches) {
        this.shift = shift;
        this.machine_id = machine_id;
        this.production = production;
        this.warpBreak = warpBreak;
        this.weftBreak = weftBreak;
        this.pickMissing = picMissing;
        this.weftBar = weftBar;
        this.floating = floating;
        this.other = other;
        this.pickPerInches = picksPerInches;
    }
 
    /*
     * First check is the SHIFT entry is done in the LOG table or not? If YES
     * then UPDATE it or else INSERT the new Entry.
     */
 
    @Override
    public boolean save() {
        DBConnection dbCon = Pool.getInstance().getDBConnection();
        Connection conn = dbCon.getConnection();
        long oneDayInMillies = 86400000;
        // Today's Time with 0.0.0 am
        DateTime dateTime = new           
        DateTime().withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0);
        logger.info("Today's Time with 0.0.0 am is: " + dateTime);
        Timestamp todaysTimestamp = new Timestamp(dateTime.getMillis());
 
        try {
 
            Statement st = conn.createStatement();
            // Checking that is ENTRY is there? & ya 1 day = 86 400 000 milliseconds
        
    String query = "SELECT production,id FROM LOG"
                    + " where shift='" + shift + "' and machine_id=" + machine_id
                    + " and LOG_TIMESTAMP > '" + todaysTimestamp + "'"
                    + " and LOG_TIMESTAMP < '" + new Timestamp(System.currentTimeMillis() + oneDayInMillies) + "'";
            ResultSet rs = st.executeQuery(query);
            
           
 
            // If ENTRY is there then update it & return else INSERT it
            if (rs.next()) {
               
                {
                logger.info("production is " + production);
                logger.info("before ip is " + ip);   
                Double match = rs.getDouble(1);
                logger.info(""+ match);
                pro1=production+match-ip;
                logger.info(" pro1 is " + pro1);
                query = "UPDATE LOG SET "
                        + "SHIFT = '" + shift + "', "
                        + "LOG_TIMESTAMP = '" + new Timestamp(System.currentTimeMillis()) + "', "
                        + "MACHINE_ID = " + machine_id + ","
                        + "PRODUCTION = " + pro1+ ", "
                        + "WARP_BREAK = " + warpBreak + ", "
                        + "WEFT_BREAK = " + weftBreak + ", "
                        + "MISSING_PICK = " + pickMissing + ", "
                        + "FLOATING = " + floating + ", "
                        + "OTHERS = " + other + ", "
                        + "WEFT_BAR = " + weftBar + ", "
                        + "PICKS_PER_INCHES = " + pickPerInches
                        + " WHERE ID = " + rs.getInt("id");
                logger.info("Going to Execute Query: " + query);
                st.execute(query);
                ip=production;
                logger.info(" now i is " + i);
                // Releasing instance which is consumed
                Pool.releaseConnection(dbCon);
                return true;
                }
                
                 //double b=i;
                 
            }
 
            // Now Insert
            query = "INSERT INTO LOG (SHIFT, LOG_TIMESTAMP, MACHINE_ID, PRODUCTION, WARP_BREAK, WEFT_BREAK, MISSING_PICK, FLOATING, OTHERS, WEFT_BAR, PICKS_PER_INCHES) "
                    + "VALUES ('"
                    + shift + "','"
                    + new Timestamp(System.currentTimeMillis()) + "',"
                    + machine_id + ","
                    + production + ","
                    + warpBreak + ","
                    + weftBreak + ","
                    + pickMissing + ","
                    + floating + ","
                    + other + ","
                    + weftBar + ","
                    + pickPerInches + ")";
            logger.info("Going to Execute Query: " + query);
            st.execute(query);
            Pool.releaseConnection(dbCon);
        } catch (SQLException ex) {
            logger.error("SQLException in the DBStore: " + ex);
            ex.printStackTrace();
            return false;
        }
        return true;
    }
}
Posted 8-Apr-13 3:15am
Edited 8-Apr-13 3:17am
(no name)249.6K
v2
Comments
Shubhashish_Mandal at 8-Apr-13 8:25am
   
In your app the variable ip is only set by the following statement .
ip = production;
So it is very easy to figure out where is the problem.Somehow the value of the production is "0".However if it is not work then post the log.
Sohushah at 9-Apr-13 0:58am
   
the value of production is not "0" because after ip=production; the value of ip is printed & that is not zero....but next time when the save function is called ip becomes "0" even if it is defined outside the function....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 8 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100