An array of C#, PHP, and HTML programming articles, tutorials, and resources

Posts Tagged ‘ example ’

I recently learned how to call a stored procedure using Java.

CallableStatement is used when a Java application needs to call a stored procedure. The stored procedure contains the SQL query to be executed on the database and is stored on the database.

To use CallableStatement, the Java code will need to first import the CallableStatement class.

import java.sql.CallableStatement;

In your method, create a CallableStatement object:

CallableStatement cs = null;

Create a connection with the database:

cs = connection.prepareCall("{ call procedure_name}");

connection is the connection to the database, and prepareCall is the method used to call the stored procedure. The syntax used in the prepareCall parameters is shown above, but replace procedure_name with the actual stored procedure name.

Next, use cs.execute(); to execute. So to put it together, you will need to add it to try/catch block as shown below:

CallableStatement cs = null;
try{
  // The syntax for a stored procedure with no
  // parameters would look like this:
  cs = connection.prepareCall("{ call procedure_name}");
  cs.execute();
} catch (SQLException e) {
}

This is the simplest use of CallableStatement. The stored procedure above does not have any parameters. If the stored procedure has parameters, you will need to modify your code to add the parameters in the Java code. CallableStatements can have IN parameters, OUT parameters, IN/OUT parameters, or no parameters. The Java API for CallableStatement is a good reference as well. Here are some examples I found that are helpful and have the proper syntax:

try {
  // Call a function that takes NO parameters
  cs = connection.prepareCall("{call procedure_name}");
  cs.execute(); //execute the stored procedure

  // Call a function that takes a String IN parameter
  // An IN parameters is when you input a value
  // for the stored procedure

  cs = connection.prepareCall("{call procedure_name_in(?)}");
  cs.setString(1, "ABC"); // Set the value for the IN parameter
  cs.execute();

  // Call a function that returns a String OUT parameter
  // An OUT parameter is when the stored procedure
  // has an output value

  cs = connection.prepareCall("{call procedure_name_out(?)}");
  cs.registerOutParameter(1, Types.VARCHAR);
  // Register the types of the return value and OUT parameter
  cs.execute();
  String outParam = cs.getString(1); // OUT parameter

  // Call a function with one IN/OUT parameter
  // An IN/OUT parameter has been an input and an output

  cs = connection.prepareCall("{call procedure_name_inout(?)}");
  cs.registerOutParameter(1, Types.VARCHAR);
  cs.setString(1, "ABC");
  cs.execute();
  String outParam = cs.getString(1);
} catch (SQLException e) {
}

How to Use a Generic List in C#

by Victor | September 10, 2008 in C# | 3 Comments

Using a Generic List in C# is an efficient method of storing a collection of variables. And probably the best part is that the List is strongly typed and casting (which degrades performance) will no longer be necessary. Your collection of variables should be of the same data type. Generic Lists were first introduced into the .NET Framework 2.0. Before we begin our Generic List example, first remember to include the correct namespace below:

using System.Collections.Generic;

In our example, we will store the integers 5, 8, 435, 76, 3256, and -85. Our next step is to initialize a List using Generics. Because we are storing integers, our it with be an int Generics List. If you wish to create the List as another variable type, simply replace int with double, decimal, string, or another type of element. We will give our List the name myGenericIntegerList.

List<int>myGenericIntegerList = new List<int>();

Now that the List has been created and initialized, we easily add integer elements. Since we stated this list as an int, the input will now be strongly typed to only accept integer values. We will now add the integeres5, 8, 435, 76, 3256, and -85 iin the example below:

myGenericIntegerList.Add(5);
myGenericIntegerList.Add(8);
myGenericIntegerList.Add(435);
myGenericIntegerList.Add(76);
myGenericIntegerList.Add(3256);
myGenericIntegerList.Add(-85);

Your generic int List is now complete! Another advantage to using the generics List is there are helper functions such as average, contains, and counts, and toArray. As brought up by Khalid, the average function is an extension method.

double average = myGenericIntegerList.Average();
bool contains = myGenericIntegerList.Contains(4);
int count = myGenericIntegerList.Count;
int[] intArray = myGenericIntegerList.ToArray();

For more functions, you can either visit the MSDN and search for “List(T) Methods” or better just give the function a try in Visual Studio and let Intellisense show you other functions.

Creating a Class in PHP

by Victor | September 1, 2008 in PHP | 1 Comment

The object oriented programming concept is great for not only organizing code, but also easing readability. Creating a class in PHP is easy and we will go through the basics. For starters, creating a PHP class is as easy as:

class myFirstClass {
   // CLASS CONTENT GOES HERE
}

The next step is to add variables and functions to the class. One of the first functions every class has is a constructor. A constructor is a function that is run when the class object is created. To create a constructor function for your class, a default constructor function name by “__construct()” is used. See the example below

class myFirstClass {
   public function __construct() {
      // CONSTRUCTOR CONTENT HERE
   }
}

Next, you can add variables to your class. Variables can be defined as public or private. Private variables can be accessed only from functions belonging to the class. Public variables on the other hand can be accessed by functions belonging to the class along with functions outside the class. Once we have defined our variables, we can initialize them in our constructor. Below is an expanded example of a public and private variable.

class myFirstClass {
   private $myPrivateVariable;
   public $myPublicVariable;

   public function __construct() {
      $this->myPrivateVariable = "Private Message";
      $this->myPublicVariable = "Public Message";
   }
}

Lastly, we can add functions to our class. Because the $myPrivateVariable is private, we cannot assign it without a custom function. Therefore, in our next example, we will create a function that assigns a string value to the $myPrivateVariable. This function should be public since we want to be able to access it from outside of the class.

class myFirstClass {
   private $myPrivateVariable;
   public $myPublicVariable;

   public function __construct() {
      $this->myPrivateVariable = "Private Message";
      $this->myPublicVariable = "Public Message";
   }

   public function assignMyPrivateVariable($message) {
      $this->myPrivateVariable = $message;
   }
}

Now, we have completed a very basic PHP class. Follow the same steps to create more variables and more functions to make the class more powerful and useful.