Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Saturday

JSP connection with MYSQL in Linux

0 comments
Now Let us  see how to connect jsp (Java Server Page) with Mysql in Linux - Ubuntu, mint, debian,. Java server page is a technology used to create dynamic web pages & server-side scripting language. It is similar to php but uses java programming language.

To run a jsp we need a web server,
   1.  Install  tomcat server in your system :
            by giving the following command in terminal

                          sudo apt-get install tomcat7      
          You can install a new version(version-8 ) of tomcat by specifying the version number in the  command.
 
          To test whether tomcat installed properly
         Open your browser & type the following URL:

                             http://localhost:8080/

             You will get the following result


  
  Install JDBC Driver :
                  To make a connection between JSP & Mysql, install a JDBC(connector/J) driver .
                     
                          Download mysql-connector-java-version-bin.zip
                                         link to Download
                          Extract the zip and get the mysql-connector-java-5.1.13-bin.jar file
                          Copy the .jar file to  the following location
                     
                                     filesystem/usr/share/tomcat6/lib
                                                       i.e
                                           /usr/share/tomcat6/lib
                       
      Restart the tomcat server :
              
                           sudo /etc/init.d/tomcat7 start
                                          sudo /etc/init.d/tomcat7 stop

    Apache tomcat server can be restarted by using following single cmd also.
                                          sudo /etc/init.d/tomcat6 restart 


         Now Let us create a Sample JSP with Mysql connection.  
                   
                  test.jsp

<!DOCTYPE HTML>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<html>

Sunday

Constructor & Methods in Java with example

0 comments


Constructor :

  •  It has the same name as class in which it resides.
  • A Constructor initializes an object upon creation .
  •  Constructor doesn't have return type even void ,since it return the whole class .
  • Automatic intialization is performed through the use of constructor , so we prefer constructor than method.

ex:

  class addc {
  public addc(int a,int b )
     {
   int c= a+b;
   System.out.println(c);
        }
      }
   class mdemo {
   public static void main(String[ ]  args)
        {
     addc  a1 = new addc(12,8);                        
            }
        }

Method :
         This is the general form of a method
             
           type name (parameter-list) {
                   //body of method
                        }

           ex:

           class addc {
          public int addc(int a,int b )
                {
             int c= a+b;
             return c;                                      // Method can have return type
                  }
              }
         class mdemo {
         public static void main(String[ ]  args)
                 {
         addc  a1 = new addc( );  
          int d = a1.addc(12,8);                 // Getting the result throw by return
           System.out.println(d);                  
              }
           }

Here, type specifies the type of data returned by the method .if the method does not return a value, its return type must be void.



__________________________________________________________________________

Thursday

Simple Program for Prime Number

0 comments
simple program to print prime number in java & c++/c#.

Simple java Program to print prime numbers 

  To find Prime Number or Not  in java

public class primenum {
public void pri()  {
int num = 1 ;
int i;
int count=0;
while(num<100)
{
for(i=1;i<=num;i++)   {
if(num%i==0)  {
count++;
}}
if(count==2){
System.out.println("prime"+num);
count=0;
num++;
}
else {
count = 0;
num++;
}
}}
public static void main(){
primenum p1 = new primenum;
p1.pri();
}
}

   This program print 1st 100 prime numbers


      The Prime Number has no divisor other than 1 & itself .From the following  code when count becomes 2 (i.e count=2 ) it is a prime number i.e 'if(num%i==0)'  condition is satisfied two times, when the number is divided by 1 & itself.
         
              if(num%i==0)  {
              count++;
                  }
Output:
     2
     3
     5
     7
     11 .......
   

To run this code :
     1. Save the code as 'primenum.java'
     2. Compile the primenum.java using below command.
             javac primenum.java
     3. Run the code
             java primenum

Above method is to run the code in the command prompt or terminal. You can also run it in IDE.

Sunday

Hello world Program in Java

0 comments
    Java is a object-oriented programming language whereas c++ is structural and partial
object-oriented programming language . My first program in java. 
           The syntax of Java is 
           

                    public class classname {            
                   public static void main (String args[]) {                  
                             ur statement ;                      
                          }                
                    }

     As Java is case sensitive language you should be carefully in upper case & lower case letters
             simple program in java
                           

                     public class sample {                    
                     public static void main (String args[]) {                            
                         System.out.println("Hello world");                  
                               }                  
                    }
         Save the above code as sample.java                                                   
         To execute :
           1.    javac sample.java   
           2.    javsample