Results 1 to 5 of 5
  1. #1
    xdzgor is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Question Output parameter

    Hi - I want to execute an sql "input" statement (using sql server), which has an output parameter. How do I do this? As far as I can see with jdbc, it is not possible to have output parameters with a prepared-statement, only with callable-statements - is this correct? And callable-statements are only used with stored-procedures?

    I do not have a stored-procedure, and need the sql statement to be in code.


    In c# I'd do something like:

    Java Code:
    command.CommandText = @"SET @listId=NEWID()
                                            INSERT INTO ListMap
                                            (SKey, ListId, MapNumber)
                                            VALUES
                                           (@SKey, @listId, @mapNumber)";
    command.CommandType = CommandType.Text;
    I can create an "output parameter" for "listId", and read it back after executing the statement/command.

    Can I do the same in Java?


    Thanks,
    Peter

  2. #2
    xdzgor is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    I have found a method that is workable: I first execute a method like this:

    Java Code:
    UUID id = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
      ps = conn.prepareStatement("select NEWID()");
      rs = ps.executeQuery();
      if (rs.next()) {
        String idstr = rs.getString(1);
        id = UUID.fromString(idstr);
      }
    } finally {
      // close rs and ps
    }
    return id;
    Then I can use this UUID in my other insert statement.

    If anyone has any other ideas or comments to that method, then please let me know.

    Thanks,
    Peter

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    I'm guessing that code for C# is intended for SQL Server?

    JDBC is intended to work with any database with a valid driver. In the JDBC world you'd write a stored procedure to do the work and call that.

  4. #4
    xdzgor is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    Hi - thanks for the reply. Yes, it is for sql server. This is an existing db, which does not have stored-procedures, and I didn't really want to make any just for a java application. On the other hand, some people tell me stored-procedures are a good idea...

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    Unless you can do:
    INSERT INTO ListMap
    (SKey, ListId, MapNumber)
    VALUES
    (@SKey, NEWID(), @mapNumber)

    in SQL Server then your best solution is a stored procedure. Otherwise you are making several calls to the server for no good reason.

Similar Threads

  1. Replies: 0
    Last Post: 09-30-2010, 08:16 PM
  2. how to pass parameter from one jsp to another jsp
    By kader_sit in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 06-10-2010, 02:04 PM
  3. Using ArrayList as a parameter
    By Crypts in forum New To Java
    Replies: 6
    Last Post: 05-13-2010, 06:56 AM
  4. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11:44 PM
  5. Retrieving a parameter through URL.
    By hisouka in forum Java Servlet
    Replies: 1
    Last Post: 09-06-2008, 12:45 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
  •