Reputation: 836
I am trying to insert/update/delete a row using jdbc. Inserting is working, and I only changed the query string (or insertTableSQL) By debugging, I suspect that executeUpdate() is not terminated and console does not show me any error message. (JUST BLANK)
[EDIT]
The program is stuck while executeUpate() is exeucting, meaning that I cannot even see the return value
Instead of using query string, I also tried PreparedStatement but no luck :(
String deleteRecordSQL = "DELETE mytable WHERE id = ?";
PreparedStatement ps = dbConnection.prepareStatement(deleteRecordSQL);
ps.setInt(1, 6);
ps.executeUpdate();
Full Code:
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class DeleteRow {
private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String DB_CONNECTION = "";
private static final String DB_USER = "";
private static final String DB_PASSWORD = "";
public static void main(String[] argv) {
try {
deleteRecordFromDbUserTable();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
private static void deleteRecordFromDbUserTable() throws SQLException {
Connection dbConnection = null;
Statement statement = null;
try {
dbConnection = getDBConnection();
statement = dbConnection.createStatement();
statement.executeUpdate("DELETE mytable WHERE id = 6");
System.out.println("Record is deleted!");
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
if (statement != null) {
statement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
}
private static Connection getDBConnection() {
Connection dbConnection = null;
try {
Class.forName(DB_DRIVER);
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
try {
dbConnection = DriverManager.getConnection(
DB_CONNECTION, DB_USER,DB_PASSWORD);
return dbConnection;
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return dbConnection;
}
}
Any help?
Upvotes: 1
Views: 11439
Reputation: 41
Since its stuck it must be waiting for some resource which might be locked. I had faced this issue for simple update query. Here the update query was run on sql developer tool and the commit was not done.
Thanks to a comment by Prabhmanmeet Singh on this link
Upvotes: 1
Reputation: 836
THIS IS NOT THE OFFICIAL ANSWER, BUT THE PROBLEM IS RESOLVED SOMEHOW
There was Java update notification, and I followed it. After update, the status of JRE System Library(JavaSE - 1.7) is changed to unbound. I changed Execution environment to JavaSE - 1.6 (Oracle WebLogic Server 11gR1 (10.3.6) JRE), and the issue was resolved..weird..
There was no syntax problem
DELETE FROM mytable WHERE id = 6
DELETE mytable WHERE id = 6
If anyone can explain me how this issue could be resolved, your help would be much appreciated.
FYI.
C:\>java -version
java version "1.7.0_67"
Java(TM) SE Runtime Environment (build 1.7.0_67-b01)
Java HotSpot(TM) Client VM (build 24.65-b04, mixed mode, sharing)
Upvotes: 2
Reputation: 310850
If you didn't get an exception, which you don't state one way or the other, you need to examine the return value of executeUpdate()
, which is the number of affected rows. If it was zero, no rows were deleted, probably because nothing matched the WHERE
clause. If it was non-zero, that many rows were deleted. You certainly shouldn't be printing "Record is deleted!"
when nothing may have happened at all. Your program may be lying to the user.
NB insertTableSQL
is a stupid name for a variable containing an SQL DELETE
statement.
Upvotes: 0