• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamic SQL with JDBC

 
Guillaume Compagnon
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is dynamic sql ?
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response. My code is working now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic