Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

calling SQL Server stored procedures

 
bala kiran
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 366
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic