java - Calling stored procedure with named JDBC parameters raises exception -


package com.brookfieldres.operations;  import java.sql.callablestatement; import java.sql.connection; import java.sql.date; import java.sql.drivermanager; import java.sql.resultset; import java.sql.sqlexception; import java.sql.timestamp; import java.util.arraylist; import java.util.properties; import java.util.resourcebundle;  import org.apache.log4j.logger;  import com.microsoft.sqlserver.jdbc.sqlserverdatasource;  public class sqlconnection {  private static connection acon = null;  private static callablestatement _cs = null;  private static string _dbserver = null;  private static string _dbusername =  null;  private static string _dbpassword = null;     private static string _dbname = null; private static string _dbinstance = null; private static string _dbwindowsauthentication = null;       private static final logger alogger = logger.getlogger(sqlconnection.class.getname()); //  static resourcebundle resource = resourcebundle.getbundle("resource");        public sqlconnection() {         _dbserver = "localhost";          _dbusername = "newlocationtestuser";         _dbpassword = "testpass123";         _dbname = "newlocationdb";         _dbinstance = "appsql";         _dbwindowsauthentication = "false";     }   public connection getconnection() {          sqlserverdatasource ds = new sqlserverdatasource();         properties properties = new properties();          try {             string dburl = "jdbc:sqlserver://" + _dbserver;             if(!_dbinstance.equalsignorecase(""))             {                 dburl += "\\" + _dbinstance;             }             if(_dbwindowsauthentication.equalsignorecase("true"))             {                 dburl += ";integratedsecurity=true";             }             else             {                 properties.put("user", _dbusername);                 properties.put("password", _dbpassword);             }             dburl += ";";             properties.put("database", _dbname);             acon = drivermanager.getconnection(dburl, properties);             system.out.println("1");            } catch (exception e) {              alogger.error(e.getmessage());          }          {             ds = null;          }         alogger.info("the sql connection has been established.");         return acon;  }   public int insertlocations(timestamp rundate, string rlpcompanyid, string rlplocationid,  string rlpopendate){       int returnval = 0;               try{                   _cs = getconnection().preparecall("{call icurrentlocations01(?, ?, ?, ?)}");                 _cs.settimestamp("rundate", rundate);                 _cs.setstring("companyid", rlpcompanyid);                 _cs.setstring("locationid", rlplocationid);                 _cs.setstring("rlpopendate", rlpopendate );                 returnval = _cs.executeupdate();                  system.out.println("2");              }catch (sqlexception e){                 alogger.error(e.getmessage());                 }finally {                     if (_cs != null){                          try{                              _cs.close();                         }catch(sqlexception e) {                              alogger.error(e.getmessage());                         }                     }                  }             return returnval;             } 

so when attempt run harness, connection made. however, when program attempts call stored procedure "icurrentlocations01". returns error message in logs

" 2016-01-27 13:11:17 error sqlconnection:97 - parameter rundate not defined stored procedure icurrentlocations01."

can please explain doing wrong here? program btw taking information ldap directory , inserting local db.

edit - stored procedure:

use [newlocationdb] go  /****** object:  storedprocedure [dbo].[icurrentlocations01]    script date:     01/27/2016 1:27:10 pm ******/ set ansi_nulls on go  set quoted_identifier on go     alter procedure [dbo].[icurrentlocations01] @rundate datetime,  @companyid varchar,  @locationid varchar,  @rlpopendate varchar    declare @retval int  set @retval = 0   insert currentlocations (rundate, companyid, locationid, rlpopendate ) values (@rundate, @companyid, @locationid, @rlpopendate)  set @retval = @@error return @retval  go 

this may not hoping for, work passing parameters stored procedure name, thereby allowing

  • specify parameters in arbitrary order in command text, and
  • omit parameters have default values.

for stored procedure

create procedure [dbo].[my_sp]      @p1 nvarchar(10) = n'hello',      @p2 nvarchar(10) = n'world' begin     set nocount on;     select @p1 + n', ' + @p2 + n'!' response; end 

the jdbc call

try (callablestatement s = conn.preparecall("{call my_sp (@p2=?)}")) {     s.setstring(1, "gord");     try (resultset rs = s.executequery()) {         rs.next();         system.out.println(rs.getstring("response"));     } } 

returns

hello, gord! 

(tested microsoft jdbc driver 4.1 sql server.)


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -