Delete statement with JDBC have no effect - sql

I am using oracle 19c on windows platform.
username and password is hr/hr
try{
//step1 load the driver class
Class.forName("oracle.jdbc.driver.OracleDriver");
//step2 create the connection object
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:#localhost:1521/orclpdb","hr","hr");
//step3 create the statement object
Statement stmt=con.createStatement();
int deletedRows = stmt.executeUpdate("DELETE FROM my_employee WHERE LAST_NAME LIKE '%man%'");
if(deletedRows>0){
System.out.println("Deleted Specific Row in the table successfully...");
}else{
System.out.println("Not exist specific row that you select for delete");
}
con.close();
}catch(Exception e){ System.out.println(e);}
Not working. There is no error showing, no syntax error.
But output here is:
Not exist specific row that you select for delete
After using select * from my_employee:
Here is the table row (with no effect of delete statement):
EMPLOYEE_ID FIRST_NAME LAST_NAME DEPARTMENT_ID SALARY
----------- -------------------- ------------------------- ------------- ----------
1 asaamansad asdamansda 12 21312
But same SQL command:
delete from my_employee where LAST_NAME LIKE '%man%';
This one works on command prompt or sql developer.
The above row got deleted when the same sql command used on command prompt.
Please solve this problem.

Related

How to run a string containing a static SQL query in PostgreSQL?

I have a function which generates a static SQL query and returns it as a string. What I need to do is to run the returned string as if I was typing it directly in psql. Here is a very simplified test case (the true mechanism is far more complicated but the following test case at least shows the idea)
drop table if exists person;
create table person(
first_name varchar(30),
last_name varchar(50),
email varchar(100)
);
insert into person(first_name, last_name, email) values
('fname01', 'lname01', 'fname01.lname01#hotmail.com'),
('fname02', 'lname02', 'fname02.lname02#hotmail.com'),
('fname03', 'lname03', 'fname03.lname03#hotmail.com'),
('fname04', 'lname04', 'fname04.lname04#hotmail.com'),
('fname05', 'lname05', 'fname05.lname05#hotmail.com');
--
--
drop function if exists sql_gen;
create function sql_gen() returns text as
$fun$
begin
return 'select * from person';
end;
$fun$
language plpgsql;
--
--
Now if I run the following:
select * from sql_gen();
sql_gen
----------------------
select * from person
(1 ligne)
This gives me the sql query. But what I want to do is to run the query from
the returned string as if I was writing manually select * from person in psql
and hitting Enter in order to obtain the result of the query.
I've checked PREPARE, EXECUTE on the online documentation but so far I've not been able to achieve this. Could you kindly make some clarification?
And answer specific for psql is to change your semicolon to \gexec.
select * from sql_gen() \gexec

FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. M

