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 =
            "com.microsoft.sqlserver.jdbc.SQLServerDriver";

    static String DBURL =
        "jdbc:sqlserver://DBLAB:1290;databaseName=HRDEMO";

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

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

      try
      {

         Class.forName(dbDriver); 

         conn = DriverManager.getConnection(
                                              db_connect_string
                                            , db_userid
                                            , db_password
                                            );
         System.out.println("connected");
      } 

      }
      catch (Exception e)
      {

         e.printStackTrace();

         System.exit(-100);

      }
   }

   static public void dbSP()
   {

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

      try
      {

        cstmt = conn.prepareCall(SP_LISTEN);

        /*
          cstmt.setString("myDataID", callID);
        */

        cstmt.setString(1, myDataID);

        results = cstmt.execute();

        cstmt.close();

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

      }

      catch (Exception e)
      {

         e.printStackTrace();

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

      }
   }

   public static void main(String[] args)
   {

      sp connServer = new sp();

       connServer.dbConnect(DBURL , strDBUser, strDBPassword );

       dbSP();

      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:

http://jtds.sourceforge.net/faq.html#urlFormat

And, given credit to Samuel_xL

link

jdbc:sqlserver://serverName[\instanceName][:port][;databaseName=db_name]

References:

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

 

Leave a Reply

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

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s