I have this program, sending request to receive weather information from the web in JSON format. I will sending one request per city in a for loop. However after the first JSON is received and parsed whenever it tries to parse the second one it gives end of file error and does not write the information into the database. What might be the problem ?
public static void getHistory()
{
Connection connection = connectToDatabase();; // manages connection
System.out.println("Getting History Data");
//Loops through every single city and requests, parses and inserts the history weather data.
PreparedStatement pStatement = null;
Scanner scanner = new Scanner(System.in);
for(int i = 35 ; 0 < cities.length ; i++)
{
int errorCount = 0;
int userInput;
System.out.printf("City no : %d. 1 to continue\n", i+1);
JSONParser parser = new JSONParser();
JSONObject jsonObj = new JSONObject();
Object obj = new Object();
String resultString = new String();
userInput = scanner.nextInt();
if(userInput != 1)
{
System.out.println("Terminating the program!");
break;
}else if(userInput == 1)
{
System.out.println("Continue...");
}
System.out.println("For Loop");
System.out.printf("!%d: %s!\n", i, cities[i]);
resultString = sendRequestForHistoryData(cities[i]);
try
{
obj = parser.parse(resultString);
jsonObj = (JSONObject)obj;
parseHistoryData(jsonObj);
}
catch(ParseException pe)
{
System.out.println("position: " + pe.getPosition());
System.out.println(pe);
}
//Iterates through all the WeatherData objects and writes them into the database.
Iterator<WeatherData> vectorIterator = parsedWeatherObjects.iterator();
System.out.printf("Vector Length: %d\n", parsedWeatherObjects.size());
Statement statement = null;
while(vectorIterator.hasNext())
{
WeatherData weather = vectorIterator.next();
/*
* Table Structure for HistoryData Table in SQL Server
* 1) City_ID - int
* 2)City_Name - nvarchar(50)
* 3)Time - datetime
* 4)Received_Time - datetime
* 5)Humidity - float
* 6)Rain_info - float
* 7)Snow_info - float
* 8)Temperature - float
* 9)Min_Temperature - float
* 10)Max_Temperature - float
* 11)Description - nvarchar(50)
*/
try
{
String statementString = "INSERT INTO WeatherHistory("
+"City_ID,"
+"City_Name,"
+"Time,"
+"Received_Time,"
+"Humidity,"
+"Rain_Info,"
+"Snow_Info,"
+"Temperature,"
+"Min_Temperature,"
+"Max_Temperature,"
+"Description)"
+"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
pStatement = connection.prepareStatement(statementString);
java.sql.Date sqlDate = new java.sql.Date(weather.getDate());
java.sql.Date currentSqlDate = new java.sql.Date(currentTimestamp.getTime());
//java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(weather.getDate());
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+2"));
String formattedDate = convertEpochToDateString(weather.getDate());
//System.out.printf("Formatted Date: " + formattedDate + "\n");
java.sql.Timestamp sqlTimestamp = Timestamp.valueOf(formattedDate);
pStatement.setInt(1, weather.getCityCode());
pStatement.setString(2, weather.getCity());
pStatement.setTimestamp(3, sqlTimestamp, cal);
pStatement.setDate(4, currentSqlDate);
pStatement.setFloat(5, weather.getHumidity());
pStatement.setFloat(6, weather.getRainInfo());
pStatement.setFloat(7, weather.getSnowInfo());
pStatement.setFloat(8, weather.getTemperature());
pStatement.setFloat(9, weather.getMinTemperature());
pStatement.setFloat(10, weather.getMaxTemperature());
pStatement.setString(11, weather.getDescription());
pStatement.execute();
}
catch (SQLException e)
{
// TODO Auto-generated catch block
//e.printStackTrace();
//System.out.println("SQL Key error.");
errorCount++;
}
}//While Loop End
System.out.printf("SQL Error Count: %d\n", errorCount);
try
{
if (pStatement != null)
{
pStatement.close();
System.out.println("Statement close.");
}
}catch (SQLException ex)
{
// TODO Auto-generated catch block
ex.printStackTrace();
}
}//For Loop End
try
{
if (connection != null)
{
connection.close();
System.out.println("Connection close.");
}
} catch (SQLException ex)
{
// TODO Auto-generated catch block
ex.printStackTrace();
}
}//Function End