• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

calling SQL Server stored procedures

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}

%>
 
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 41905
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic