Saturday, December 6, 2008

Programming Aid Coad

  1. try {
  2. // Call a function that takes NO parameters
  3. cs = connection.prepareCall("{call procedure_name}");
  4. cs.execute(); //execute the stored procedure
  5. // Call a function that takes a String IN parameter
  6. // An IN parameters is when you input a value
  7. // for the stored procedure
  8. cs = connection.prepareCall("{call procedure_name_in(?)}");
  9. cs.setString(1, "ABC"); // Set the value for the IN parameter
  10. cs.execute();
  11. // Call a function that returns a String OUT parameter
  12. // An OUT parameter is when the stored procedure
  13. // has an output value
  14. cs = connection.prepareCall("{call procedure_name_out(?)}");
  15. cs.registerOutParameter(1, Types.VARCHAR);
  16. // Register the types of the return value and OUT parameter
  17. cs.execute();
  18. String outParam = cs.getString(1); // OUT parameter
  19. // Call a function with one IN/OUT parameter
  20. // An IN/OUT parameter has been an input and an output
  21. cs = connection.prepareCall("{call procedure_name_inout(?)}");
  22. cs.registerOutParameter(1, Types.VARCHAR);
  23. cs.setString(1, "ABC");
  24. cs.execute();
  25. String outParam = cs.getString(1);
  26. } catch (SQLException e) {
  27. }

CallableStatement for Java

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.

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 for Java

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.

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:

Sample Code on Website

You may have noticed the fancing code snippets that accompany many of the tutorials on this website! I would love to say I developed this myself, but the truth is that this is a free resource free for anybody to download and use. It is called SyntaxHighlighter and can be found at http://code.google.com/p/syntaxhighlighter/.

What is Included?

So what does the Syntax Highlighter come with? Below is a list my favorite features of this software:

  1. It’s completely free!
  2. Associate keywords for supported languages are highlighted.
  3. Each line of code is numbered and colored for easier reading!
  4. Allows one click to code only view.
  5. Allows one click copy to clipboard of code.
  6. Allows one click copy to printer.

What Comes With the Download

The file can be downloaded from their official website at http://code.google.com/p/syntaxhighlighter/ as a rar compressed file. Inside the compressed file, you will find various folders and files. The core files that are required include:

  1. clipboard.swf
  2. shCore.js
  3. SyntaxHighlighter.css

Still, there are many more JavaScript files that have been included in the download. Not all of these files are required. Instead, you choose additional optional JavaScript files for the programming or scripting languages you want the Syntax Highlighter to associate.

C# Dictionary Collection

A Dictionary object in C# has become one of my recent favorites. Below are a few important aspects:

  1. Requires including System.Collections.Generic;
  2. Each entry in a Dictionary is a KeyValuePair (consisting of two objects: key and value)
  3. The key must be unique
  4. The value does not need to be unique
  5. Both the key and value can be any object (string, int, custom class, etc…)
  6. Retrieving a value via a key take close to O(1) time
  7. The order of a KeyValuePair enumeration is not defined

C# programmer deals

At some point, every C# programmer deals with populating, selecting, and even deleting from a DataTable. A moderately advanced operation is transferring an entire single row from an original DataTable and inserting it into a new DataTable. My original thoughtprocess is the following:

view plaincopy to clipboardprint?
  1. DataTable originalTable = new DataTable();
  2. originalTable.Columns.Add("id", typeof(int));
  3. originalTable.Columns.Add("firstName", typeof(string));
  4. originalTable.Columns.Add("lastName", typeof(string));
  5. originalTable.Columns.Add("age", typeof(string));
  6. originalTable.Columns.Add("gender", typeof(string));
  7. DataRow dr = originalTable.NewRow();
  8. dr["id"] = 1;
  9. dr["firstName"] = "Victor";
  10. dr["lastName"] = "Chen";
  11. dr["age"] = 5;
  12. dr["gender"] = "M";
  13. originalTable.Rows.Add(dr);
  14. dr = originalTable.NewRow();
  15. dr["id"] = 2;
  16. dr["firstName"] = "John";
  17. dr["lastName"] = "Doe";
  18. dr["age"] = 35;
  19. dr["gender"] = "M";
  20. originalTable.Rows.Add(dr);
  21. dr = originalTable.NewRow();
  22. dr["id"] = 3;
  23. dr["firstName"] = "Jane";
  24. dr["lastName"] = "Doe";
  25. dr["age"] = 32;
  26. dr["gender"] = "F";
  27. originalTable.Rows.Add(dr);
  28. DataTable newTable = originalTable.Clone();
  29. newTable.Rows.Add(originalTable.Rows[1]);

Though the above example is long, lines 1 - 30 handle creating and populating the original DataTable (If you need explanation of how this works, check out Add Rows to a DataTable in C# tutorial). Line 32 creates a new DataTable by taking the structure of the original DataTable. In Line 33, we attempt to select Row 1 from the original DataTable and add it as a new row in the new DataTable. However, the above example above does not work. It will throw the runtime error below.

This row already belongs to another table.

The correct solution takes more lines of coding (and therefore more logic), but ultimately gets the job done. It involves using a DataRow’s ItemArray method to transfer a row’s data column by column through an object array, and inserting them column by column back into a new DataRow. See the example below:

view plaincopy to clipboardprint?
  1. DataTable originalTable = new DataTable();
  2. originalTable.Columns.Add("id", typeof(int));
  3. originalTable.Columns.Add("firstName", typeof(string));
  4. originalTable.Columns.Add("lastName", typeof(string));
  5. originalTable.Columns.Add("age", typeof(string));
  6. originalTable.Columns.Add("gender", typeof(string));
  7. DataRow dr = originalTable.NewRow();
  8. dr["id"] = 1;
  9. dr["firstName"] = "Victor";
  10. dr["lastName"] = "Chen";
  11. dr["age"] = 5;
  12. dr["gender"] = "M";
  13. originalTable.Rows.Add(dr);
  14. dr = originalTable.NewRow();
  15. dr["id"] = 2;
  16. dr["firstName"] = "John";
  17. dr["lastName"] = "Doe";
  18. dr["age"] = 35;
  19. dr["gender"] = "M";
  20. originalTable.Rows.Add(dr);
  21. dr = originalTable.NewRow();
  22. dr["id"] = 3;
  23. dr["firstName"] = "Jane";
  24. dr["lastName"] = "Doe";
  25. dr["age"] = 32;
  26. dr["gender"] = "F";
  27. originalTable.Rows.Add(dr);
  28. DataTable newTable = originalTable.Clone();
  29. DataRow newRow = newTable.NewRow();
  30. newRow.ItemArray = originalTable.Rows[1].ItemArray;
  31. newTable.Rows.Add(newRow);

Keeping consistent with our original example, lines 1-30 handle creating a DataTable. On line 32, create a DataTable. On Line 32, create a new DataRow. On Line 33, I select Row 1 from the Original DataTable, take it’s ItemArray, and transfer it to the newly created DataRow from Line 32. Finally, on line 34, I transfer newRow into the newTable.

If you have more than one row you would like to copy, merely repeat this process through a loop. Happy Coding!