Saturday 30 June 2012

How to install and run java programs on windows

Hi Everyone,

In this tutorial we will learn the very basic of running a simple java program on windows machine
This blog is for the newbie in java who wants to start development in java.

Pre-Requisite


1) Any JDK (recommended jdk1.6)

Note: to run a java program you only need JRE but to write and compile the java program you need JDK
 if you do not have JDK download  it from here JDK 1.6
You will get a list of downloads, Please download JDK for your operating system
if you are using 32-bit OS then select x86 from the list and if you are using 64-bit operating system download x64. 

2) Any text Editor to write a java program (Notepad , notepad++)

Installation of JDK



1) Run the set up file after you have downloaded the JDK file

2) Choose the path where you want to install the JDK by default it is C:\Program Files\Java\jdk1.6.0_33
     if you change this path please remember the path you have chosen for installation

3) Rest part is self - explanatory just click next and it will be installed on your machine


Setting Environment Variables


This part is very important to compile and run the java programs, most of the time if we do not set up environment variables properly we may end up in error while running the java programs.

PATH Variable:-

You can run Java applications just fine without setting the PATH environment variable. Or, you can optionally set it as a convenience.

Set the PATH environment variable if you want to be able to conveniently run the executables (javac.exe, java.exe, javadoc.exe, and so on) from any directory without having to type the full path of the command in command prompt.

 If you do not set the PATH variable, you need to specify the full path to the executable every time you run it, such as:
C:\Program Files\Java\jdk1.6.0_33\bin\javac HelloWorld.java
 
The PATH environment variable is a series of directories separated by semicolons (;). Microsoft Windows looks for programs in the PATH directories in order, from left to right. You should have only one bin directory for the JDK in the path at a time (those following the first are ignored), so if one is already present, you can update that particular entry.
It is useful to set the PATH environment variable permanently so it will persist after rebooting. To make a permanent change to the PATH variable, use the System icon in the Control Panel. The precise procedure varies depending on the version of Windows:

Value to add in path variable is where our JDK is installed
for e.g. C:\Program Files\Java\jdk1.6.0_33\bin



Windows XP
  1. Select Start, select Control Panel. double click System, and select the Advanced tab.
  2. Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New.
  3. In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.
Windows Vista:
  1. From the desktop, right click the My Computer icon.
  2. Choose Properties from the context menu.
  3. Click the Advanced tab (Advanced system settings link in Vista).
  4. Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New.
  5. In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.
Windows 7:
  1. From the desktop, right click the Computer icon.
  2. Choose Properties from the context menu.
  3. Click the Advanced system settings link.
  4. Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New.
  5. In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.

Hello World Program  in Java


1) Open any text editor just copy paste the below code

/**
 * The HelloWorld class implements an application that
 * simply prints "Hello World!" to standard output.
 */
class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World! inside c"); // Display the string.
    }
}
 
Note: Type all code, commands, and file names exactly as shown. Both the compiler
(javac) and launcher (java) are case-sensitive, so you must capitalize 
consistently.


HelloWorld is not the same as helloworld in java.
 


2) It is a good practice to save the file with the name of the class in which your main method is
    because when you compile this file our .class file will be generated with the name of class in which main    method is present
     So save the file with the name HelloWorld.java
     For now create a folder name javaPrograms in C drive and save the source file in that folder
    

3) Now to run any java file we need to first compile it and then run

Compile the Program


 In java we use javac tool to compile the java programs, To compile
Bring up a shell, or "command," window. You can do this from the Start menu by choosing Command Prompt (Windows XP), or by choosing Run... and then entering cmd. The shell window should look similar to the following figure.




a window where you can enter DOS commands
A shell window.

