aspose file tools*
The moose likes JDBC and the fly likes calling SQL Server stored procedures Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "calling SQL Server stored procedures" Watch "calling SQL Server stored procedures" New topic
Author

calling SQL Server stored procedures

bala kiran
Ranch Hand

Joined: Jun 11, 2004
Posts: 34
How do I call SQL Server Stored Procedure from Jsp? This is the code snippet I'm using. This should return me some string value "abc". But, its returning me a numeric zero. Where i'm doing wrong? Its not throwing any exception.

<%@ page import="javax.ejb.*,java.sql.*,javax.naming.*,javax.sql.*" %>
<%
InitialContext initialcontext = null;
DataSource datasource = null;
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
CallableStatement cs = null;
String username = "user";
String password = "password";
String customertype = "A";
try {
initialcontext = new InitialContext();
datasource = (DataSource)initialcontext.lookup("db");
connection = datasource.getConnection();
cs = connection.prepareCall("{?= call tc_sp(?, ?, ?)}");
cs.registerOutParameter(1, Types.VARCHAR);
cs.setString(2, customertype);
cs.setString(3, username);
cs.setString(4, password);
cs.execute();
out.println("result is: " + cs.getString(1));
} catch(Exception e) {
out.println("exception occured : " + e.toString());
} finally {
if(connection !=null) connection.close();
connection= null;
}

%>
Sridhar Srikanthan
Ranch Hand

Joined: Jan 08, 2003
Posts: 366
Originally posted by bala kiran:
How do I call SQL Server Stored Procedure from Jsp? This is the code snippet I'm using. This should return me some string value "abc". But, its returning me a numeric zero. Where i'm doing wrong? Its not throwing any exception.

<%@ page import="javax.ejb.*,java.sql.*,javax.naming.*,javax.sql.*" %>
<%
InitialContext initialcontext = null;
DataSource datasource = null;
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
CallableStatement cs = null;
String username = "user";
String password = "password";
String customertype = "A";
try {
initialcontext = new InitialContext();
datasource = (DataSource)initialcontext.lookup("db");
connection = datasource.getConnection();

cs = connection.prepareCall("{?= call tc_sp(?, ?, ?)}");
cs.registerOutParameter(1, Types.VARCHAR);
cs.setString(2, customertype);
cs.setString(3, username);
cs.setString(4, password);
cs.execute();

out.println("result is: " + cs.getString(1));
} catch(Exception e) {
out.println("exception occured : " + e.toString());
} finally {
if(connection !=null) connection.close();
connection= null;
}

%>



You are calling the stored proc with three parameters
("{?= call tc_sp(?, ?, ?)}"); but setting four parameters,


cs.registerOutParameter(1, Types.VARCHAR);
cs.setString(2, customertype);
cs.setString(3, username);
cs.setString(4, password);


you have to have the same number of parameters if the stored proc has to run and return some value.

Thanks
Sridhar
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

Bala,
The JSP looks ok. Did you check what the stored procedure returns if you call it from the SQL Server command line rather than JDBC?

Sridhar,
Since the first parameter is the result (an out parameter), it is ok to have one more than the number of arguments.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: calling SQL Server stored procedures