Stack Overflow Questions Tags Users Badges Unanswered Ask Question Getting image from server and saving to MySQL DB
Posted by: Damian Dorame
Date: November 17, 2014 10:59AM

Im getting an image from server as InputStream and then saving it to mySQL database. It works when I use `Thread.sleep(5000);`. But if I dont use it no picture is saved to the DB or only one picture and half of it or less. So I understand that the program needs time writing image to the database, but how much time? This is the question, I would like to know exactly when it finished writing image to the database and can start with the next image. Below is my code:

ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {

int ID = rs.getInt(1);
String myName = rs.getString(2);

try {

String myCommand = "take picture and save /mydir/mydir2/mydir3" + myName + ".png";
telnet.sendCommand(myCommand); // Here taking a picture via telnet
// Thread.sleep(5000);// If I uncomment this line it works

String sqlCommand = "UPDATE my_table SET Picture = ? WHERE ID ='" + ID +"';";
PreparedStatement statement = conn.prepareStatement(sqlCommand);

String ftpUrl = "ftp://"+server_IP+"/mydir/mydir2/mydir3"; + myName + ".png;type=i";

URL url = new URL(ftpUrl);
URLConnection connUrl = url.openConnection();

//Thread.sleep(5000); // If I uncomment this line, it works too.

InputStream inputStreamTelnet = connUrl.getInputStream();

statement.setBlob(1, inputStreamTelnet);
int row = statement.executeUpdate();
if (row > 0) {
System.out.println("A picture was inserted into DB.");
System.out.println("Value of row(s) : " + row);
}

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

} // End of while

I would expect to put the waiting(sleep) after `InputStream inputStreamTelnet = connUrl.getInputStream();` but it doesnt work when I put the sleep after this line. It works only when the sleep is before. Could someone explain me why and I would like to avoid using `Thread.sleep(5000);` and instead would like to wait exact time or not wait at all which will make the program faster also there might be a case saving the picture can take more than 5 seconds or maybe saving the picture doesnt take time but opening the url connection. There are 2 sleep lines on the code when I uncomment one of them the program works(saves the images to mysql DB successfully). I also verified on the server that the images exist but in the end I dont see them in the mysql DB.

MySQL version: '5.5.37-0ubuntu0.12.04.1'
MySQL connector : mysql-connector-java-5.1.7-bin
Java version : 1.8 32bit

Options: ReplyQuote


Subject
Written By
Posted
Stack Overflow Questions Tags Users Badges Unanswered Ask Question Getting image from server and saving to MySQL DB
November 17, 2014 10:59AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.