Results 1 to 5 of 5
  1. #1
    Yuvarajsinh is offline Member
    Join Date
    Apr 2010
    Posts
    13
    Rep Power
    0

    Question problem with PreparedStatement

    y we cant set table name as place holder in the PreparedStatement object?

    i.e,

    PreparedStatemen ps=con.prepareStatement("select ? from ?");
    ps.setString(1,"tname");
    ps.setString(2,"tab");
    ResultSet rs = ps.executeQuery();

    Above code compiles fine, but at runtime it gives invalid table name exception..

    Any body can realize me what acually happens here?

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Because PreparedStatements (from the DB side as well as from the Java side) don't work that way. The "placeholders" can be used for values, and values only.

  3. #3
    Yuvarajsinh is offline Member
    Join Date
    Apr 2010
    Posts
    13
    Rep Power
    0

    Default

    is it not true that our query is cashed in compiled form at DB server?
    if true then y cannt we do the above?
    thnx in adv...

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default SMS speak has no place in a programming forum

    Quote Originally Posted by Yuvarajsinh View Post
    is it not true that our query is cashed in compiled form at DB server?
    if true then y cannt we do the above?
    thnx in adv...
    To stop appearing semi-literate, read and follow the tips contained here:
    Write well

    db

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by Yuvarajsinh View Post
    is it not true that our query is cashed in compiled form at DB server?
    if true then y cannt we do the above?
    thnx in adv...
    Well think about it. What does "caching the statement" (although that is not quite what PreparedStatements do) accomplish for the DB server? It allows it to predetermine the "workpath" (this is what PreparedStatements do). How is it going to do that when it doesn't even know which table, or columns, are going to be used?

Similar Threads

  1. Problem with PreparedStatement?
    By ZeCute in forum New To Java
    Replies: 6
    Last Post: 06-13-2010, 12:50 PM
  2. Regarding PreparedStatement
    By adeeb in forum JDBC
    Replies: 0
    Last Post: 06-09-2008, 09:07 PM
  3. Using PreparedStatement
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-22-2007, 11:24 AM
  4. Help me. PreparedStatement
    By Felissa in forum JDBC
    Replies: 2
    Last Post: 06-28-2007, 05:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •