Results 1 to 7 of 7
  1. #1
    vinoth is offline Member
    Join Date
    Aug 2009
    Posts
    21
    Rep Power
    0

    Default Need Explanation for a Simple Query

    Hi everybody.
    I was developing a JSP page with a "Registration Form" in it.
    Therefore I am supposed to store all the information that user provides into the Database.
    I was trying to use "Prepared Statement", but my faculty told to use the following simple query. I used it and it worked. But I didn't understand it's meaning.
    Pls explain the following query.
    Here varibles are String data types.

    insert into <table_name> values (' "+variable1+" ',' "+variable2+" ');

    Thanks in advance.

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

    Default

    It means exactly what it says. But I find it hard to believe that "faculty" told you to do something as error-prone, and possibly destructive, as that.

    Essentially it is
    Java Code:
    insert into <table> values ('<var1>', '<var2'>)

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You need to read a database tutorial that explains SQL queries.
    The insert should still have been done using a PreparedStatement.

  4. #4
    vinoth is offline Member
    Join Date
    Aug 2009
    Posts
    21
    Rep Power
    0

    Default

    My doubt is why '+' and Double quotes symbols are used?

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    When using sql, you use ' ' wrapped aroung text for character literals
    e.g

    Java Code:
    insert into users(id,name) values (1, 'vinoth');
    The characters need to have the quotes while the integer does not need them.

    When writing the query in java you need to add those quotes too. Read about this in a SQL tutorial.



    The + operator just adds the strings together the same way as it does in
    Java Code:
    String greetVinoth = "Hello " + "vinoth";
    Or
    Java Code:
    String variable= "vinoth";
    String greeting = "Hello " + variable;
    Using the PreparedStatement takes away the need to add the ' ' around characters and this is one of the reasons why it should be prefered.
    Last edited by r035198x; 02-15-2010 at 02:15 PM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    Quote Originally Posted by masijade View Post
    It means exactly what it says. But I find it hard to believe that "faculty" told you to do something as error-prone, and possibly destructive, as that.
    I don't...

  7. #7
    vinoth is offline Member
    Join Date
    Aug 2009
    Posts
    21
    Rep Power
    0

Similar Threads

  1. explanation of this loop?
    By glopez09 in forum New To Java
    Replies: 4
    Last Post: 11-15-2009, 02:36 AM
  2. the explanation of output of simple java program
    By amol84 in forum New To Java
    Replies: 1
    Last Post: 11-06-2008, 05:06 PM
  3. Class explanation
    By mcal in forum New To Java
    Replies: 1
    Last Post: 02-05-2008, 06:50 PM
  4. need a little explanation
    By cew27 in forum New To Java
    Replies: 7
    Last Post: 12-13-2007, 11:39 PM
  5. I need didactic explanation
    By Eric in forum New To Java
    Replies: 2
    Last Post: 07-02-2007, 05:37 AM

Tags for this Thread

Posting Permissions

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