Hi,
I have written a piece of code to add site for my web based application.If the site entered is already present then flag should be false and it should exit from function.
If new site id is not present in db then flag should be true and addtion must take place.

public boolean adds(String id,String desc)
    {


            boolean flag=true;
            System.out.println("---in java--------"+id);
            System.out.println("---in java--------"+desc);


            String jdbcDriver;
            String connectionURLThin;
            String DbUserId;
            String DbUserPassword;

            Connection con = null;


                try {

            Properties prop=new Properties();
            prop.load(new FileInputStream("C:\\DbConnection.properties"));
            jdbcDriver=prop.getProperty("jdbcDriver");
            System.out.println("ANKITA >>>> " + jdbcDriver);
                    Class.forName(jdbcDriver);

            connectionURLThin=prop.getProperty("connectionURLThin");
            System.out.println(connectionURLThin);


            DbUserId=prop.getProperty("DbUserId");
            System.out.println(DbUserId);


            DbUserPassword=prop.getProperty("DbUserPassword");
            System.out.println(DbUserPassword);


            System.out.println("hi2");
            con=DriverManager.getConnection(connectionURLThin,DbUserId,DbUserPassword);

            Statement stmt=con.createStatement();
            String query="select * from AIP_QM_SITE_MST";
                    ResultSet rst=stmt.executeQuery(query);
                    while(rst.next()){


                                            String id1=rst.getString("AQSM_SITE_ID");

                                if(id1.equals(id)){ 
                                            System.out.println("---site id is--------->>>>>"+id1);
                                            flag=false;
                                            System.out.println("--site id already present--->>>>"+flag);
                                            break;
                                }
                    }







                    if(flag == true){
                        System.out.println("-site added--->>>>"+flag);
                        String command="insert into AIP_QM_SITE_MST(AQSM_SITE_ID,AQSM_SITE_DESC) values (?,?)";

                            PreparedStatement stat=con.prepareStatement(command);


                            stat.setString (1,id);
                                stat.setString (2,desc);

                                stat.executeUpdate();
                    }



        }


              catch(Exception e){
            e.printStackTrace();
        }
          return flag;

    }

The code is workin fine except it executes twice.So the site that i give comes as already present in db the second time around and correspondingly error msg is displayed!!!..but it does get added in db.How can i ensure that execution should stop once id is found/not found???..please help..

don't really see the problem in there, but didn't look that thoroughly either. you might try to format your code a bit, using the code tags. I'm sure a lot more of developers present will be pursuaded to read your code that way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.