I am having difficulty executing a MS SQL Server stored procedure from Java/jsp. I wish to return a table set of data; the last line of the stored procedure is a regular select statement from a table.
(From this point, executing a stored procedure is a cinch in PHP
.)
I took a look at these sites for help:
www.2netave.com
www.stackoverflow.com
I didn't realize there's a function just for stored procedures, as I was using createStatement()
instead.
Now, please understand the stored procedure executes perfectly in SQL Server Management Studio and I have had no problems executing ad-hoc queries in jsp/java with createStatement()
.
I created a simple stored procedure that takes no arguments, just to narrow down the problem:
CREATE PROCEDURE sp_test AS
BEGIN
PRINT 'HELLO WORLD'
END
Here is the code in my jsp page:
Class.forName("net.sourceforge.jtds.jdbc.Driver");
java.sql.Connection conn = java.sql.DriverManager.getConnection("jdbc:jtds:sqlserver://MySQLServer:1433/test", "user", "pass");
java.sql.CallableStatement cs = conn.prepareCall("{call sp_test}");
java.sql.ResultSet ResultSet = cs.execute();
The browser is telling me that the page cannot be displayed because an interal server error has occurred. I know this means there is an issue with the code above.
I tried this:
java.sql.ResultSet ResultSet = cs.executeQuery();
And this:
java.sql.CallableStatement cs = conn.prepareCall("{execute sp_test}");
And this:
java.sql.CallableStatement cs = conn.prepareCall("{exec sp_test}");
And nothing worked. Once I can get this working, then I can run an actual stored procedure that returns table data from a select statement. But I can't even get this dummy stored procedure to work.
What am I doing wrong here?
Thank you.
Update:
Checked the server logs (IIS) and my HTTP proxy, fiddler, and it doesn't report anything. However, the IIS is using tomcat as the servlet engine for jsp pages. And tomcat log file reported the following:
An error occurred at line: 20 in the jsp file: /test.jsp
Type mismatch: cannot convert from boolean to ResultSet
17:
18: java.sql.CallableStatement cs = conn.prepareCall("{call sp_test}");
19:
20: java.sql.ResultSet ResultSet = cs.execute();
21:
22: // java.sql.ResultSet ResultSet = state.executeQuery(SQL);
23:
I tried changing the above to:
cs.execute();
And the log files reported:
- Servlet.service() for servlet jsp threw exception
java.sql.SQLException: The EXECUTE permission was denied on the object 'sp_test', database 'test', schema 'dbo'.
So, I have figured out I have to GRANT EXECUTE
to the user. The other issue is returning table data from a stored procedure.
If I have a procedure like this:
CREATE PROCEDURE sp_test2 AS
BEGIN
SELECT * FROM TABLE
END
How do I manipulate the table data in jsp? Would ResultSet
work or is that only for ad-hoc queries, as opposed to stored procedures, where one would use createStatement()
to execute a query?
Thank you.
Update2:
Solution:
In order to manipulate table data, I had to use this:
java.sql.ResultSet RS = cs.executeQuery();
It failed on execute()
and it failed on naming the ResultSet
object "ResultSet". It never complained about this in the past with createStatement()
. But for some reason, with stored procedures, it didn't like this naming convention.
Thank you.
Frequently we deal with other fellow java programmers work which create these Stored Procedure. and we do not want to mess around with it. but there is possibility you get the result set where these exec sample return 0 (almost Stored procedure call returning zero).
check this sample :
I came to this solutions after few days trial and error, googling and get confused ;) it execute below Stored Procedure :
you will get the "java SQL Code" by right click on stored procedure in your database. something like this :
check the query String I've done, that is your homework ;) so sorry answering this long, this is my first answer since I register few weeks ago to get answer.
Our server calls stored procs from Java like so - works on both SQL Server 2000 & 2008:
FWIW, sp_test will not be returning anything but an integer (all SQL Server stored procs just return an integer) and no result sets on the wire (since no SELECT statements). To get the output of the PRINT statements, you normally use the InfoMessage event on the connection (not the command) in ADO.NET.
Thank to Brian for the code. I was trying to connect to the sql server with
{call spname(?,?)}
and I got errors, but when I change my code toexec sp...
it works very well.I post my code in hope this helps others with problems like mine: