-->

Monday, January 20, 2014

Literals and with its Types used in NetBeans part-1: Java

In Java Programming, Literals (often referred to as constants) are data items that are fixed data values. Java allows several kinds of literals:
  • Integer-literal
  • Floating-literals
  • Boolean-literals
  • Character-literals
  • Strings-literal
  • Null literals

Integer Literals

Integer literals are whole numbers without any fractional part. The method of writing integer constants has been specified in the following rule.
An integer constant must have at least one more digit and must not contain any decimal point. It may contain either + or – sing. A number with no sing is assumed to be positive. Commas cannot appear in an integer constant.
Java allows three types of integer literals:
  • An integer literal consisting of a sequence of digit is taken to be decimal integer constant unless it begins with 0(digit zero). For instance, 1234, 41, +97,-17 are decimal integer literals.
  • A sequence of digit starting with 0(digit zero) is taken to be an octal integer. For instance decimal integer 8 will be written as 010 as octal integer (810 =108) and decimal integer 12 will be written as 014 as octal integer (1210 = 1410). But make sure that when an integer begins with 0, it must not contain 8 and 9 as these are invalid octal digits.
  • A sequence of digit preceded by 0x or OX is taken to be a hexadecimal integer. For instance decimal 12 will be written as 0XC as hexadecimal integer. But with Hexadecimal constants only 0-9 and A-F can be used. All other letters are illegal.

Floating Literals

Floating literals are also called real literals. Real literals are numbers having fractional parts. These may be written in one of the two forms called fractional form or the exponent form.

A real literal in fraction form consist of signed or unsigned digits including a decimal point between digits. The rule for writing a real constant in fraction from is given below:
A real literal in fraction form must have at least one digit before a decimal point and at last one digit after decimal point. It may also have either + or – sign preceding it. A real literal with no sign is assumed to be positive.        
Valid real literals in fraction form contains 2.0, 17.5, -13.0, -0.00625
Invalid real constants contains 7, (7.), +17/2, 250.26.2, (17,250.262)

A real literals in exponent form consists of two parts: mantissa and exponent. For instance 5.8 can be written as 0.58 X 10 = o.58E01 where mantissa part is 0.58 (the part appearing before E) and exponent part is 1(the part appearing after E). E01 represent 10 1 . The rule for writing a real literal in exponent from is given below:
A real literal in exponent from has two parts: a mantissa and an exponent. The mantissa must be either must be an integer or a proper real literal. The mantissa is followed by a letter E or e and the exponent. The exponent must be an integer.
Valid real literals in exponent form: 152E05, 1.52E07, 0,152E08, 152.0E08, 152E+8, 1520E04

Invalid real literals in exponent from:
      (i)  172.E5                    (at least a digit must follow the decimal point)
      (ii) 1.7E                         (no digit specified for exponent)
      (iii) 0.17E2.3                (exponent cannot have fraction part)
      (iv) 17,225E02             (no comma allowed)
      (v) .25E-7                      (no preceding digits before decimal point)

Continue...

Identifiers and its Naming Conventions in NetBeans: Java

In Java Programming, Identifiers are fundamental building blocks of a program and are used as the general terminology for the names given to different parts of the program viz. variables, objects, classes, function, arrays etc.

Identifier forming rules of Java state the following:

  • Identifiers can have alphabets, digits and underscores and dollar sign characters.
  • They must not be a keyword or Boolean literal or null literal.
  • They must not begin with a digit.
  • They can be any of length.
  • Java is case sensitive i.e., upper-case letters and lower-case letters are treated differently.    


The following are some valid identifiers:

Myfile                 DATE9_7_77        ZT0Z9          A_to_Z
MYFILE                _DS                  _HJI3_JK     isLetterorDIgit
_CHK                   FILE13                 αρετη         $1_to_$10

The following are some invalid identifiers:

DATA_REC             contains special character _ (other than A - Z, a – z and _or )$
29CLCT                   starting with digit
break                      reserved keyword
My.file                   contain special character.

Identifier Naming Conventions:

While making identifier names, certain convention are followed. These are:


  • The names of public methods and instance variables should begin with a lower case letter e.g. Maximum sum
  • For names having multiple words, second and subsequent words beginning character is made capital so as to enhance readability e.g. avgSalaryofEmployees, dateofBirth
  • Private and local variable should use lower case letter e.g. Width, result, final_score
  • The class names and interface names begin with an upper case letter e.g. InitialClass, Employee, and Student
  • The constant should be named using all capital letters and underscores e.g. MAX_VALUE, MAX_MARKS, SPECIAL_SALARY, and TOTAL


Sunday, January 19, 2014

About the Folders in MVC Web Application in Visual Studio: Part 2

Models folder represents the application model for your MVC application, may be called a template for the views. The classes, defined in this folder, are used to define validations of the respective field. For example username is required and password is of some length.

By default MVC application contains a single model i.e. AccountModel, having all the relative classes usable in account management. These classes may be for login, register, change password etc. A brief look about the login model is:

public class LoginModel
    {
        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [Display(Name = "Remember me?")]
        public bool RememberMe { get; set; }
    }

In this class, look out the first field i.e. UserName having some validation i.e. required and display. User have to set some value to username before submit, because it is required and on the view it will be displayed as the string defined.

The second field Password, also a required field and a data type have also been defined for it. The datatype of this field is password means entered string will not be readable. Same as this model, there are register model, change password model are defined with changed fields.

Programmer can write its own models and create their views with respective validation if any. All these models will be placed in the model folder.

Tokens and its Types used in NetBeans: Java

In a passage of text, individual words and punctuation marks are called tokens. In fact, every unit that makes a sentence in Java programming is a token.
The smallest individual unit in a program is known as a Token.
Java has the following types of tokens:

Keywords

Keywords are the words that convey a special meaning to the language complier. These are reserved for special purpose and must not be used as normal identifier names.

The following character sequence, formed from ASCII letters, are reserved for use as keywords and cannot be used as identifiers:
Keywords and its Types used in NetBeans: Java
The keywords const and goto are reserved, even though they are not currently used. This may allow a Java complier to produce better error message if these Java keywords incorrectly appear in programs.

While true and false might appear to be keywords, they are technically Boolean literals. Similarly, while null might appear to be keywords, it is technically the null literal. Thus true, false and null are not keywords but reserved words.

Character Set used in NetBeans: Java Programming

Java programming have its own character set that is used by the programmers to write code easily. Character set is composed of valid characters in a language that may be any letter, digit etc.

In any language, there are some fundamentals that you need to know before you can write even the most elementary programs. This article introduce Java fundamentals to you so that you may start writing efficient code for your GUI applications.

Character set is a set of valid characters that a language can recognize. A character represent any letter, digit or any other sign. Java uses Unicode character set. Unicode is two-byte character code set that has character representing almost all character in almost all language and writing systems around the world including English, Arabic, Chinese and many more.

The first 128 character in the Unicode character set are identical to common ASCII character set. The second 128 characters are identical to the upper 128 characters of the ISOLaptin-1 extended SCII character set. Its next 65,280 that are capable of representing character of nearly all recognized of the world.

You can refer to a particular Unicode character by using the escape sequence \u followed by a four digit hexadecimal.
For example
\u00AE         ©       The copyright Symbol
\u0022          “        The double quote
\u00BD        ½        The fraction ½
\u0394        ∆         The capital Greek letter delta

You can even use the full Unicode character sequence to name your variables. However, chances are your text editor won’t be able to handle more than basic ASCII very well.

Saturday, January 18, 2014

About the Folders in MVC Web Application in Visual Studio

In Visual Studio, when programmer creates a new MVC 4 Web Application some folders and files added by default. These folders contains Controllers, Models, and Views etc. to be complete MVC framework basis. The following figure shows the solution explorer created by default application:

MVC 4 Solution Explorer in Visual studio

App_Data

As the name implies, this folder will contains all the data related to our application. In further articles, we will use this folder to add a database (SQL database).

App_Start

All the files used to start the application are placed in this folder. These files contains AuthConfig, BundleConfig and RouteConfig etc. To specify routes for the application, add some files dynamically may be done by using these files.

