Sunday 24 June 2012

C# Properties : using the getter/setter


C# properties: Properties in C# are used to provide protection to the attributes of a class against direct reading or writing to it . By using properties we fix the rule that a particular attribute of the class can be read or written by means of property associated to that attribute.


These can be classified into 3 types:

  • Read only properties : We implement only the getter in the property implementation.
Syntax:
public String Name
{
get
{
return name;
}
                }
  • Write only properties :We implement only the Setter in the property implementation.
Syntax:
public String Name
{
set{name=value;}
}
  • Auto implemented properties: This is the most commonly used type. we define the both getter and setter without any actual implementation.
Syntax:

public String Name{get; set;}

Example: Accessing the Employee class with properties


Employee.cs
using System;
namespace lab1
{
public class Employee
{
private string name;
private long empid;

public Employee (String Name,long id){ name=Name;empid=id;}
public String Name
{
get
{
return name;
}
set{name=value;}

}
public long Empid
{
get

return empid;
}
set{empid=value;}

}
public override string ToString ()
{
return ("Name:"+name+"\nEmployee ID:"+empid);
}
}
}


Main.cs



using System;
using Gtk;


namespace lab1
{
class MainClass
{
public static void Main (string[] args)
{
Employee E1=new Employee();

E1.Name="kiran";
E1.Empid=1234;

Console.WriteLine("Employee details\n {0}", E1);
Console.WriteLine("End of Employee details",E1);

Console.ReadKey();
}
}
}




Publish Your Own Website For Free

I have been planning to write on this topic for quite some time so finally here it comes.

Before actually trying to publish our website lets understand what this whole process takes.By this I mean that lets first understand the pre requisites for publishing the website and also understand what makes them so very much needed for publishing the website.

The pre requisites are :
  1. A domain name.
  2. The hosting service provider.
  3. Host.
  4. Template. 
1. The Domain name: Is basically used to point to a particular IP address . That means it maps or identifies the IP address associated to the name of your website. To understand it better lets take a few examples of the same:

  • gov
  • co
  • co.cc
  • com
  • in
  • net
  • org
what makes it so important ?
Since it is difficult to remember the IP addresses you need alphabetical name to identify our site and also to let the audience find you on the the internet you need a unique IP address.

2.The Hosting Service provider:  Is the service that allows us to publish our web pages online on the internet. To publish the web pages all we need is some space on the server.Don't worry if you want to have this space for free. There are loads of companies which provide you with this server space for free :) .
An example of such a provider can be 000webhost (http://www.000webhost.com/). 

A website cannot exist without a web space and thus without a webhost,  well by this we just answered the question "what makes it so important ?"

3.The host: The application that basically holds the content for our website.Don't worry they are also available for free. An example of this is FileZilla  which can be used on Windows, Linux,Mac OS X . You can easily download , install and upload your file to the your server space of your website. Sites like GoDaddy, 000webhost recommend FileZilla.

4. Template:  This is all about the look and feel of the website we are trying to create and publish. If you have a good knowledge of Html and CSS you can self code it otherwise you can buy it or even download it for free from some free template providing website. 

now lets come to the steps for actually publishing the website that too for free :) .

Step 1: Registering the domain name: Register the domain name.
Step 2:Getting the server space for the website: 


Step 3: Getting the template : Buy it or download for free or Code it or download it and make your own edits .
Step 4: Download and install the host and then uploading the files to the server:






Friday 22 June 2012

Database Connection To Java Application with Login and Registration Form



The Database  I have used here is SQLite version3.7.8
 To download the SQLite shell visit : http://www.sqlite.org/download.html
To download the SQLite jar file visit: http://www.zentus.com/sqlitejdbc/
Main.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sample;


/**
 *
 * @author SunShine
 */
public class Sample {


    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
       Login login=new Login();
       login.setVisible(true);
       
        
    }
}


Register.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sample;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.*;

/*
 * Register.java
 *
 * Created on 22 Jun, 2012, 12:07:46 PM
 */