I am new to Hive. I have set up hive and I am using mysql to store the metadata for hive tables. I am able to launch hive. However when I try to create a simple table.
I get this error:
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. MetaException(message:Exception thrown when executing query : SELECT DISTINCT 'org.apache.hadoop.hive.metastore.model.MTable' AS NUCLEUS_TYPE,
Here is what I tried,
hive> create external table employee (FirstName string,LastName string, EmployeeId string)
row format delimited
fields terminated by ',';
I have been doing some research but no luck..What could I be doing wrong?

Oracle XE 11gR2 where my tables are located?

I have created some tables under a system as sysdba session using
create table mytable
(
ID char(20) not null,
val1 char(150),
val2 numeric(4)
);
when i m trying to query the tables everything works fine but when i m trying to look the contents of all_tables or user_tables with : eg. SELECT table_name from all_tables my tables are nowhere to be found...
Why is that? am i missing something?
What i actualy want to do is to calculate the used blocks from a table i created
and the query i use is :
SELECT blocks - empty_blocks
FROM user_tables
WHERE table_name = 'mytable';
which because of the problem i explained above does not work.
Firstly,
Open cmd and write sqlplus then connect to user system like this system as sysdba,
Secondly,
create a new user TEST with password TEST with this command create user TEST identified by TEST; then grant all privileges to the user like this grant all privileges to TEST;
Finally,
connect to the new user with this command conn TEST/TEST and create your table

"Maximum open cursors exceeded": How to know for some DB object how many cursors are open?

In Finacle, sometimes users are facing "The resource is busy" error due to the oracle error generated in back-end as "Maximum open cursors exceeded". Not that this error comes often, but we want to take precautionary measures and that is why we are trying to generate an alert for the Finacle engineers i.e. for ourselves so that whenever the maximum no of cursors exceeds for some DB object, we can get the alert. For that, we need to know what is the command to know how many cursors are open for the DB objects at a given time. Can somebody guide us on this matter please.
Another thing to clear my concept, does Oracle use any default cursor for querying a table? Because, my guess is this error is generating because number of users for this software has increased and there are lots of sessions getting opened for the same DB object with select and update query and that is how number of open cursors is getting increased.
I guess you don't close your cursors after you use it, For example, I customized the open_cursor parameter to allow user to have only one opened cursor (3 cursors oracle uses for its own needs):
SQL> conn / as sysdba
Connected.
SQL> alter system set open_cursors=4 scope=memory
2 /
System altered.
SQL> conn hr/hr
Connected.
SQL> var l_cursor refcursor;
SQL> ed
Wrote file afiedt.buf
1 begin
2 open :l_cursor for select 1 from dual;
3* end;
SQL> /
PL/SQL procedure successfully completed.
SQL> var l_cursor1 refcursor;
SQL> ed
Wrote file afiedt.buf
1 begin
2 open :l_cursor1 for select 1 from dual;
3* end;
SQL> /
begin
*
ERROR at line 1:
ORA-01000: maximum open cursors exceeded
As you see I face the same error you had. Let's close the l_cursor cursor (the print command displays a cursor's content and closes it) and then open l_cursor1 once again:
SQL> print l_cursor
1
----------
1
SQL> ed
Wrote file afiedt.buf
1 begin
2 open :l_cursor1 for select 1 from dual;
3* end;
SQL> /
PL/SQL procedure successfully completed.
SQL> print l_cursor1
1
----------
1
As you can see Oracle can successfully open the l_cursor1 cursor.
As a quick solution of your problem you can increase the open_cursor parameter with command (you system might need additional resources to keep the current performance):
alter system set open_cursors=800 scope=both
I used scope=memory, because I want this demo doesn't affect my system after the database server is restarted. You have to specify scope=both to persist this parameter in spfile. But I guess you have to exam your system and find out if you have opened cursors that aren't used anymore.
Your guess about "does Oracle use any default cursor for querying a table?" is true, Oracle uses cursors to query tables, you can make sure:
SQL> var l_cursor refcursor;
SQL> ed
Wrote file afiedt.buf
1 begin
2 open :l_cursor for select 1 from dual;
3* end;
SQL> /
PL/SQL procedure successfully completed.
SQL> select * from employees;
select * from employees
*
ERROR at line 1:
ORA-00604: error occurred at recursive SQL level 1
ORA-01000: maximum open cursors exceeded
SQL> print l_cursor
1
----------
1
SQL> select * from employees;
EMPLOYEE_ID FIRST_NAME LAST_NAME
----------- -------------------- -------------------------
EMAIL PHONE_NUMBER HIRE_DAT JOB_ID SALARY
------------------------- -------------------- -------- ---------- ----------
COMMISSION_PCT MANAGER_ID DEPARTMENT_ID
-------------- ---------- -------------
205 Shelley Higgins
...
To get all opened cursors check this answer

what is the statement for "where" condtion in pl/sql

