2

can anyone please help me on this? I am trying to execute a SQL query (to Microsoft SQL) using JAVA. But, nothing happens on my table. Also, no exception either. But i am pretty sure that this code directly connecting to my DB. Here's my code.

package Testing;
//import java.sql.*;
import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

public class TestingClass {
   public static void main(String[] srg) 
   {
       String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // Start JDBC
       String dbURL = "jdbc:sqlserver://localhost:1433;DatabaseName=OPERATIONS";  //  Connect the server and the database
       String userName="AppControlTeam";
       String userPwd="*****";
       //
       Connection connection = null;
       try{
           Class.forName(driverName);
           connection = DriverManager.getConnection(dbURL,userName,userPwd); 
           String sql = "DELETE FROM GFR_GROWTH_RATE";
           PreparedStatement statement = connection.prepareStatement(sql);
           statement.executeUpdate();
           connection.close();
       }
       catch (Exception e){
           e.printStackTrace();
       }

}
}

I have been already added the JDBC driver on the package. Also, i'm pretty sure that this is not a classpath issue since the connection to DB thru this code is already success.

Thanks in advance to someone who can help! :D

-Jiro

0

2 Answers 2

3

Your DELETE may be rolled back if you didn't set your JDBC driver's autocommit property to true. Maybe try calling

connection.commit();
// right before...
connection.close();

Or alternatively:

connection.setAutoCommit(true);
// before...
PreparedStatement statement = connection.prepareStatement(sql);
Sign up to request clarification or add additional context in comments.

5 Comments

Still nothing happens.. is there anything else i need to check? Please help me i'm begging you.. :(
What's the int result of statement.executeUpdate()? Can you execute the same statement directly on the database?
There's no result for int samplevariable = statement.executeUpdate(). Actually it hangs right after the String sql = "DELETE FROM GFR_GROWTH_RATE";. I think there's something wrong right after that statement. Also, it is success when executed on the workbench of MS SQL.
Hmm, hard to say. From the JDBC perspective, there's probably nothing wrong. You'll have to find out why your statement hangs (prepared statement, or execution)... The problem might be on the database side...
:( ok.. i hope i find this out soon.. thank you by the way! God Bless!
1

Probably the row is locked by a different session, may be an uncommitted session that has modified (deleted/updated) the records from the table. Another possibility is by a tool which open the row/record with locked state - so the delete statement hangs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.