0

I have a following PL/SQL procedure:

CREATE OR REPLACE PROCEDURE getDogInfo
(Dog_ID IN NUMBER, Dog_name OUT VARCHAR) AS
BEGIN
  SELECT Dog_name INTO Name
  FROM Dog_family
  WHERE ID = Dog_ID;
END;

I need to make a java class file that does the same. I've been trying like this:

import java.sql.*;
import java.io.*;

public class Procedure {

  public static void getDogInfo (int Dog_ID, String Dog_name)
    throws SQLException
    { String sql =
      "SELECT Dog_name INTO Name FROM Dog_family WHERE ID = Dog_ID";
    try { Connection conn = DriverManager.getConnection("jdbc:default:connection:");
      PreparedStatement apstmt = conn.prepareStatement(sql);
      apstmt.setInt(1, Dog_ID);
      apstmt.registerOutParameter(2, java.sql.Types.VARCHAR);
      ResultSet rset = apstmt.executeQuery();
      rset.close();
      apstmt.close(); //Connection close
      }
    catch (SQLException e) {System.err.println(e.getMessage());
    }
  }
}

What am I doing wrong? Can someone help me get this working? Thanks

user3074445
  • 3
  • 2
  • 6

3 Answers3

0

You should read about JDBC (and Java in general too).

The query should be :

SELECT Name 
FROM Dog_family 
WHERE ID = ?

(assuming Name is the column name you are selecting from the table - it wasn't clear whether Name or Dog_name was the column name).

Then after you execute the query and get a result set :

String name = null;

if (rset.next()) {
  name = rset.getInt (1);
}
...
return name;

Finally, your function should return a String. You can't pass the String as a parameter and update its value. String is immutable in Java.

One more thing - the line apstmt.registerOutParameter(2, java.sql.Types.VARCHAR); is not needed. registerOutParameter is only used with CallableStatement, which is a statement you use to execute a stored procedure.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Eran
  • 387,369
  • 54
  • 702
  • 768
0

Have alook at this link showing you how to correctly use PreparedStatements.

You will find that the parameter should be ? not Dog_ID

Try

SELECT Name FROM Dog_family WHERE ID = ?

It will also show you how to iterate through your resultSet

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
0

Well, you do not tell us what the problem is, but I see several issue right away:

  1. Your select statement should not have an INTO clause. That is a PL/SQL construct. You need to return the result of the query back as a result set.

  2. Your input parameter, Dog_ID will not be used, because you have not named the parameter correctly in the SQL statement.

  3. Java string parameters cannot be updated within the method, which I am assuming that is what you are attempting. You either need to return a string value from the method, or use a StringBuilder reference, or some other container to pass in. See this link There is no "out" parameter to register. Read up on result sets here

So, change your SQL statement to something like this(since you are using a positional parameter as opposed to a named parameter):

"SELECT Dog_name FROM Dog_family WHERE ID = ?"
Community
  • 1
  • 1
OldProgrammer
  • 12,050
  • 4
  • 24
  • 45