I want to make a trigger that will be executed for each row before delete on the table Clients, what I'm trying to do is when some one tries to delete a row :
DELETE FROM Clients WHERE id=5 AND name = 'test';
I want to print something on the screen like :
"you deleted with the following conditions : id is 5 , name is test "
or execute another query with same conditions ...
Any kind of help is very appreciated
EDIT
let's suppose the user typed the following query : DELETE FROM Clients where name = 'test'
create or replace
TRIGGER DELETECLIENT
BEFORE DELETE ON CLIENTS
DECLARE
pragma autonomous_transaction;
name1 clients.name%Type;
BEGIN
select name into name1 from clients where name = %I want here the name in the user query %;
IF name1 != null THEN
DELETE FROM clients WHERE name = name1;
commit;
END IF;
END;
what I've tested :
i tried adding :old.name , and :new.name , but this doesn't work when the the row doesn't exist in my database
PS : i'm doing this for educational purpose only, I know the script doesn't make sense. i just wan't to know how to achieve the following task : getting the parameters typed after WHERE in the query
The correlation names and pseudorecords the represent, i.e. :old and :new by default, only have any meaning for row-level triggers:
Note:
This topic applies only to triggers that fire at row level—that is, row-level simple DML triggers and compound DML triggers with row-level timing point sections.
You cannot refer to these in a statement-level trigger, and only :old would have any meaning in a delete trigger. On the other hand, statement-level triggers fire even if no data is affected, whereas a row-level trigger won't fire if, in your example, no data is deleted - because there are no matching rows in your table. (This applies for instead of triggers for views as well, in case you're wondering if that would be a workaround).
So basically you can't do what you're attempting - conditionally deciding whether to delete from another table instead - from a trigger. Triggers are rarely the right answer. For your 'scenario where if it doesn't exist on my table i should try to find it on other site and delete it' you can try to delete a row locally, test whether it did anything, and then decide to delete from the remote table if it didn't. In a PL/SQL block that might be as simple as:
create procedure delete_client(p_name clients.name%type) as
begin
delete from clients where name = p_name;
if sql%rowcount = 0 then
delete from clients#site2 where name = p_name;
end if;
end delete_client;
/
And rather than deleting directly from the table, you make everyone call the procedure instead. Say you start off with some data:
select * from clients;
ID NAME
---------- ----------
1 Joe
2 Anna
select * from clients#site2;
ID NAME
---------- ----------
1 Joe
3 Max
Then calling the procedure for two names:
exec delete_client('Joe');
exec delete_client('Max');
... has removed local and remote records appropriately:
select * from clients;
ID NAME
---------- ----------
2 Anna
select * from clients#site2;
ID NAME
---------- ----------
1 Joe
Joe was only deleted from the local schema despite existing in both; Max didn't exist locally so was deleted from the remote schema.
It doesn't have to be a procedure; if you're deleting through JDBC etc. you can test the result of an execute() call to see how many rows were affected, which is all sql%rowcount is doing really, and the application code can decide whether to do the second delete.
But with a procedure (probably in a package) you can grant execute on that, and remove delete privileges from the users, so they can't bypass this check and do a straight delete from clients where ...
If you really want some 'display' output for testing purposes:
create procedure delete_client(p_name clients.name%type) as
begin
delete from clients where name = p_name;
if sql%rowcount > 0 then
dbms_output.put_line('Deleted ' || sql%rowcount
|| ' rows from local schema for "' || p_name || '"');
else
delete from clients#site2 where name = p_name;
if sql%rowcount > 0 then
dbms_output.put_line('Deleted ' || sql%rowcount
|| ' rows from remote schema for "' || p_name || '"');
else
dbms_output.put_line('No rows deleted on local or remote schema for "'
|| p_name || '"');
end if;
end if;
end delete_client;
/
set serveroutput on
exec delete_client('Joe');
anonymous block completed
Deleted 1 rows from local schema for "Joe"
exec delete_client('Max');
anonymous block completed
Deleted 1 rows from remote schema for "Max"
exec delete_client('Fred');
anonymous block completed
No rows deleted on local or remote schema for "Fred"
But you shouldn't assume anyone calling your procedure will have serveroutput on, or even be using a client capable of consuming dbms_output messages.