The prompt shows your current directory. When you bring up the prompt, your current directory is usually your home directory for Windows XP (as shown in the preceding figure.
To compile your source file, change your current directory to the directory where your file is located. For example, if your source directory is javaPrograms on the C drive, type the following command at the prompt and press Enter:
cd C:\javaPrograms
 
 
Now you are ready to compile. At the prompt, type the following command and press Enter.
javac HelloWorld.java
 
The compiler has generated a bytecode file, HelloWorld.class. At the prompt, type dir to see the new file that was generated, as shown in the following figure.


 

Run the Program

In the same directory, enter the following command at the prompt:

java HelloWorld
 
The next figure shows what you should now see:






The program prints "Hello World!" to the screen.
Congratulations! Your program works!





Sunday 8 April 2012

Entity Framework with MySql Database in asp.net 4.0

Hi guys, i am back again but this time i have some useful things, forgive me for my last post.

we will learn a good feature of asp.net 4.0 Entity Framework, there are lots of blog for how to connect sql server databases with Entity Framework in asp.net but a very few for MySQL Database. So trust me you are going to thank me for this blog, so today we will create 

 Entity Framework with MySQL Database in visual Studio 2010

step to step guide to connect MySQL database in Entity Framework with snapshots

Pr-requisite

1) You have installed VISUAL STUDIO TEAM EXPLORER EVERYWHERE 2010
if not please install it from here it is free to download.
Note: any visual studio 2010 will do.

2) You have MySQL server installed on your machine.
    if not please install it from here.

Steps to connect MySQL Database with Entity Framework in visual studio 2010.

1) First of all you need to download MySQL Connector Net 6.3.5 if you have it go to step 2
    else download and install it from here, Do not worry it is free to download as well :)
this connector is use to connect MySQL database from asp.ney

2) After installation open visual studio 2010 and create a simple web site project from file > new >web site
 shown below.




3) A pop up will come select asp.net website and click OK you will have a web site ready

4) Right Click on your website and select add new item




5) A pop will come with lots of options if you have visual studio 2010 you will get inbuilt feature to add ADO.Net Entity Data Model, select that and click Add.



6) A warning dialog box will come , just say yes and app_code folder will be created in your solution




7) Now is the main stuff that we are waiting for , if you want to create a Entity framework for existing database that you have already created in MySQL database click Generate from Database option and click next.




8) Now a Pop up will open which will ask for connection details to your MySql database.
    if you are setting up first time a blank drop down will appear, so click on new connection button to provide details about your MySQL database.





9) A POP will open that will have data source as Microsoft Sql server, click on change button next to it,
to change it to MySQL as we want to connect MySQL Database remember he he





10) A pop up will open which will have MySql Database option on left panel select that and click OK



11) After clicking OK it will go to previous Pop up . now you need to provide details about your MySQL database such as
Server Name : machine on which DB is present
User Name : username for that DB
password : Password for DB
Database Name : Database you want to connect

If you are using a development machine and your MySQL database is installed in same machine as Visual studio then you can give server name as local host
fill the details and click test connection if it is succeeded then we are good to go.



12) Now select one of the two options provided in below snapshot and click next to proceed
select yes for now.



13) After clicking on next a pop will open that will show your database tables, views, stored procedures
for mapping of table to Entity model select on tables check-box, you can individually select the tables from your database or if you want all tables then select check-box tables.

Just look at your namespace name as well this will be used afterwards. and click on finish button


14) whoa, you have Entity Model ready for your MySQL database, you can see Model.edmx file inside your app_code folder.just double click on it and you can see your tables in model


15) now you can write all type of code to get this entities for example i have written a simple code to get all entries from my question table in a Generic List type questionList

 protected void Page_Load(object sender, EventArgs e)
    {
        MyDataBaseModel.MyDataBaseEntities  db = new MyDataBaseModel.MyDataBaseEntities();
        List<MyDataBaseModel.question> questionList = db.questions.ToList();
    }


16)This is it now time to play around with it.... will get back soon with some different stuff bye..

Friday 6 April 2012

what is asp.net

Hi Guys, this is my first blog so bear with me as i am not much into blogging.

In today's blog i will try to cover the basics of asp.net, you will ask why asp.net ?
the reason behind this is i had started my first project of life when i was in college with this great framework.
I remember when we guys want to make a site we did not even knew whats a website is then one of my friend told that there is a tool called Visual Studio 2008 in which if you drag and drop things and play you can build website. From that day we started using ASP.NET and till now it is being a great experience.

So now lets come back to the point, what is asp.net ?
i know there are thousands of site which will give you the perfect 8marks answer for this but trust me why i am writing this blog is i could never understand those big words of dictionary.

"ASP.NET is a Web application framework developed and marketed by Microsoft to allow programmers to build dynamic Web sites, Web applications and Web services. It was first released in January 2002 with version 1.0 of the .NET Framework, and is the successor to Microsoft's Active Server Pages (ASP) technology"