-->

Friday, December 13, 2013

JFC, Toolkit and Software Tiers used in GUI Programming: Introduction to JAVA

In JAVA, GUI features are supported via JFC. JFC is short for Java Foundation Classes, which encompass a group of features for building graphical user interfaces (GUI's) and adding rich graphics functionally and interactivity to Java applications. One major feature of JFC is Swing API, which includes everything from buttons to split panes to tables and every other functionally to create a GUI application.

Our later articles will describe, how to build GUI programs using Swing API’s features. The GUI programs that will create in Java will contain three tiers of software.
  • Graphical components make up the GUI such as a button or text box or a menu etc. Each graphical components has certain Properties. A property is a characteristic of an object such as its size, colour, title etc. You’ll learn about basics graphical components that Java’s Swing API offers, later in the articles.
  • Events Listeners that get notified about the events and respond to them.
  • The graphical Event Handler that do the work for the user in case the event occurs.
The graphical components are those like the one listed above. You can use them as they are, or extend them into your own classes. Event listeners are registered with GUI objects. In case of occurrence of an event, the objects notifies its event listeners them invokes event-handler method then executes the Java code written in it.

Java GUI toolkit

You know that a GUI in Java is created with at least three kinds of objects: components, events and listeners. A components is an object that defines a screen elements such as a push button, text field, scroll bar, menu, etc. the components that you can use for creating GUI in Java are available through Swing API.
A component is an object that defines a screen element such as a push button, text field, scroll bar, menu etc.

Types of Graphical Components

The graphical controls that you put in GUI applications are broadly of two types: the Container control and the Child control.

The container is control that can hold other control within it e.g. a Panel (there can be multiple controls inside a Panel) or Panes or simply frame (you can put so many controls on it). Control inside a container are known as a child controls. The child controls can exist completely inside their containers. That means you can’t move them outside their container and if try to drag them beyond and the boundary of their container, part of the control gets hidden. When you delete a container control, all its child controls automatically get deleted.

Do you know that Java GUI programming demands that all swing components s must contained in a container? A GUI application must contain a top-level container that can hold other controls in it.
A control is also known as a widget (short for window gadget). You can think of widget/ control as an element of a GUI that can be seen and that display an information and with user, e.g., a window, text field, frame etc. are widgets.
Graphical Controls of Swing

How to Retrieve Records based on One or More Condition: SQL Programming

Logical operators are used in the SELECT statement to retrieve records based on one or more conditions. While querying data in sql programming, programmer can combine more than one logical operator to apply multiple search conditions. As same as in comparison operator, the conditions specified by the logical operators are connected with the WHERE clause.

The syntax for using the logical operators in the SELECT

SELECT column_list
FROM table_name
WHERE conditional_expression 1 logical operator Conditional_expression 2

Where

  • column_list: list of fields to be shown in output.
  • table_name: from the records are to be retrieved.
  • conditional_expression 1 and conditional_expression 2 are any conditional expressions.
  • logical operator: any operator listed below.

Basically there are three types of logical operators in every computer programming. They are Or, And & Not, described below with example.

OR: Return a true value when at least one condition is satisfied. The following SQL query retrieves records from the Department table when the GroupName is either Manufacturing or Quality Assurance:

SELECT * FROM HumanResources.Department WHERE GroupName = 'Manufacturing' OR GroupName = 'Quality Assurance'

How to Retrieve Records based on One or More Condition: SQL Programming

AND: Is used to join two conditions and returns a true value when both the conditions are satisfied. For example, to view the details of all the employees of Adventure Works who are married and working as an Engineering Manager, you can use the AND logical operator, as shown in the following SQL query:

SELECT * FROM HumanResources.Employee WHERE Title = 'Engineering Manager' AND MaritalStatus = 'M'

How to Retrieve Records based on One or More Condition: SQL Programming


NOT: Reverses the result of the search condition. The following SQL query retrieves records from the Department table when the GroupName is not Manufacturing or Quality Assurance:

SELECT * FROM Humansources.Employee WHERE Title = 'Design Engineer' And NOT MaritalStatus = 'F'

The preceding query retrieves all the rows, except the rows that match the condition specified after the NOT conditional expression.

How to Retrieve Records based on One or More Condition: SQL Programming

Thursday, December 12, 2013

How to Use Comparison Operators to Specify Conditions: SQL Programming

Comparison operators test whether two expressions are same or not. These operators can be used on all the sql data types except some of them like text, ntext or image. Equal to, greater then, less than are such operators. The article will let you enable to use these operators with examples.

While arithmetic operators are used to calculate column values, Comparison operators test for similarity between two expressions. You can create conditions in the SELECT statement to retrieve selected rows by using various comparison operators. Comparison operators allow row retrieval from a table based on the condition specified in the WHERE clause. Comparison operators cannot be used on text, ntext, or image data type expressions.
The syntax for using the comparison operator in the SELECT statement is:

 SELECT column_list
 FROM table_name
 WHERE expressiona1 comparison_operator expression2

Where
  • column_list: list of fields to be shown in output.
  • table_name: from the records are to be retrieved.
  • expression1 and expression2: any expression on which the operator will be applied.
  • comparison_operator: may be one listed below.
The following SQL query retrieves records from the Employee table where the vacation hour is more than 20:
SELECT BusinessEntityID, NationalIDNumber, JobTitle, VacationHours FROM HumanResources.Employee WHERE VacationHours > 20

In the preceding example, the query retrieves all the rows that satisfy the specified condition by using the comparison operator. The result is shown in the following image:

How to Use Comparison Operators to Specify Conditions: SQL Programming

The SQL Server provides the following comparison operators.
  • =   Equal to
  • >   Greater than
  • <   Less than
  • >=   Greater than or equal to
  • <=   Less than or equal to
  • <>   Not equal to
  • !=   Not equal to
  • !<   Not less than
  • !>   Not greater than
Sometimes, you might need to view records for which one or more conditions hold true. Depending on the requirements, you can retrieve records based on the following conditions:

How to Retrieve Selected Rows and Calculating Column values: SQL Programming

All the programming languages uses operators to be executed some calculations on the data. In SQL programming there are also some arithmetic operators listed in the article. Using where clause, programmer can easily get selected records, in the query.

Calculating Column Values

Sometimes, you might also need to show calculated values for the columns. For example, the Purchasing.PurchaseOrderDetail table stores the order details such as PurchaseOrderID, ProductID, DueDate, UnitPrice, and ReceivedQty etc. To find the total amount of an order, you need to multiply the UnitPrice of the product with the ReceivedQty. In such scenarios, programmer have apply arithmetic operators.

Arithmetic operators are used to perform mathematical operations, such as addition, subtraction, division, and multiplication, on numeric columns or on numeric constants. As in earlier article + have also used to concatenate the output of sql query.

Here're some arithmetic operations SQL Server supports:

  • + used for addition
  • - used for subtraction
  • / used for division
  • * used for multiplication
  • % used for modulo – the modulo arithmetic operator is used to obtain the remainder of two divisible numeric integer values

All arithmetic operators can be used in the SELECT statement with column names and numeric constants in any combination.

When multiple arithmetic operators are used in a single query, the processing of the operation takes place according to the precedence of the arithmetic operators. The precedence level of arithmetic operators in an expression is multiplication (*), division (/), modulo (%), subtraction (-), and addition (+). You can change the precedence of the operators by using parentheses (()). When an arithmetic expression uses the same level of precedence, the order of execution is from the left to the right.

The EmployeePayHistory table in the HumanResources schema contains the hourly rate of the employees. The following SQL query retrieves the per day rate of the employees from the EmployeePayHistory table:

SELECT BusinessEntityID, Rate, Per_Day_Rate = 8 * Rate FROM HumanResources.EmployeePayHistory

In the preceding example, Rate is multiplied by 8, assuming that an employee works for eight hours. The result is shown in the following image.

How to Retrieve Selected Rows and Calculating Column values: SQL Programming

Retrieving Selected Rows

In a given table, a column can contain different values in different records. At times, you might need to view only those records that match a specific value or a set of values. For example, in a manufacturing organization, an employee wants to view a list of products from the Products table that are priced between $100 and $200.

To retrieve selected rows based on a specific condition, you need to use the WHERE clause in the SELECT statement. Using the WHERE clause selects the rows that satisfy the condition. The following SQL query retrieves the department details from the Department table, where the group name is Research and Development:

SELECT * FROM HumanResources.Department WHERE GroupName = 'Research and Development'

The SQL Server will display the output of the query, as shown in the following figure.

How to Retrieve Selected Rows and Calculating Column values: SQL Programming

In the preceding example, rows containing the Research and Development group name are retrieved. Through the output window, it looks like there are only three records in the database satisfying the condition given above.


Tuesday, December 10, 2013

Customizing and Concatenating Output in SQL Database: SQL Server

Sql Server Management Studio have some options to customizing the display like adding the literals in the sql query to change the column name in output. It also have an option to concatenate strings in records, selected by sql query in sql server. In this article we will do these tasks with some examples in sql.

Customizing the Display

Sometimes, you might be required to change the way the data is displayed on the output screen. For example, if the names of columns are not descriptive, you might need to change the column headings by creating user-defined headings.

Consider the following example that displays the Department ID and Department Names from the Department table of the Adventure Works database. The report should contain column headings different from those given in the table, as specified in the following format.

Department Number  Department Name

You can write the query in the following ways:

  • SELECT ‘Department Number’ – DepartmentID, ‘Department Name’ FROM HumanResources.Department
  • SELECT DepartmentID ‘Department Number’, Name ‘Department Name’ FROM HumanResources.Department
  • SELECT DepartmentID AS ‘Department Number’, Name AS ‘Department Name’ FROM HumanResources.Department

Similarly, you might be required to make results more explanatory. In such case, you can add more text to the values displayed by the columns by using literals. Literals are string values enclosed in single quotes and added to the SELECT statement. The literal value is printed in a separate column as they are written in the SELECT list. Therefore, literals are used for display purpose.

The following SQL query retrieves the department-Id and their name from the Department table and change the column header as specified in the query.

SELECT DepartmentID 'Department Number', Name 'Department Name'
FROM Department

The SQL Server will display the output of the query, as shown in the following figure

Customizing and Concatenating Output in SQL Database: SQL Server
Add caption

Concatenating the Text Values in the Output

As a database developer, you will be required to address requirements from various users, who might want to view results in different ways. You might be required to display the values of multiple columns in a single column and also to improve readability by adding a description with the column value. In this case, you can use the Concatenation operator. The concatenation operator is used to concatenate string expressions. They are represented by the + sign.

The following SQL query concatenates the data of the Name and GroupName columns of the Department table into a single column. Text values, such as “department comes under” and “group”, are concatenated to increase the readability of the output:

SELECT Name + ' department comes under ' + groupName + ' group' AS Department
FROM Department

When you execute the query, it will format the output according to above query. The SQL Server will display the output of the query, as shown in the following figure.

Customizing and Concatenating Output in SQL Database: SQL Server

Retrieving specific Records
Retrieve Selected Rows and Calculate Column values

How to Retrieve Specific Records by SQL Database: SQL Server

By-default when user fire a query on the sql database it provides all the columns in the output, the table have. Database developer can change the no. of columns, to be displayed in the output. Sql Server Management Studio provides some special features explained in the article.

Retrieving Specific Attributes

While retrieving data from tables, you can display one or more columns. For example, the Adventure Works database stores the department details, such as Name and GroupName in the Department table. Users might want to view single column such as Name. Programmer can retrieve the required data from the database tables by using the SELECT statement.

The SELECT statement is used for accessing and retrieving data from a database. The syntax of the SELECT statement is:

SELECT [ALL | DISTINCT] select_column_list
[INTO [new_table_name]]
[FROM {table_name |view_name}
[WHERE search condition]

 

Where
  • ALL: represented with an (*) asterisk symbol and displays all the columns of the table.
  • Select_column_list: name of the table from which data is to be retrieved.
Note:
The SELECT statement can contains some more arguments such as WHERE, GROUP BY, COMPUTE, and ORDER BY that will be explained in later articles. All the examples in this SQL related articles are based on the Adventure Works Database, and can be download from here.
Consider the department table stored in the HumanResources schema of the Adventure Works database. To display all the detail of employees, you can use the following query:
 

SELECT * FROM HumanResources.Department
 

Execute the query and SQL Server will display the output of the query, as shown in the following figure.

How to Retrieve Specific Records by SQL Database: SQL Server 
The result set displays the records in the order in which they are stored in the table. In other words the records are sorted in ascending order of DepartmentId that is primary key of the department table.
   
Note:

The number of rows in the output window may vary depending on the modifications done on the database.
If you need to retrieve specific columns, you can specify the column names in the SELECT statement. For example, to view specific details, such as only Name of the employees of AdventureWorks, you can specify the column names in the SELECT statement, as shown in the following SQL query:
   
SELECT DepartmentId, Name FROM HumanResources.Department

The SQL Server will display the output of the query, as shown in the following figure.


How to Retrieve Specific Records by SQL Database: SQL Server 
In the output, the result set shows the column names the way they are present in the table definition. You can customize these column names, if required. As same as in above sql query you can specify some more columns as per the requirements.

Customizing and Concatening display in SQL Server

Sunday, December 8, 2013

Computer programming : Print string in double quote escape sequences, Verbatim literal

In c#, String is enclosed in double quote, suppose we want to store name then we should use string type. For example
String name="Computer Programming";

Starting double quote define the beginning of the string and ending double quote define the end of the string. Now if we want to print this string then in console application, we have to write:

System.Console.writeline (name);

If we want to print words Computer Programming in double quote or you can say output window display Computer Programming in double quote like "Computer Programming"

Note: If we use double quote beginning of the string like " "Computer Programming" then you get error in the string.

Solution: Use Escape sequence character (\) for keep double quote in the string.
For example :  String name = "\"Computer Programming\" ";
There are different types of escape sequences available in msdn library.

Lets take another example, if we want to print path of the file like C:\csharp\tutorial\beginning, then we  should use escape sequence character before every back slash.
Solution : C:\\csharp\\tutorial\\beginning.
Now this type of solution is too typical and unreadable string. So use verbatim literal for this
Solution : @” C:\csharp\tutorial\beginning”

Here @ symbol is a verbatim literal for removing escape sequences characters in the string.

Computer programming : Print string in double quote escape sequences, Verbatim literal

Here's the full program in c# language
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string name = "\"Computer Programming\"";
            Console.WriteLine(name);
            String path = @"C:\csharp\tutorial\beginning";
            Console.WriteLine(path);
            Console.ReadKey(false);
        }
    }
}

Write above lines of code in c# console application and run the project, string with quote and path with quote will display on the screen.



© Copyright 2013 Computer Programming | All Right Reserved