I'm using a PreparedStatement
to Insert a row into my remote database.
I'm getting an issue where after I finish preparing my PreparedStatement
, and use its .executeUpdate()
method, I do not get anything returned. No exception, value, or error of any sort. My database is working perfectly fine as I can still make queries to it.
I've logged statements in my code to see where the issue is:
as soon as I call int i = statement.executeUpdate();
I never get anything returned.
The exception
catch (SQLException e) {
System.out.println("Error: " + e);
return null;
}
is also never called.
I have also tried using a timeout for the PreparedStatement
of 10 seconds
statement.setQueryTimeout(10);
but unfortunately does not display any timeout errors. Any idea why this is happening?
String query = "INSERT INTO appointments(Title, Description, Location, Type, Start, End, " +
"Created_By, Last_Updated_By, Customer_ID, User_ID, Contact_ID) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
connection = DriverManager.getConnection(Constants.CONNECTION_URL);
statement = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
System.out.println("Called here");
//Have tried setting a timeout as well, but it doesn't do anything
statement.setQueryTimeout(10);
statement.setString(1, appointment.getTitle());
statement.setString(2, appointment.getDescription());
statement.setString(3, appointment.getLocation());
statement.setString(4, appointment.getType());
statement.setString(5, appointment.getStartDateAndTime());
statement.setString(6, appointment.getEndDateAndTime());
statement.setString(7, LoginManager.getUsername());
statement.setString(8, LoginManager.getUsername());
statement.setInt(9, appointment.getCustomer().getCustomerId());
statement.setInt(10, LoginManager.getUserId());
statement.setInt(11, appointment.getContact().getContactId());
//This Println is never called for some reason.
System.out.println("Called here two.");
int i = statement.executeUpdate();
//Never finishes beyond this point
//Exception is never called
System.out.println("ExecuteUpdate results: " + i);
if (i > 0) {
//success
resultSet = statement.getGeneratedKeys();
System.out.println("Success: " + resultSet.getInt(1));
if (resultSet.next()) {
appointment.setAppointmentId(resultSet.getInt(1));
}
return appointment;
} else {
return null;
}
} catch (SQLException e) {
System.out.println("Error: " + e);
return null;
} finally {
closeConnection(connection, statement, resultSet);
}
}
public static void closeConnection(Connection connection, Statement statement, ResultSet resultSet) {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}