Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Jdbc Prepared Statement execute()

 
Ranch Hand
Posts: 40
Firefox Browser Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi I have written the following code. After execution when I give inpt from command line it does not do anything. Please tell me is there anything wrong in code.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
As I understand it, the first thing it would do would be to wait for you to input data. This might appear to be "doing nothing" to somebody who hadn't been told they had to provide input.

Or perhaps "doing nothing" means something else?
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Create an instance of the PreparedStatement after you have taken input for query. You have provided an empty string as the query, which the PreparedStatment is using it. You taking of input from the command line for the query isnt changing the query in the PreparedStatement.
 
Ranch Hand
Posts: 111
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
please check your code.. you have added the null query to prepared statement.. let us see that...



initially your query string is empty .. so that it would do nothing for you.. you should put your query string(after reading from user) to prepared statement..
 
NitishK Kumar
Ranch Hand
Posts: 40
Firefox Browser Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Bharath Raja wrote:please check your code.. you have added the null query to prepared statement.. let us see that...



initially your query string is empty .. so that it would do nothing for you.. you should put your query string(after reading from user) to prepared statement..



Bharath, execute() is for executing any type of sql statements whether it select or non-select. At runtime, if you don't know what type of query is the client going to give, then which behavior will you use. Moreover, if the client gives exit instead of giving any query then what happens. That's why, I have used empty string.
 
NitishK Kumar
Ranch Hand
Posts: 40
Firefox Browser Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

mohamed sanaullah wrote:Create an instance of the PreparedStatement after you have taken input for query. You have provided an empty string as the query, which the PreparedStatment is using it. You taking of input from the command line for the query isnt changing the query in the PreparedStatement.



Sir, what should I do in this case, because my requirement is to give queries at runtime whether it may be select, insert, update, etc.
 
NitishK Kumar
Ranch Hand
Posts: 40
Firefox Browser Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Paul Clapham wrote:As I understand it, the first thing it would do would be to wait for you to input data. This might appear to be "doing nothing" to somebody who hadn't been told they had to provide input.

Or perhaps "doing nothing" means something else?



Yes, you are right, it is waiting for me to give an input. After giving input for e.g.:"select * from emp" and pressing enter it does nothing. That's what I mean by "doing nothing". Only one thing it does is exit.
Please suggest me what to do.
 
Bharath Raja
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator


execute() is for executing any type of sql statements whether it select or non-select. At runtime, if you don't know what type of query is the client going to give, then which behavior will you use. Moreover, if the client gives exit instead of giving any query then what happens. That's why, I have used empty string.



what "mohamed sanaullah" said is exactly right.. so how you expected empty prepared statement give you the result.. you should try the following code...

 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
So the flow would be:
  • Ask the user for the Query Input
  • Take the input
  • Create PreparedStatement instance using the above query

  •  
    NitishK Kumar
    Ranch Hand
    Posts: 40
    Firefox Browser Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator

    Bharath Raja wrote:






    How can you write this type of code, as prepared statement object is instantiated and intialized by connection object and for that prepareStatement() method is defined in the connection interface and this method takes input as SQL statement which is nothing but string in java.

     
    Bharath Raja
    Ranch Hand
    Posts: 111
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    oops .. sorry .. there is correction in my previous post...



    NitishK Kumar wrote:
    How can you write this type of code, as prepared statement object is instantiated and intialized by connection object and for that prepareStatement() method is defined in the connection interface



    we can... see the comments of the code
     
    Sheriff
    Posts: 14691
    16
    Eclipse IDE VI Editor Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator

    Bharath Raja wrote:oops .. sorry .. there is correction in my previous post...


    And what happens when you enter "exit" ?
     
    Bharath Raja
    Ranch Hand
    Posts: 111
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator

    Christophe Verré wrote:
    And what happens when you enter "exit" ?



    his logic is actually wrong... he have to write the while loop after reading input from user right.
     
    NitishK Kumar
    Ranch Hand
    Posts: 40
    Firefox Browser Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator

    mohamed sanaullah wrote:So the flow would be:

  • Ask the user for the Query Input
  • Take the input
  • Create PreparedStatement instance using the above query



  • thank you sir, one more problem I have when I am giving when I am giving non-parameterized SQL statement, it works fine but for parameterized SQL statements, its not working.
     
    Christophe Verré
    Sheriff
    Posts: 14691
    16
    Eclipse IDE VI Editor Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    You need to create a new statement to update records. I'd advice you to go through a JDBC tutorial first. Here is one.
     
    NitishK Kumar
    Ranch Hand
    Posts: 40
    Firefox Browser Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator

    mohamed sanaullah wrote:So the flow would be:

  • Ask the user for the Query Input
  • Take the input
  • Create PreparedStatement instance using the above query



  • thank you sir, one more problem I have when I am giving when I am giving non-parameterized SQL statement, it works fine but for parameterized SQL statements, its not working.
     
    Mohamed Sanaulla
    Bartender
    Posts: 3225
    34
    IntelliJ IDE Oracle Spring Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator

    NitishK Kumar wrote:thank you sir, one more problem I have when I am giving when I am giving non-parameterized SQL statement, it works fine but for parameterized SQL statements, its not working.



    As said by Christophe, please go through the JDBC tutorial. And it should work for parametrized/non-parametrized SQL.
     
    NitishK Kumar
    Ranch Hand
    Posts: 40
    Firefox Browser Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator

    Bharath Raja wrote:

    his logic is actually wrong... he have to write the while loop after reading input from user right.



    You mean to say that I should first take query from the user and then should go for checking whether it is equal to exit or not. Am I right?
     
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator

    NitishK Kumar wrote:

    Bharath Raja wrote:

    his logic is actually wrong... he have to write the while loop after reading input from user right.



    You mean to say that I should first take query from the user and then should go for checking whether it is equal to exit or not. Am I right?




    Yes Nitish,that's the way it will work.
     
    NitishK Kumar
    Ranch Hand
    Posts: 40
    Firefox Browser Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator

    Prince Sewani wrote:

    Yes Nitish,that's the way it will work.



    But, when I did so, the resultset loop started executing infinite loop.
     
    Christophe Verré
    Sheriff
    Posts: 14691
    16
    Eclipse IDE VI Editor Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    You have to use another ResultSet.
     
    NitishK Kumar
    Ranch Hand
    Posts: 40
    Firefox Browser Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator

    Christophe Verré wrote:You have to use another ResultSet.



    Sir, can you please explain me?
     
    Prince Sewani
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator

    NitishK Kumar wrote:

    Prince Sewani wrote:

    Yes Nitish,that's the way it will work.



    But, when I did so, the resultset loop started executing infinite loop.



    Alright i don't know what they're mentioning about two result-sets but to me it comes like this:

    Yeah as mohamed sanaullah wrote:
    "So the flow would be:
    Ask the user for the Query Input
    Take the input
    Create PreparedStatement instance using the above query"

    So according to me it should be more like : -



    Now as per the parameterized part,that's something which asks for the analysis of what exactly are you gonna provide to the user,
    1. is it gonna be and live query browser? -then you don't have to worry about parameters
    2. if you're gonna provide a GUI that's gonna work both the ways that is like a query browser and as well as an interactive version which asks for few values
    and based on them it does the rest of the operations then it will be better to define stored procedures in database and use them from your code based on some event like a radio button and all and then using :-


    For that you gotta go to the JDBC tutorial..



    regards

    Prince Sewani



     
    Don't get me started about those stupid light bulbs.
      Bookmark Topic Watch Topic
    • New Topic