aspose file tools*
The moose likes JDBC and the fly likes Dynamic SQL with JDBC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Dynamic SQL with JDBC" Watch "Dynamic SQL with JDBC" New topic
Author

Dynamic SQL with JDBC

Guillaume Compagnon
Ranch Hand

Joined: Aug 09, 2001
Posts: 106
is JDBC allowed Dynamic SQL ?
(I guess no)

if not, do U know if the JDBC JSR works on including Dynamic SQL into JDBC 4 ?

---------<BR>Guillaume
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

yes. check out the java.sql.PreparedStatement interface to execute your SQL dynamically.
http://java.sun.com/docs/books/tutorial/jdbc/basics/prepared.html
I hope that was what you were looking for,
Jamie
Vishakha Ahuja
Ranch Hand

Joined: Sep 13, 2000
Posts: 191
What is dynamic sql ?
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Originally posted by Vishakha Ahuja:
What is dynamic sql ?

defining your sql at runtime, rather than compile time. I guess Statement could be considered dynamic too if you use a variable to define part of the query string at runtime.
static SQL:
"select * from emp"
dynamic SQL:
"select * from emp where id = ?" for a PreparedStatement
"select * from emp where id = '" + stringVariable + "'" for a Statement
Jamie
Poornima Ganesh
Greenhorn

Joined: Aug 16, 2001
Posts: 9
Iam also trying dynamic sql. U have given eg.
"select * from emp where id = '" + stringvar + "' ";
if id is integer and i want to check against a dynamic integer variable how do u write the code?
i mean if id is also integer and,
"select * from emp where id = integervariable ";
i would appreciate if anyone replies quickly .
Thanks.
Poornima.
Guillaume Compagnon
Ranch Hand

Joined: Aug 09, 2001
Posts: 106

that 's just PreparedStatement !
(I ve forgotten!)
We could precise that It's better to use PreparedStatement than Statement because U could reuse PreparedStatement, and the PreparedStatement is compiled once in the DB, and kept as long as the Java program uses the PreparedStatement.
Moreover, U ve got type checking on each variable (?) everytime U use PreparedStatement, that s another reason to use widely PreparedStatement.
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Originally posted by Poornima Ganesh:
if id is integer and i want to check against a dynamic integer variable how do u write the code?
i mean if id is also integer and,
"select * from emp where id = integervariable ";
i would appreciate if anyone replies quickly .
Thanks.
Poornima.

almost:
"select * from emp where id = " + integervariable
the only difference is that single quotes are not needed around integer values
Jamie
Poornima Ganesh
Greenhorn

Joined: Aug 16, 2001
Posts: 9
Thanks for the response. My code is working now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamic SQL with JDBC