Content

Content folder contains themes folder including css files, static files, icons and images used by the application. A default Site.css file is added in this folder that creates standard theme for the application. Programmer can change these themes as per the roles, account by using these folder.

Controllers

MVC gives all the controller the standard name suffix with “Controller”. The default mvc application have two controller i.e. AccountController and HomeController. These controller have some actions defined, used for redirection of the user. Programmer can add more controllers as per the requirements and any controller have multiple actions discussed later.

Models

Classes represents the application model for your application are defined in this folder. These classes may be used to create validation for the fields entered by the user. We will add some model classes in this folder in later article. It have default model added in the visual studio MVC application.

Views

Pages shown to the user, according to the controller classes are stored in this folder. Related views may be placed in a folder having the same name of controller by which these views relates. Any controller have some standard actions like Index, Create, Edit and Delete. The related views folder have all these views having the same name.
These views may have aspx or razor pages according to the requirement of the programmer.

Scripts

JavaScript files used by the application are stored in this folder. By default MVC add approx. 15 js files in the folder which are standard java script files. Programmer can also add some new files and use them in the application.

Thursday, January 16, 2014

How to search item from database using stored procedure in ASP.NET

Step-1: First create a Database Table in visual studio
I have some columns like

SNO    int      primarykey with auto increment by 1
Title      nvarchar(50)  Allow Null
MetaDescription  nvarchar(MAX)  Allow Null
URL   nvarchar(200) Allow Null

Step-2: Add TextBox, Button and GridView control into the .aspx page
Step-3: Create a static class for connection.
Step-4: Handle Button_click event

<p>
        Enter Text Here :
        <asp:TextBox ID="TextBox1" runat="server" Height="32px" Width="237px"></asp:TextBox>
    </p>
    <p>
        <asp:Button ID="Button1" runat="server" Height="33px" onclick="Button1_Click" 
            Text="Search" Width="122px" />
    </p>
    <p>
        <asp:GridView
         AutoGenerateColumns ="false"
          ID="GridView1" runat="server"
           Height="180px" Width="426px"
                        GridLines ="None" 
                                AllowPaging="True" 
            onpageindexchanging="GridView1_PageIndexChanging" PageSize="5" 
            ShowHeader="False">

           <Columns>
           <asp:TemplateField>
              <ItemTemplate>
                  <asp:HyperLink ID="HyperLink1" runat="server" Text ='<%# Eval("title") %>' NavigateUrl='<%# Eval("URL") %>'/>
                  <br />
                  <asp:Label ID="Label1" runat="server" Text='<%# Eval("title") %>'/><br />
               <asp:Label ID="description" runat="server" Text='<%# Eval("metaDescription") %>'/>
               <asp:Label ID="Label2" runat="server" Text='<%# Eval("keywords") %>'/>
              </ItemTemplate>
               
           </asp:TemplateField>
           
          
           </Columns>
        
        </asp:GridView>
    </p>
<p>
        &nbsp;</p>

.aspx.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection();
    SqlCommand cmd = new SqlCommand();
    DataSet ds = new DataSet();
        
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        loadgrid();

    }

    private void loadgrid()
    {
        con.ConnectionString = connection.Connectionstring;
        con.Open();
        cmd.CommandText = "GetEngine";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@parameter", TextBox1.Text);
        cmd.Connection = con;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        loadgrid();
    }
}

Connection Class  file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;

/// <summary>
/// Summary description for connection
/// </summary>
public static class connection
{
    private static string DBConnectionString;
static connection()
{
        DBConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
}
    public static string Connectionstring
    {
        get
        {
            return DBConnectionString;
        }
    
    }
}

Stored Procedure code

Create Procedure GetEngine
@parameter nvarchar(100)
As
Select * from SearchEngine where Title like '%'+@parameter+'%' or MetaDescription like '%'+@parameter+'%' or  URL like '%'+@parameter+'%'


Code generate the following output

How to search item from database using stored procedure in ASP.NET

© Copyright 2013 Computer Programming | All Right Reserved