java - Why does deletion of rows from one table work, but not from another? -
i trying delete rows tables. code in first program succeeds in deleting row. code in second, similar program deletes 0 rows.
however, when issuing same delete command sql command line tool, reports 1 row being deleted.
first program:
package oracle; import java.sql.drivermanager; import java.sql.connection; import java.sql.preparedstatement; import java.sql.sqlexception; import java.text.dateformat; import java.text.simpledateformat; public class jdbcpreparedstatementinsertexample { private static final string db_driver = "oracle.jdbc.driver.oracledriver"; private static final string db_connection = "jdbc:oracle:thin:@localhost:1521:xe"; private static final string db_user = "system"; private static final string db_password = "datta123"; public static void main(string[] argv) { try { insertrecordintotable(); } catch (sqlexception e) { system.out.println(e.getmessage()); } } private static void insertrecordintotable() throws sqlexception { connection dbconnection = null; preparedstatement preparedstatement = null; try { dbconnection = getdbconnection(); system.out.println("oracle database connected:\n"); string deletesql = "delete dbuser username = ?"; preparedstatement = dbconnection.preparestatement(deletesql); preparedstatement.setstring(1, "avi"); // execute delete sql stetement int = preparedstatement.executeupdate(); system.out.println("success:"+i); } catch (sqlexception e) { system.out.println(e.getmessage()); } { if (preparedstatement != null) { preparedstatement.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; } }
output:
oracle database connected: success:1
second program:
package oracle; import java.sql.drivermanager; import java.sql.connection; import java.sql.preparedstatement; import java.sql.sqlexception; import java.text.dateformat; import java.text.simpledateformat; public class jdbcpreparedstatementinsertexample { private static final string db_driver = "oracle.jdbc.driver.oracledriver"; private static final string db_connection = "jdbc:oracle:thin:@localhost:1521:xe"; private static final string db_user = "system"; private static final string db_password = "datta123"; public static void main(string[] argv) { try { insertrecordintotable(); } catch (sqlexception e) { system.out.println(e.getmessage()); } } private static void insertrecordintotable() throws sqlexception { connection dbconnection = null; preparedstatement preparedstatement = null; try { dbconnection = getdbconnection(); system.out.println("oracle database connected:\n"); string deletesql = "delete admin name = ?"; preparedstatement = dbconnection.preparestatement(deletesql); preparedstatement.setstring(1, "datta"); // execute delete sql stetement int = preparedstatement.executeupdate(); system.out.println("success:"+i); } catch (sqlexception e) { system.out.println(e.getmessage()); } { if (preparedstatement != null) { preparedstatement.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; } }
output:
oracle database connected: success:0
but command line tool, same query executed successfully:
sql> delete admin name='datta'; 1 row deleted.
Comments
Post a Comment