/**
 *
 * @author SunShine
 */
public class Register extends javax.swing.JFrame {

    /** Creates new form Register */
    public Register() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        UsernameLabel = new javax.swing.JLabel();
        UserNameTextField = new javax.swing.JTextField();
        PasswordLabel = new javax.swing.JLabel();
        RegisterButton = new javax.swing.JButton();
        PasswordField = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        UsernameLabel.setText("Username:");

        PasswordLabel.setText("Password:");

        RegisterButton.setText("Register Me !");
        RegisterButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RegisterButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(UsernameLabel)
                            .addComponent(PasswordLabel))
                        .addGap(44, 44, 44)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(PasswordField)
                            .addComponent(UserNameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(116, 116, 116)
                        .addComponent(RegisterButton)))
                .addContainerGap(112, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(UsernameLabel)
                    .addComponent(UserNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PasswordLabel)
                    .addComponent(PasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(97, 97, 97)
                .addComponent(RegisterButton)
                .addContainerGap(103, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>

private void RegisterButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
// TODO add your handling code here:
     
     PreparedStatement statement = null;

        try {
            Class.forName("org.sqlite.JDBC");
           Connection connection =DriverManager.getConnection("jdbc:sqlite:C:\\test.db");
            statement = (PreparedStatement) connection.prepareStatement("insert into test(username,password)values(?,?)");
            statement.setString(1,UserNameTextField.getText() );
            statement.setString(2,(PasswordField.getText()));
            

            statement.execute();

        } catch (Exception ex) {
            ex.printStackTrace();
        } 
}                                              

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Register.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Register.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Register.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Register.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Register().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JTextField PasswordField;
    private javax.swing.JLabel PasswordLabel;
    private javax.swing.JButton RegisterButton;
    private javax.swing.JTextField UserNameTextField;
    private javax.swing.JLabel UsernameLabel;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration
}

Login.java


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/*
 * Login.java
 *
 * Created on 23 Jun, 2012, 3:15:08 PM
 */
package sample;
import java.sql.*;
import javax.swing.JOptionPane;
/**
 *
 * @author SunShine
 */
public class Login extends javax.swing.JFrame {


    /** Creates new form Login */
    public Login() {
        initComponents();
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {


        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jPasswordField1 = new javax.swing.JPasswordField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();


        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);


        jLabel1.setText("Username:");


        jLabel2.setText("Password:");


        jButton1.setText("Login");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });


        jButton2.setText("Register");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });


        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(58, 58, 58)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jPasswordField1)
                            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(79, 79, 79)
                        .addComponent(jButton1)
                        .addGap(50, 50, 50)
                        .addComponent(jButton2)))
                .addContainerGap(141, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(84, 84, 84)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(99, Short.MAX_VALUE))
        );


        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );


        pack();
    }// </editor-fold>


private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
    Register register=new Register();
    register.setVisible(true);
}


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
    PreparedStatement statement=null;
    ResultSet resultSet=null;
    try{
        Class.forName("org.sqlite.JDBC");
        Connection connection=DriverManager.getConnection("jdbc:sqlite:c:\\test.db");
        statement=(PreparedStatement) connection.prepareStatement("select * from user where username=? and password=?");
        statement.setString(1,jTextField1.getText() );
        statement.setString(2,new String(jPasswordField1.getPassword()));
        
        resultSet=statement.executeQuery();
        boolean exists=false;
        while(resultSet.next()){
            exists=true;
            JOptionPane.showMessageDialog(null,"Login success","Information",JOptionPane.INFORMATION_MESSAGE);
        }
        
    }
    catch(Exception ex){
        ex.printStackTrace();
        
    }
}


    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>


        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {


            public void run() {
                new Login().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration
}


You can watch the video tutorial for the same .... I have performed each and every step ...with explanation.


Part 1/3:

Part 2/3:



Part 3/3:








I hope it will help you :)