Using PreparedStatement to execute a SQL Query
hi All,
I am trying to use PreparedStatement to execute a Query in Java.
The Problem is that where Clause of that Query is dynamically formed as per
user inputs .
So , in this case will it help if I use PreparedStatement in place of Normal Statement ?
If Yes , then how to handle the Dynamic where clause of the Query ?
Thanks in Advance.
Regards,
ninad
Let's say the user is providing a name, and you'r querying based on that name: PrepartedStatement ps = con.prepareStatement("select * from my_table where name = ?);
ps.setString(1, nameFromUser);
ResultSet rs = ps.executeQuery();
http://java.sun.com/developer/onlineTraining/Database/JDBC20Intro/
Similar Messages
-
How to execute an SQL query present in a string inside an ABAP program?
hello,
How to execute an SQL query present in a string inside an ABAP programRaut,
You can execute Native SQl statements.
Ex: To use a Native SQL statement, you must precede it with the EXEC SQL statement, and follow it with the ENDEXEC statement as follows:
EXEC SQL [PERFORMING <form>].
<Native SQL statement>
ENDEXEC.
There is no period after Native SQL statements. Furthermore, using inverted commas (") or an asterisk (*) at the beginning of a line in a native SQL statement does not introduce a comment as it would in normal ABAP syntax. You need to know whether table and field names are case-sensitive in your chosen database.
In Native SQL statements, the data is transported between the database table and the ABAP program using host variables. These are declared in the ABAP program, and preceded in the Native SQL statement by a colon (:). You can use elementary structures as host variables. Exceptionally, structures in an INTO clause are treated as though all of their fields were listed individually.
If the selection in a Native SQL SELECT statement is a table, you can pass it to ABAP line by line using the PERFORMING addition. The program calls a subroutine <form> for each line read. You can process the data further within the subroutine.
As in Open SQL, after the ENDEXEC statement, SY-DBCNT contains the number of lines processed. In nearly all cases, SY-SUBRC contains the value 0 after the ENDEXEC statement. Cursor operations form an exception: After FETCH, SY-SUBRC is 4 if no more records could be read. This also applies when you read a result set using EXEC SQL PERFORMING.
EXEC SQL PERFORMING loop_output.
SELECT connid, cityfrom, cityto
INTO :wa
FROM spfli
WHERE carrid = :c1
ENDEXEC.
Pls. Mark If useful -
It is possible to execute a SQL query from OMBPlus?
We have some simple SQL scripts we'd like to be able to incorporate into our existing TCL scripts, instead of having to run them outside of OMBPlus in sqlplus. Is is possible to run a SQL SELECT query from OMBPlus and/or TCL? And get a result set back to iterate through?
Thanks,
Jim C.You can execute SQLPlus from OMB or just get down and use JDBC to execute the SQL, see others posts on forum such as below;
How to run SQL from OMB+
Cheers
David -
Submit button executes a sql query
In my servlet I'd like to create a submit button, and when it is clicked I want to execute an sql query.
For example;
If I write a name; I want to add that name to my table in my database.
Could you give an example please?
Thank youPreparedStatement ps = con.prepareStatement("insert into foo (name, birthdate) values (?, ?)"); // double check my sql insert syntax. I'm not sure if it's right.
String theName = ...; // get name from what user enters
Date theBirthDate =...; // likewise birthdate
ps.setString(1, theName);
ps.setDate(2, theBirthDate);
ps.executeUpdate(); Look at the APIs or google for java preparedstatement tutorial or something for more info. -
Executing PL-SQL Query before running a Report
Hi Guys,
Can i use a Run a Pl-Sql Query every time before executing a report. Like if i am opening a report then the pl-sql Query should run before it.
Thanks
Rondo.Try with Evaluate as one of the column's expression.. so that we can limit to specific report..
or
Use Connection Pool->Connection Pool Scripts tab->use 'Execute before query'
Since we can set the repository variables using advanced tab.. the same variable we may validate using Connection pool scripts tab.
This might be more dev work..
Edited by: Srini VEERAVALLI on Dec 17, 2012 9:00 PM -
Error while executing a sql query for select
HI All,
ORA-01652: unable to extend temp segment by 128 in tablespace PSTEMP i'm getting this error while i'm executing the sql query for selecting the data.I am having 44GB of temp space, while executing the below query my temp space is getting full, Expert please let us know how the issue can be resolved..
1. I dont want to increase the temp space
2. I need to tune the query, please provide your recomendations.
insert /*+APPEND*/ into CST_DSA.HIERARCHY_MISMATCHES
(REPORT_NUM,REPORT_TYPE,REPORT_DESC,GAP,CARRIED_ITEMS,CARRIED_ITEM_TYPE,NO_OF_ROUTE_OF_CARRIED_ITEM,CARRIED_ITEM_ROUTE_NO,CARRIER_ITEMS,CARRIER_ITEM_TYPE,CARRIED_ITEM_PROTECTION_TYPE,SOURCE_SYSTEM)
select
REPORTNUMBER,REPORTTYPE,REPORTDESCRIPTION ,NULL,
carried_items,carried_item_type,no_of_route_of_carried_item,carried_item_route_no,carrier_items,
carrier_item_type,carried_item_protection_type,'PACS'
from
(select distinct
c.REPORTNUMBER,c.REPORTTYPE,c.REPORTDESCRIPTION ,NULL,
a.carried_items,a.carried_item_type,a.no_of_route_of_carried_item,a.carried_item_route_no,a.carrier_items,
a.carrier_item_type,a.carried_item_protection_type,'PACS'
from CST_ASIR.HIERARCHY_asir a,CST_DSA.M_PB_CIRCUIT_ROUTING b ,CST_DSA.REPORT_METADATA c
where a.carrier_item_type in('Connection') and a.carried_item_type in('Service')
AND a.carrier_items=b.mux
and c.REPORTNUMBER=(case
when a.carrier_item_type in ('ServicePackage','Service','Connection') then 10
else 20
end)
and a.carrier_items not in (select carried_items from CST_ASIR.HIERARCHY_asir where carried_item_type in('Connection') ))A
where not exists
(select *
from CST_DSA.HIERARCHY_MISMATCHES B where
A.REPORTNUMBER=B.REPORT_NUM and
A.REPORTTYPE=B.REPORT_TYPE and
A.REPORTDESCRIPTION=B.REPORT_DESC and
A.CARRIED_ITEMS=B.CARRIED_ITEMS and
A.CARRIED_ITEM_TYPE=B.CARRIED_ITEM_TYPE and
A.NO_OF_ROUTE_OF_CARRIED_ITEM=B.NO_OF_ROUTE_OF_CARRIED_ITEM and
A.CARRIED_ITEM_ROUTE_NO=B.CARRIED_ITEM_ROUTE_NO and
A.CARRIER_ITEMS=B.CARRIER_ITEMS and
A.CARRIER_ITEM_TYPE=B.CARRIER_ITEM_TYPE and
A.CARRIED_ITEM_PROTECTION_TYPE=B.CARRIED_ITEM_PROTECTION_TYPE
AND B.SOURCE_SYSTEM='PACS'
Explain Plan
==========
Plan
INSERT STATEMENT ALL_ROWSCost: 129 Bytes: 1,103 Cardinality: 1
20 LOAD AS SELECT CST_DSA.HIERARCHY_MISMATCHES
19 PX COORDINATOR
18 PX SEND QC (RANDOM) PARALLEL_TO_SERIAL SYS.:TQ10002 :Q1002Cost: 129 Bytes: 1,103 Cardinality: 1
17 NESTED LOOPS PARALLEL_COMBINED_WITH_PARENT :Q1002Cost: 129 Bytes: 1,103 Cardinality: 1
15 HASH JOIN RIGHT ANTI NA PARALLEL_COMBINED_WITH_PARENT :Q1002Cost: 129 Bytes: 1,098 Cardinality: 1
4 PX RECEIVE PARALLEL_COMBINED_WITH_PARENT :Q1002Cost: 63 Bytes: 359,283 Cardinality: 15,621
3 PX SEND BROADCAST PARALLEL_TO_PARALLEL SYS.:TQ10001 :Q1001Cost: 63 Bytes: 359,283 Cardinality: 15,621
2 PX BLOCK ITERATOR PARALLEL_COMBINED_WITH_CHILD :Q1001Cost: 63 Bytes: 359,283 Cardinality: 15,621
1 MAT_VIEW ACCESS FULL MAT_VIEW PARALLEL_COMBINED_WITH_PARENT CST_ASIR.HIERARCHY :Q1001Cost: 63 Bytes: 359,283 Cardinality: 15,621
14 NESTED LOOPS ANTI PARALLEL_COMBINED_WITH_PARENT :Q1002Cost: 65 Bytes: 40,256,600 Cardinality: 37,448
11 HASH JOIN PARALLEL_COMBINED_WITH_PARENT :Q1002Cost: 65 Bytes: 6,366,160 Cardinality: 37,448
8 BUFFER SORT PARALLEL_COMBINED_WITH_CHILD :Q1002
7 PX RECEIVE PARALLEL_COMBINED_WITH_PARENT :Q1002Cost: 1 Bytes: 214 Cardinality: 2
6 PX SEND BROADCAST PARALLEL_FROM_SERIAL SYS.:TQ10000 Cost: 1 Bytes: 214 Cardinality: 2
5 INDEX FULL SCAN INDEX CST_DSA.IDX$$_06EF0005 Cost: 1 Bytes: 214 Cardinality: 2
10 PX BLOCK ITERATOR PARALLEL_COMBINED_WITH_CHILD :Q1002Cost: 63 Bytes: 2,359,224 Cardinality: 37,448
9 MAT_VIEW ACCESS FULL MAT_VIEW PARALLEL_COMBINED_WITH_PARENT CST_ASIR.HIERARCHY :Q1002Cost: 63 Bytes: 2,359,224 Cardinality: 37,448
13 TABLE ACCESS BY INDEX ROWID TABLE PARALLEL_COMBINED_WITH_PARENT CST_DSA.HIERARCHY_MISMATCHES :Q1002Cost: 0 Bytes: 905 Cardinality: 1
12 INDEX RANGE SCAN INDEX PARALLEL_COMBINED_WITH_PARENT SYS.HIERARCHY_MISMATCHES_IDX3 :Q1002Cost: 0 Cardinality: 1
16 INDEX RANGE SCAN INDEX PARALLEL_COMBINED_WITH_PARENT CST_DSA.IDX$$_06EF0001 :Q1002Cost: 1 Bytes: 5 Cardinality: 1 -
Use realational operators in an SQL query??????
does any one knows how to use realational operators in an SQL query??????
i wud like to do something like
select decode(2<3,sysdate,sydate +1) from dual
but i know decode does not supports relational operators......
thanx and Regards
amytYou can use a CASE statement which does support relational operators, or if you must use DECODE, then you can use something like:
SELECT DECODE(SIGN(2 - 3),-1,sysdate,sysdate - 1)
FROM dual;The SIGN function returns -1 if the expression is < 0, 1 if the expression is > 0 and 0 if the expression is 0. This works for numeric comparisions. You can use the GREATEST or LEAST functions in a similar fashion for character comparisions.
TTFN
John -
How to execute a sql query in VO????
Hi every body.
Can you show me the way to execute a sql query in VO.
For example: I have a viewobject1, and I add a new method void execSQL() before the last '}' of the java file of viewobject1 like this:
public void execSQL() {
String strSql = "Select sysdate from dual";
I want to execute strSql query and return a Resultset, how can I perform ???
Thanks a lot.The executeQuery method in ViewObjectImpl does not return a ResultSet.
ViewObjectImpl voImpl;
voImpl.setQuery(strSql);
voImpl.executeQuery(); -
How to Use the Procedures in a Sql Query
Hi Friends,
Can anyone help me out whether can we use the procedure in the sql query..
if yes help me out with an example
my requirement is
i have one sql query .. in which i need to use the procedure which returns multiple values... how can i overcome it,can anyone help me out for this..
for your reference i am pasting the sql query
SELECT paf.person_id
FROM per_all_assignments_f paf START WITH paf.person_id = p_person_id
AND paf.primary_flag = 'Y'
AND paf.assignment_type IN('E', 'C')
AND l_effective_date BETWEEN paf.effective_start_date
AND paf.effective_end_date
CONNECT BY PRIOR paf.supervisor_id = paf.person_id
AND paf.primary_flag = 'Y'
AND paf.assignment_type IN('E', 'C')
AND l_effective_date BETWEEN paf.effective_start_date
AND paf.effective_end_date
and paf.person_id not in (>>>I HAVE TO USE THE PROCEDURE HERE<<<<);
Thanks in advanceWe never saw your procedure, but maybe you could wrap it in a function
SQL> create or replace procedure get_members(in_something IN number, out_members OUT sys_refcursor)
is
begin
open out_members for
'select level member_id from dual connect by level <= :num' using in_something;
end get_members;
Procedure created.
SQL> create or replace type numbers as table of number;
Type created.
SQL> create or replace function members(in_something IN number)
return numbers
as
member_cur sys_refcursor;
members numbers;
begin
get_members(in_something, member_cur);
fetch member_cur bulk collect into members;
close member_cur;
return members;
end;
Function created.
SQL> select * from table(members(4));
COLUMN_VALUE
1
2
3
4
4 rows selected.Variant on same using piplined function
SQL> create or replace function members_piped(in_something IN number)
return numbers pipelined
as
member_cur sys_refcursor;
rec number;
begin
get_members(in_something, member_cur);
loop
fetch member_cur into rec;
exit when member_cur%notfound;
pipe row(rec);
end loop;
close member_cur;
return;
end;
Function created.
SQL> select * from table(members_piped(4));
COLUMN_VALUE
1
2
3
4
4 rows selected.
SQL> drop function members_piped;
Function dropped.
SQL> drop function members;
Function dropped.
SQL> drop type numbers;
Type dropped.
SQL> drop procedure get_members;
Procedure droppedEdit:
Sorry Blu, had not seen you already posted similar thing
Edited by: Peter on Jan 27, 2011 5:38 AM -
How to execute this SQL Query in ABAP Program.
Hi,
I have a string which is the SQL Query.
How to execute this sql Query (SQL_STR) in ABAP Program.
Code:-
DATA: SQL_STR type string.
SQL_STR = 'select * from spfli.'.
Thanks in Advance,
VinayHi Vinay
Here is a sample to dynamically generate a subroutine-pool having your SQL and calling it.
REPORT dynamic_sql_example .
DATA: BEGIN OF gt_itab OCCURS 1 ,
line(80) TYPE c ,
END OF gt_itab .
DATA gt_restab TYPE .... .
DATA gv_name(30) TYPE c .
DATA gv_err(120) TYPE c .
START-OF-SELECTION .
gt_itab-line = 'REPORT generated_sql .' .
APPEND gt_itab .
gt_itab-line = 'FORM exec_sql CHANGING et_table . ' .
APPEND gt_itab .
gt_itab-line = SQL_STR .
APPEND gt_itab .
gt_itab-line = 'ENDFORM.' .
APPEND gt_itab .
GENERATE SUBROUTINE POOL gt_itab NAME gv_name MESSAGE gv_err .
PERFORM exec_sql IN PROGRAM (gv_name) CHANGING gt_restab
IF FOUND .
WRITE:/ gv_err .
LOOP AT gt_result .
WRITE:/ .... .
ENDLOOP .
*--Serdar -
Execute custom SQL query in OIM11g
Hi,
Can you please provide me some java code snippet to execute a custom sql query and also to get db connection to execute this query in OIM 11g. I am going to run this query in an adapter.
Thanks a lot for ur help.
MikeHey Mike,
Use the below sample code to get db connection and then to execute a query in OIM11g.
String query = "SELECT UD_ADUSER_EMAIL FROM UD_ADUSER WHERE upper(UD_ADUSER_UID) like '%TESTU%'";
Connection con = Platform.getOperationalDS().getConnection();
PreparedStatement preparedStatement = con.prepareStatement(query);
ResultSet resultSet = preparedStatement.executeQuery();
while(resultSet.next())
System.out.println("Output:"+resultSet.getString(1));
} -
How to execute a SQL Query in Ms-Access
Hi,
I've a query which fetches the data from the tables based on daily transactions.
I mean, the data gets updated daily basis.
Now, i want to send the user a Ms-access work sheet with the query written, so that when ever the query is executed, it would fetch the records (with latest updates).
I found some documents in google to, how to write sql query in ms-access.
How ever, i'm unable to find the connection setup.
how do i connect to oracle database using ms-access??
Can any one please help me regarding this.
Thanks,
SanthoshYou can try this tutorials
http://www.reo.gov/gis/tools/infobase/LinkAccessToOracle.pdf
Basically steps include,
Install and configure Oracle client
Configure ODBC DSN using correct TNS name configured in first step
Link the table from Msaccess, make sure you linked the table not import it. -
How to execute multiple sql query in one time?
HI
I'm trying to convert my sql project In Oracle. In sql i could run multiple select statement/query in once and they return in multiple table result respectively. but in oracle its not executed.
like:
sqlQry := "Select * from abc; select * from qwe; select * from kkk; select * from xyz"
its return 4 table abc, qwe,kkk,xyz to my dataset result
how it is possible in oracle 10gSaten Chamoli wrote:
I'm trying to convert my sql project In Oracle. In sql i could run multiple select statement/query in once and they return in multiple table result respectively. but in oracle its not executed.
like:
sqlQry := "Select * from abc; select * from qwe; select * from kkk; select * from xyz"
its return 4 table abc, qwe,kkk,xyz to my dataset result That is pretty much a hack - there are no ANSI SQL standards supporting this syntax. It makes no sense either.
If you want to combine 4 data sets, there are the UNION and UNION ALL set commands.
If you want to create 4 cursors with a single call, then use the following (anonymous PL/SQL block) call:
begin
open :c1 for select * from abc;
open :c2 for select * from qwe;
open :c3 for select * from kkk;
open :c4 for select * from xyz;
end;Bind 4 client cursor variables to the ref cursors c1 to c4 and make the call to Oracle.
And I suggest that you read up on Oracle concepts and fundamentals as your approach with you "sql project" shows ignorance in this regard. -
Possible to execute custom SQL query?
Is it possible to execute a custom SQL query and get back a collection of
objects with Kodo JDO? Query expression languages like JDOQL are all very
well (I actually think JDOQL is pretty messy), but sometimes when you've
got a complex query (and you know you're using a RDBMS) it is best
accomplished in a SQL statement. I see Kodo has extended JDOQL, which is
good, but developing custom extensions seems to be limited to implementing
kodo.jdbc.query.JDBCFilterListener where you can customise the where
clause. What I want to be able to do is to specify the entire SQL string.
I am looking into using Kodo JDO for my organisation, and being able to
use custom SQL is something i would expect (and something we can do with
our current O/R mapping product (TopLink)).
Regards,
AlexPatrick Linskey wrote:
On Tue, 30 Sep 2003 10:57:13 +0000, Alex wrote:
being able to use custom SQL is something i would expect
We absolutely agree. Take a look at the samples/customSQL example in the
Kodo distribution.
-Patrick
Patrick Linskey
SolarMetric Inc.Hi Patrick,
I have downloaded kodo-jdo-3.0.0RC1 for windows for evaluation, but there
is no samples/customSQL directory.
Alex. -
JVM hangs while executing a sql query
My JVM hangs while I try to execute an update query. I am using JDBC thin client and Oracle 9i as database. Can someone please suggest what could be wrong.
I have a serious problem regarding the usage of Prepared Statement.
I am working on J2EE based application with the application server as Oracle 9ias.We have mostly used Prepared in 0ur queries.But one our application got implemented we are facing some strange problems.The first instance was a web page was getting hanged and does not give any results.But this had worked fine in all our testing environment.We analysed the table which it uses and still no result.Finally we changed prepared statement to statement and the result page came within seconds.
The second instance is the main issue.Again a query is getting hanged when we try to process it through the web page.We again tested it by converting from prepared statement to statement and its coming within seconds.
Can someone give me some input.Is there any databse parameter that is controlling this prepared statement or statement.
Maybe you are looking for
-
We are in the process of redesigning a php site into j2ee using jsf. The site is rather large and we were thinking about using seperate war files for each "category" of the website for several reasons (the biggest being only deploying that particular
-
I have read all of the related threads. I have fully uninstalled and reinstalled all of itunes program as detailed in trouble shooting. I have confirmed that the program file for the apple mobile device driver is present. When i open itunes manual
-
Hi, I am trying to connect my apple tv for the first time on a samsung tv bought 6 month ago with hdmi cable but it says "no signals". I am on the correct channel and have tried to unplug but no luck. Any idea ? Thanks
-
Anyone could help me? After uploading a CSV file, parsing it with StringTokenizer and displaying it in a table, how can i catch a value of a cell so to compare it to another? Thanks PS: I heard something about comma separated list. What´s the diferen
-
Application Manager Hangs, Can't Open Illustrator
I am so frustrated. My Illustrator subscription was expired. I renewed it. Then tried to open Illustrator. Everytime I try to do this, the Adobe Application Manager pops up, it bounces in the dock for a while and then nothing happens. If I try to ope