troubleshooting Question

A simple Prepared Statement problem in Java

Avatar of Sreejith22
Sreejith22Flag for India asked on
JavaJava EE
4 Comments1 Solution470 ViewsLast Modified:
Hi,
I have a recursive function which is given below.
It works perfectly.
So, I have no questions concerned with the function.
The only question I have is related with the Prpared statement used inside this function.
As you can see, I am adding data to the database using this prepared statement.
The problem is that, it  adds duplicate values to the database. ie; I have only 56 entries which need to be added to the database. Instead of that 1069 entries are entered due to duplication.
I counted the number of times the data gets printed(using SOP's) and it also shows, only 56 entries which shows that the recursive function is perfect.
So, what is the problem that occurs during inserting values to the database using prepared statement.

The prepared statement is initialised only once >>prep = conn.prepareStatement("insert into user_file_info values (?, ?, ?, ?);");

Do i need to initialise each time?

Kindly help.
Any help in this regard will be well appreciated with points.
Regards,
Sreejith
public void constructChildren(String dir) throws IOException, SQLException
	{
		Vector list = sftpclient.ls(dir);
		Iterator children = list.iterator();
		DecimalFormat df = new DecimalFormat("#.##");
		while(children.hasNext())
		{
			SFTPv3DirectoryEntry child = (SFTPv3DirectoryEntry)children.next();
			if(child.attributes.isDirectory()){
 
				if(!child.filename.matches("^[.]{1}[a-zA-Z0-9]+")
						&& !child.filename.equals(".")
						&& !child.filename.equals("..")
						&& !child.filename.startsWith(".")){
					System.out.println("filename issssssssssssssssssssssssss 1 "+child.filename );
					
					String size = "";
					if(child.attributes.size<1000)
						size = child.attributes.size + " bytes" ;
					else if(child.attributes.size<1000000)
						size = df.format(((Double)(child.attributes.size/1000.0))).toString() + " Kb";
					else size = df.format(((Double)(child.attributes.size/1000000.0))).toString() + " Mb";
 
 
					prep.setString(1, child.filename);
					prep.setString(2, size);
					prep.setString(3, dir);
					prep.setString(4, DateToOrdiServer(new Date(child.attributes.mtime*1000L)).toString());
					prep.addBatch();
					
					if(! dir.equals("/"))
						constructChildren(dir + "/" + child.filename);
					else
						constructChildren( "/" + child.filename);
				}
 
			}
 
			else{
				System.out.println("filename issssssssssssssssssssssssss 1 "+child.filename );
				String size = "";
				if(child.attributes.size<1000)
					size = child.attributes.size + " bytes" ;
				else if(child.attributes.size<1000000)
					size = df.format(((Double)(child.attributes.size/1000.0))).toString() + " Kb";
				else size = df.format(((Double)(child.attributes.size/1000000.0))).toString() + " Mb";
 
 
 
				prep.setString(1, child.filename);
				prep.setString(2, size);
				prep.setString(3, dir);
				prep.setString(4, DateToOrdiServer(new Date(child.attributes.mtime*1000L)).toString());
				prep.addBatch();
			}
 
		}
		conn.setAutoCommit(false);
		prep.executeBatch();
		conn.setAutoCommit(true);
	}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros