Results 1 to 7 of 7
  1. #1
    will71110 is offline Member
    Join Date
    Oct 2013
    Posts
    14
    Rep Power
    0

    Default Returning String from objects

    Hello fellow Java programers , I am trying to figure something out. I want to create a set of objects that when I call objects like this

    select.all.from("name").where("name = Joe")

    it returns a string like this:

    "SELECT * FROM name WHERE name = 'Joe'"

    I know I need to create a set of classes but how do have each return parts of the string to make it whole or is that even possible? I need to have it look like this:

    String sql = select.all.from("name").where("name = Joe");

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,781
    Rep Power
    5

    Default Re: Returning String from objects

    The problem is that the object "select" must return a string. This means that the String class must know about the method "all", which it doesn't. However, you can do it by creating your own Command class which contains methods of all, where, from, etc. Each method returns the current instance of the class and also builds up the string in that class. The final returned instance of the Command object has the command string as a field.

    However, I am not certain I see the advantage in doing this.

    Regards,
    Jim
    Last edited by jim829; 10-10-2013 at 09:04 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    will71110 is offline Member
    Join Date
    Oct 2013
    Posts
    14
    Rep Power
    0

    Default Re: Returning String from objects

    The main reason I wanted to do this is to simplify the process. I wanted to make it where I don't have to keep typing out the strings for SQL (I know there is other ways of doing this too. I just wanted to try it as objects.). I did get it to work for all the way to from as I made from store and return a string. However I can't give it the option not to use the .where(). I have an idea to make where give nothing if it is not used. That may give me the results I'm looking for, but I haven't tried it yet.

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

    Default Re: Returning String from objects

    This is similar to Hibernate's Criteria, where the Criteria are chained together.
    Except you're trying to do it for an entire SQL statement.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Returning String from objects

    Quote Originally Posted by Tolls View Post
    This is similar to Hibernate's Criteria, where the Criteria are chained together.
    Except you're trying to do it for an entire SQL statement.
    Not only hibernates, but also EclipseLink, etc, essentially EVERY JPA implementation will have something similar.
    @OP, how are you REALLY saving any time and/or typing doing this? Your chained together syntax is longer than the actual SQL string in the provided example.

  6. #6
    will71110 is offline Member
    Join Date
    Oct 2013
    Posts
    14
    Rep Power
    0

    Default Re: Returning String from objects

    Ruby on rails does something like this. I wanted to see if I can mimic that behavior in Java. I see this as an easy way to throw variables in to the SQL statements instead of just append them to the string. From what I' gathering, this isn't something easy and I just rather stick to spelling it out as a single string.

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

    Default Re: Returning String from objects

    In general you shouldn't be appending your variables in anyway, at least for the WHERE clause.
    You should be binding them via a PreparedStatement.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. returning objects explanation please
    By raffs03 in forum New To Java
    Replies: 2
    Last Post: 06-25-2013, 09:30 PM
  2. Returning local objects
    By Lowest0ne in forum New To Java
    Replies: 1
    Last Post: 12-11-2012, 01:14 AM
  3. Function call returning array of objects
    By ShitalJain in forum New To Java
    Replies: 4
    Last Post: 06-25-2011, 09:39 AM
  4. Replies: 1
    Last Post: 02-07-2011, 08:22 PM
  5. Polygons... returning and passing objects?
    By asherwolf in forum New To Java
    Replies: 3
    Last Post: 07-09-2010, 04:41 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
  •