How to use prepared statements with a single id
Here is the code I am having trouble with:
String sqlb = "INSERT INTO books.book(title)" +
"VALUES(?)";
String sqla = "INSERT INTO books.author(name,date_of_birth)" +
"VALUES(?,?)";
PreparedStatement psb = c.prepareStatement(sqlb);
PreparedStatement psa = c.prepareStatement(sqla);
psb.setString(1,book.getTitle());
psa.setString(1,author.getName());
psa.setString(2,author.getDate());
m = psb.executeUpdate();
n = psa.executeUpdate();
After placing two book/Author objects into the database I get a table that looks something like this:
0 AGreatBook Joe Bloggs 08-10-83
1 BGreatBook Joe Bloggs 08-10-83
2 AGreatBook John Smith 12-12-65
3 BGreatBook JohnSmith 12-12-65
Clearly this is not correct I want this:
0 AGreatBook Joe Bloggs 08-10-83
1 BGreatBook John Smith 12-12-65
By the way I have placed a foreign key in book table
Anyone know how to do this? (I am using postgres)
regards
http://forum.java.sun.com/thread.jspa?threadID=726820&tstart=0
http://forum.java.sun.com/thread.jspa?threadID=726823&tstart=0
Similar Messages
-
How to use union statement with declare & set function?
Hi Experts,
i have small query about how to use union statement with declare & set function?
Example as below :
DECLARE @name AS date
Declare @name2 AS date
/* SELECT FROM [2013].[dbo].[OINV] T0 */
/* WHERE */
SET @name = /* T0.DocDate */ '[%1]'
SET @name2 = /* T0.DocDate */ '[%2]'
select '2013',t5.U_salmannm,t1.CardName,t2.sumapplied as CollectionAmount,t2.DcntSum ,t3.DocTotal as InvoiceTotal,
datediff(dd,t3.DocDate,t1.Docdate) as Days
from 2013.dbo.orct t1
inner join 2013.dbo.RCT2 t2 on t1.DocNum = t2.DocNum
left join 2013.dbo.oinv t3 on
t3.docentry = t2.baseAbs
inner join 2013.dbo.ocrd t4 on t1.Cardcode = t4.CardCode
inner join [2013].[dbo].[@CQ_RTSM] t5 on t4.U_BeatCode = t5.U_RoutCode
where t2.DcntSum <> 0.000000 and t3.DocDate between [%1] and [%2]
Union
/* SELECT FROM [2014].[dbo].[OINV] T0 */
/* WHERE */
SET @name = /* T0.DocDate */ '[%1]'
SET @name2 = /* T0.DocDate */ '[%2]'
select '2014',t5.U_salmannm,t1.CardName,t2.sumapplied as CollectionAmount,t2.DcntSum ,t3.DocTotal as InvoiceTotal,
datediff(dd,t3.DocDate,t1.Docdate) as Days
from 2014.dbo.orct t1
inner join 2014.dbo.RCT2 t2 on t1.DocNum = t2.DocNum
left join 2014.dbo.oinv t3 on
t3.docentry = t2.baseAbs
inner join 2014.dbo.ocrd t4 on t1.Cardcode = t4.CardCode
inner join [2014].[dbo].[@CQ_RTSM] t5 on t4.U_BeatCode = t5.U_RoutCode
where t2.DcntSum <> 0.000000 and t3.DocDate between [%1] and [%2]You have to create stored procedure in SQL only .
Like u must have create for Crystal .
You can execute procedure in query manager but you have to enter parameter manually..
example
Exec @Test '20140101' '20140501'
Every time user has to enter it manually in yyyymmdd format in case of date parameters.
Example
Create Proc [@Test]
as begin
DECLARE @name AS date
Declare @name2 AS date
/* SELECT FROM [2013].[dbo].[OINV] T0 */
/* WHERE */
select '2013',t5.U_salmannm,t1.CardName,t2.sumapplied as CollectionAmount,t2.DcntSum ,t3.DocTotal as InvoiceTotal,
datediff(dd,t3.DocDate,t1.Docdate) as Days
from 2013.dbo.orct t1
inner join 2013.dbo.RCT2 t2 on t1.DocNum = t2.DocNum
left join 2013.dbo.oinv t3 on
t3.docentry = t2.baseAbs
inner join 2013.dbo.ocrd t4 on t1.Cardcode = t4.CardCode
inner join [2013].[dbo].[@CQ_RTSM] t5 on t4.U_BeatCode = t5.U_RoutCode
where t2.DcntSum <> 0.000000 and t3.DocDate between @Name and @Name2
Union
/* SELECT FROM [2014].[dbo].[OINV] T0 */
/* WHERE */
select '2014',t5.U_salmannm,t1.CardName,t2.sumapplied as CollectionAmount,t2.DcntSum ,t3.DocTotal as InvoiceTotal,
datediff(dd,t3.DocDate,t1.Docdate) as Days
from 2014.dbo.orct t1
inner join 2014.dbo.RCT2 t2 on t1.DocNum = t2.DocNum
left join 2014.dbo.oinv t3 on
t3.docentry = t2.baseAbs
inner join 2014.dbo.ocrd t4 on t1.Cardcode = t4.CardCode
inner join [2014].[dbo].[@CQ_RTSM] t5 on t4.U_BeatCode = t5.U_RoutCode
where t2.DcntSum <> 0.000000 and t3.DocDate between
between @Name and @Name2
end -
How to use Mulitple DAQs with a single triggered event on only one of the DAQs
I have three PXI6115 and would like to tie all these to a single triggered event on one of the DAQs. I've scaned the examples and any assistance would be greatly appreciated....
TCjr,
Please refer to your other post:
How to use Multiple DAQs with a single triggered event on only one of the DAQs -
How to use " PERFORM ", STATEMENT WITH OFFSET
hI,
How we use Perform statement with offset, like
Example: " PERFORM READ_SEGRELEASE USING INT_EDIDC+11."
My requirement is to use Perform with Automatic length declaration.
Automatic length declaration in PERFORM statement
Kindly Explain and Provide some Example programm written in this situation.
Thanks,
P.N.KumarHiere is an example of what you probably require:
DATA:
lv_offset TYPE syoffi,
lv_length TYPE flength,
lv_field TYPE string.
lv_field = 'This is my value: hello world'.
lv_length = 5.
lv_offset = 18.
PERFORM read_segrelease USING lv_field
lv_length
lv_offset.
*& Form READ_SEGRELEASE
* text
* -->P_LV_FIELD text
* -->P_LV_LENGTH text
* -->P_LV_OFFSET text
FORM read_segrelease USING p_lv_field TYPE string
p_lv_length TYPE flength
p_lv_offset TYPE syoffi.
DATA:
lv_result TYPE string.
lv_result = p_lv_field+p_lv_offset(p_lv_length).
WRITE: / lv_result.
ENDFORM. " READ_SEGRELEASE -
How to use prepared statement - For Everyone Help
public String count()
int count = 0;
String fetchsize = "";
try
Connection conn = getOADBTransaction().getJdbcConnection();
Statement statement = conn.createStatement();
String Query = "select count(*) count from fnd_lookups where lookup_type like 'SAPE_BILL_TO_SHIP_TO_UPDATE'";
ResultSet resultset = statement.executeQuery(Query);
while (resultset.next())
count = (int)resultset.getInt("count");
fetchsize = ""+count;
catch(Exception e)
return fetchsize;
}Hi,
we can use prepare statement in Co....
import java.sql.PreparedStatement;
import java.sql.ResultSet;
try
PreparedStatement prpdStmt1 = am.getOADBTransaction().getJdbcConnection().prepareStatement("select full_name from per_all_people_f where person_id='"+empid+"' ");
ResultSet rs=prpdStmt1.executeQuery(); //Cursor fetch row
while(rs.next())
empname=rs.getString(1);
catch(Exception e)
System.out.println("Exception is"+e.getMessage());
t1.setValue(pageContext,empname);
Thanks
nani -
How to use parameterized statements with labSQL?
Hi everybody,
I have a big problem with labSQL. Every statement or query I have to use in my program works correctly, but I have a performance problem when writing my data to the database. The problem is the handling of the SQL statements by the DB. The solution will be the use of parameterized statements with labSQL.
The manual for NI Labview Database Connectivity Toolkit provides such information, but there are large differences to labSQL, and I don't get the clue. Is anybody able and would be so kind to help me? If someone has an example for using parameterized SQL statements with labSQL or the ADO methods for such stuff, please be so kind to share it with me.
Thank you for reading all of this
Best regards,
MatthiasThank you very much for your help. I already had set up the correct SQL statement and created the parameters but got errors when executing the command object. After some tries I found that neither the SQL statement nor the parameters were the source of the error, as I had expected first.
The problem was that I was sending the wrong data type to the execute method of the command object. It expected some variant input for the parameters. I send an array of strings, which caused an error. I casted that array to variant with the same result. But a single string to variant worked well (with only one parameter in the SQL statement). The easy solution was to cast all strings to variants, and then build an array out of them. The execute method worked with that. Now the transaction is 4 times faster than before
Thanks again!
Edit:
If someone is interested in some example VI, please let me know. I should have some time next week to build one.
Message Edited by manicnobody on 02-26-2009 10:19 AM -
How to use prepared statement by Application Module on ADF?
Hello
i am using a prepared statement on the view side of my project,
this is my code, i'm not sure about using this code.
AppModuleGFTImpl am = (AppModuleGFTImpl) Configuration.createRootApplicationModule(amDef,config);
try {
ps =am.getDBTransaction().createPreparedStatement("Select * from XXXXXX where kullanici_id=? and sifre=?",0);
ps.setString(1, kullanici);
ps.setString(2, sifre);
rs = ps.executeQuery();
if (rs.next()) {
girebilir = true;
} else {
girebilir = false;
ps.close();
rs.close();
//am.getDBTransaction().closeTransaction(); ? I'm not Sure
//Configuration.releaseRootApplicationModule(am, true); ? I'm not Sure
Is it True? How we use this code on the project view side? Must we close Transaction, or release application Module.
thanks for interest.
sorry my english.Hello, for the SP you can use something like this in your application module
private static final String BULKSTAMMENN =
"begin IVA_OWNER.IVA_UI_ALGEMEEN_PCK.USM_SNELLE_INVOER_GUI( P_RLE_ID => :1, P_AANTAL => :2, P_OJR_JAAR => :3, P_RAS_ID => :4, P_TOELICHTING => :5, P_SUCCES => :6 ); end;";
public void bulkStamen ( int rasId, int telerId, int jaar, int aantal, String toelichting ) {
CallableStatement bulkStamenSP = null;
try {
bulkStamenSP = getDBTransaction().createCallableStatement( BULKSTAMMENN, 0 );
bulkStamenSP.setInt( 1, telerId );
bulkStamenSP.setInt( 2, aantal );
bulkStamenSP.setInt( 3, jaar );
bulkStamenSP.setInt( 4, rasId );
bulkStamenSP.setString( 5, toelichting );
bulkStamenSP.registerOutParameter( 6, Types.VARCHAR );
bulkStamenSP.executeUpdate();
} catch ( Exception e ) {
log.warning( "Aanroep naar " + BULKSTAMMENN + " gefaald, " + e.getMessage() );
} finally {
if ( bulkStamenSP != null ) {
try {
bulkStamenSP.close();
} catch ( Exception e ) {
log.warning( e.getMessage() );
log.info( "Stammen bulk uitgevoerd voor " + rasId + " " + telerId + " " + jaar + " " + aantal + " " +
toelichting );
}You can then drag this method onto your page or right into your task flow if your using JDev 11.
Also you can access it from a backing bean although the methods above are preferred
-Anton -
How to Use Switch Statement with Exclusion Group (radio buttons)?
Wouldn't you know, just when I though I'd really be making progress, I've come across another problem I can't solve. In a homeowners insurance application I am building, there is an exclusion group that needs to set the value of several variables
I have setup in the form properties/variables. These variables take on different values depending on the users choice. For the exclusion group, in the object pallet, I have set the binding to normal, and have checked the "Specify Item Values" check box. Also the values for the choices have been assigned 1,2,3,4,5.
Here is my code for the change event fir the exclusion group (This is exactly what I have tried). For now, the values for the variables to take on in the different cases, are completely arbitrary.
switch (this.change.rawValue) // I have tried so many things here
case "1": // I have tried the caption, single quotes in all combinations
addLivingExp = "1";
damageOthersProperty = "2";
liabilityIncl = "3";
maxCoverage = "4";
minCoverage = "5";
persProperty = "6";
relatedPrivateStruct = "7";
break;
case "2":
addLivingExp = "10";
damageOthersProperty = "20";
liabilityIncl = "30";
maxCoverage = "40";
minCoverage = "50";
persProperty = "60"
relatedPrivateStruct = "70";
break;
case "3":
addLivingExp = "100";
damageOthersProperty = "200";
liabilityIncl = "300";
maxCoverage = "400";
minCoverage = "500";
persProperty = "600"
relatedPrivateStruct = "700";
break;
case "4":
addLivingExp = "1000";
damageOthersProperty = "2000";
liabilityIncl = "3000";
maxCoverage = "4000";
minCoverage = "5000";
persProperty = "6000"
relatedPrivateStruct = "7000";
break;
case "5":
addLivingExp = "10000";
damageOthersProperty = "20000";
liabilityIncl = "30000";
maxCoverage = "40000";
minCoverage = "50000";
persProperty = "60000"
relatedPrivateStruct = "70000";
break;
default:
minCoverage= 5;
break;
There must be something obvious I am missing? Eternally grateful for advice on this.
StephenThere are two issues in this script:
1. You are not using the accessor 'value' to set form variables
2. You are not correctly getting the value of the radio button list in the switch clause
Please see the working script below.
Ben Walsh
www.avoka.com
switch (this.rawValue)
case "1":
addLivingExp.value = "1";
damageOthersProperty.value = "2";
liabilityIncl.value = "3";
maxCoverage.value = "4";
minCoverage.value = "5";
persProperty.value = "6";
relatedPrivateStruct.value = "7";
break;
case "2":
addLivingExp.value = "10";
damageOthersProperty.value = "20";
liabilityIncl.value = "30";
maxCoverage.value = "40";
minCoverage.value = "50";
persProperty.value = "60"
relatedPrivateStruct.value = "70";
break;
case "3":
addLivingExp.value = "100";
damageOthersProperty.value = "200";
liabilityIncl.value = "300";
maxCoverage.value = "400";
minCoverage.value = "500";
persProperty.value = "600"
relatedPrivateStruct.value = "700";
break;
case "4":
addLivingExp.value = "1000";
damageOthersProperty.value = "2000";
liabilityIncl.value = "3000";
maxCoverage.value = "4000";
minCoverage.value = "5000";
persProperty.value = "6000"
relatedPrivateStruct.value = "7000";
break;
case "5":
addLivingExp.value = "10000";
damageOthersProperty.value = "20000";
liabilityIncl.value = "30000";
maxCoverage.value = "40000";
minCoverage.value = "50000";
persProperty.value = "60000"
relatedPrivateStruct.value = "70000";
break;
default:
minCoverage.value = 5;
break; -
Prepared Statement with ORDER BY
I am trying to use order by with prepared statement but it is not ordering.
String sql = "SELECT * FROM MATERIAL WHERE (LOWER(NAMEE) LIKE ('%' || ? || '%') ORDER BY ? ";
PreparedStatement ps=CM.getStatement(sql);
ps.setString(1,p);
ps.setString(2,sort);
ResultSet r = ps.executeQuery();
Can any one tell me how do I use prepared statement with order byYou can not parameterize column names and such, only literals. You should build the ORDER BY clause dynamically.
-
How to Insert Character using Prepared statement
Hi All,
Can anyone let me know how can I insert character using prepared statement.
Thanks
SameerIn the future JDBC related questions should be posted into the JDBC forum.
Can you please provide some more information about what you are trying to do? It isn't clear to me. Are you trying to update a CHAR field? -
How to avoid data repetation when using select statements with innerjoin
how to avoid data repetation when using select statements with innerjoin.
thanks in advance,
satheeshyou can use a query like this...
SELECT DISTINCT
frg~prc_group1 "Product Group 1
frg~prc_group2 "Product Group 2
frg~prc_group3 "Product Group 3
frg~prc_group4 "Product Group 4
frg~prc_group5 "Product Group 5
prc~product_id "Product ID
txt~short_text "Product Description
UP TO 10 ROWS
INTO TABLE l_i_data
FROM
Joining CRMM_PR_SALESG and
COMM_PR_FRG_ROD
crmm_pr_salesg AS frg
INNER JOIN comm_pr_frg_rod AS prd
ON frgfrg_guid = prdfragment_guid
Joining COMM_PRODUCT and
COMM_PR_FRG_ROD
INNER JOIN comm_product AS prc
ON prdproduct_guid = prcproduct_guid
Joining COMM_PRSHTEXT and
COMM_PR_FRG_ROD
INNER JOIN comm_prshtext AS txt
ON prdproduct_guid = txtproduct_guid
WHERE frg~prc_group1 IN r_zprc_group1
AND frg~prc_group2 IN r_zprc_group2
AND frg~prc_group3 IN r_zprc_group3
AND frg~prc_group4 IN r_zprc_group4
AND frg~prc_group5 IN r_zprc_group5.
reward it it helps
Edited by: Apan Kumar Motilal on Jun 24, 2008 1:57 PM -
Performance issue doing a prepare statement with LIKE
Hi
I'm doing a preparestatement like this
select foo
from bar
where des like ?
and then do the prepare with the string 'foo%'
If I do the prepare of this variable, the query is very slow. If I don't do the prepare, the query is very fast. Can anyone help me out on this? DBA's will kill me if I don't use prepare statements but users will kill be if the query is slow...
Thank you!
HappyGuyAs indicated in the last reply this probably has to do with use of string literals over bound variables
With a prepared statement the optimiser dosen't get the chance to use histograms against your data... so whilst it might be more efficent to drive off an index for some of the searches it looses access to the data (histograms) that enable it to make this decission...
Hints are probably the way to go... if your sure that driving off a index is always going to be the most efficent...
Using explain (inside of JDeveloper9i or turning on the autotrace functionality inside of sqlplus) will give you some idea of how Oracle would drive the query if you use a string literal
Dom -
Resultset from a batch select using prepared statement
Hi,
Is it possible to add results of batch execute of select statements and retrieve the result set ..
I am running select statement in a batch mode setting in value for a column.I add this to a prepared batch.
I would like to get the reults as a batch and spin through the result set like a r.next(). Is this possible ?
I have given the code below
sql = "select ordervalue , orderdate from discreet_orders "+
"where order_no = ? ";
dbObject.prepareStatement(sql);
dbObject.prepareStatement(sql);
for (int i=0; i < orderNoVect.size(); i++) {
dbObject.setString(1,(String)scnV.elementAt(i));
dbObject.addBatchPrepared();
r = dbObject.execPrepared();
dbObject.clearBatchPrepared();
I would like to do a r.getNext() process the values form the resultset.
Is this possible ? If not is there an other way to do it ?
Thanks
ArnoldOlek wrote:
hi
Is there a way do a batch update using prepared statement?
I don't want to insert every row in a single query...
regards,
OlekPossibly the confusion here stems from the existence of two addBatch methods in PreparedStatement.
addBatch(String sql)inherited from Statement adds a new query to the batch of SQL statements for the statement. However you don't want to use this with PreparedStatements Instead you want
addBatch()which adds the current set of parameters to the prepared statements batch of commands.
In this scenario you prepare your query (insert statement) and then call setXXX(x,y) for each parameter and then addBatch for each tuple. Then call executeBatch to execute all the queries based on your batched parameters.
If this didn't answer your question I don't know what your question is exactly. I would add that you probably want to set a "reasonable" size on your batches and break them into smaller batches as is reasonable. Mainly because the implementation of batched PreparedStatements widely varies depending on the database and driver, thus there are certainly scenarios where given enough batched parameters you could have various unpleasant things visited upon your program. Like terminal slowness, running out of memory, etc. -
How to use perform statements in sap scripts
how to use perform statements in sap scripts . and pls send me one progam for this
thnaks
rajaHi Raja,
<b>PERFORM</b> key work is used to include subroutine in sapscript form...
But the processing is lttle bit different form the one we use in ABAP.
Here the paramters passed to form is stored in internal table of name-value table. there are two table one for inbound parameter and other for outbound parameters.
Check out the example below to see how this is used..
<b>Definition in the SAPscript form:</b>
/: PERFORM GET_BARCODE IN PROGRAM QCJPERFO
/: USING &PAGE&
/: USING &NEXTPAGE&
/: CHANGING &BARCODE&
/: ENDPERFORM
/ &BARCODE&
<b>Coding of the calling ABAP program:</b>
REPORT QCJPERFO.
FORM GET_BARCODE TABLES IN_PAR STUCTURE ITCSY
OUT_PAR STRUCTURE ITCSY.
DATA: PAGNUM LIKE SY-TABIX, "page number
NEXTPAGE LIKE SY-TABIX. "number of next page
READ TABLE IN_PAR WITH KEY PAGE.
CHECK SY-SUBRC = 0.
PAGNUM = IN_PAR-VALUE.
READ TABLE IN_PAR WITH KEY NEXTPAGE.
CHECK SY-SUBRC = 0.
NEXTPAGE = IN_PAR-VALUE.
READ TABLE OUT_PAR WITH KEY BARCODE.
CHECK SY-SUBRC = 0.
IF PAGNUM = 1.
OUT_PAR-VALUE = |. "First page
ELSE.
OUT_PAR-VALUE = ||. "Next page
ENDIF.
IF NEXTPAGE = 0.
OUT_PAR-VALUE+2 = L. "Flag: last page
ENDIF.
MODIFY OUT_PAR INDEX SY-TABIX.
ENDFORM.
Hope this is clear to understand...
Enjoy SAP.
Pankaj Singh. -
Could not find prepared statement with handle %.
Greetings. I've seen several posts for this error on the web, but no clear cut answers. I captured the code below in profiler, with the intention of replaying in mgmt studio.
However, the attempt end in the following error: "Could not find prepared statement with handle 612."
declare @p1 int
set @p1=612
declare @p2 int
set @p2=0
declare @p7 int
set @p7=0
exec sp_cursorprepexec @p1 output,@p2 output,N'@P0 int,@P1 int,@P2 int,@P3 int,@P4 bit',N'EXEC dbo.mySproc @P0,@P1,@P2,@P3,@P4 ',4112,8193,@p7 output,219717,95,NULL,1,0
select @p1, @p2, @p7
Something noteworthy is that my sproc only has 5 input parameters, but this makes it look like it has many more.
How do I manipulate the code enough to make it work in mgmt studio? Thanks!
TIA, ChrisRDBAIn profiler you would normally see RPC:Starting and RPC:Completed. The statement shown in RPC staring is what you need to pick because as Erland explained, completed would show "funky" behavior.
Balmukund Lakhani | Please mark solved if I've answered your question, vote for it as helpful to help other users find a solution quicker
This posting is provided "AS IS" with no warranties, and confers no rights.
My Blog |
Team Blog | @Twitter
Author: SQL Server 2012 AlwaysOn -
Paperback, Kindle
Maybe you are looking for
-
ITunes and iTouch working against me???
Hello, My iTunes account is deleting my purchases. I have bought the music on my iPod Touch which then downloaded onto my PC. Nothing bad happened, but once I had access to my MacBook Pro, I plugged my iTouch into the computer and the computer d
-
Exporting from aperture to maxtor??
My mac is at max capacity and won't take any more photos on board I have tried exporting to a maxtor but it won't let me do this ? Is it possible to export to a maxtor Or is there another way I can export them to somewhere else (easily) to free up so
-
I can't find any of my albums, or see a way to create a new album, in PSE12 organizer. -- Larry
-
Buying from a different app store
I have an ipod touch 4 and would like to purchase a game from the U.S. app store but i live in Australia and my id is set up for AUS. How can i buy from the US store? Thanks Heaps
-
<SPAN>PC: Windows XP <SPAN>Mp3 Player: Creative <SPAN>MuVo<SPAN>TX<SPAN> FM 256MB <SPAN>Problem: The USB-Root-HUB dri've doesn?t seem to work. <SPAN>Hi, <SPAN>Please help me! I got this new mp3 player last Thursday, after thinking about it for a week