Oracle/Sun Java – JDBC – Stored Procedure – DB/MS SQL Server

Oracle/Sun Java – JDBC – Stored Procedure – DB/MS SQL Server

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.CallableStatement;
import java.sql.SQLException;
import java.sql.Types;

public class sp

   static Connection conn = null;

   static String dbDriver = 

    static String DBURL =

    static String strDBUser = "butifier";
    static String strDBPassword = "pwd";

   public void dbConnect(String db_connect_string,
                         String db_userid,
                         String db_password)



         conn = DriverManager.getConnection(
                                            , db_userid
                                            , db_password

      catch (Exception e) 




   static public void dbSP()

      String myDataID = "1";
      CallableStatement cstmt = null;
      ResultSet rs = null;
      String SP_LISTEN = "{call [dbo].[listenToMyDemo](?) }";
      boolean results;
      int rowsAffected = 0;


        cstmt = conn.prepareCall(SP_LISTEN);

          cstmt.setString("myDataID", callID);

        cstmt.setString(1, myDataID);

        results = cstmt.execute();


        System.out.println("DB processed");


      catch (Exception e) 


         System.out.println("Problem in dbSP");


   public static void main(String[] args)

      sp connServer = new sp();

       connServer.dbConnect(DBURL , strDBUser, strDBPassword );


      sp = null;



Quick Points:

1) JTDS does not support the placement of the database name right after the port as the JDBC Syntax supports.  So to ensure that you ‘re in proper database specify databasename as part of the properties ‘element’

More explanation:

And, given credit to Samuel_xL



  1. Calling SQL Server Stored Procedures with Java using JDBC
  2. PreparedStatements and Performance
  3. Connect to MS SQL Server Using JDBC
  4. How to configure Hibernate Config File for SQL Server

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s