Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Monday, January 28, 2019

A Beginners Guide to Java JDBC

Introduction to JDBC in Java

Java Database Connectivity is a standard Java API used to connect Java application with Database. Java JDBC is used to communicate with the different type of Databases like Oracle, MS Access, My SQL and SQL Server. JDBC can also define as the platform-independent interface between a relational database and Java programming. It allows a java program to execute the SQL statement and retrieve the result from the database. JDBC uses drivers to connect with database. 

Different Types of JDBC Driver in Java 

Java JDBC

1. JDBC-ODBC Bridge Driver
2. Natïve Driver
3. Network Protocol Driver
4. Thin Driver

Why should we use JDBC?

There is some critical point which explains why we should use JDBC.

1. JDBC API is Standard API. We can communicate with any Database without
revising our Application, i.e. it is Database Independent API.
2. JDBC Drivers developed in Java, and hence JDBC Concept is applicable for any
Platform. i.e., JDBC is Platform Independent Technology.
3. By using JDBC API, we can perform basic CRUD Operations very efficiently.
C ➔ Create (Insert)
 R ➔ Retrieve (Select)
 U ➔ Update (Update)
 D ➔ Delete (Delete)
4. We can also perform Complex Operations like inner joins, Outer joins, calling
Stored Procedures, etc. very quickly by using JDBC API.
5. JDBC API supported by many vendors and they developed multiple Products
based on JDBC API.

Advantages of JDBC:

1. Provide present enterprise information.
2. Automatically Creates an XML structure of data from the database.
3. No content conversion required.
4. Query and Stored procedure supported.
5. We can use JDBC for both Synchronous and Asynchronous processing.
6. Supports modules.
7. Zero Configuration for Network Computers.
8. Full Access to Metadata.
9. Database Connection Identified by URL.
10. It does not require an installation.

Monday, January 21, 2019

How Java Servlet Works?

Features of Java Servlet

There are 5 Features of Servlet are as Follow:
Portable: Servlet uses Java Programming language, and as we know java is portable language, so servlet is also portable.
Efficient: Servlets invocation is highly skilled as compared to any CGI programs.
Robust: JVM manages Servlet. That’s why Servlet is robust.
Performance: Servlet creates a thread for each request, so the performance of servlet is better than CGI.
Secure: Because it uses java language, so servlet is safe.

How Servlet Works?

Whenever a request comes, it received by the servlet, and then they forward the request to the web container. A Web container is responsible for handling the request by creating the new thread. Container creates multiple threads to execute various requests.

How Java Servlet Works?


Let’s understand the working of Servlet –
1. The User sends a request by clicking a link for a servlet.
2. Then, the container finds the servlet and Create two instances.
  a.HttpServletRequest
  b.HttpServletResponse
3. After this, the container creates a thread for executing the request. So they call the servlet’s service() method and passes the instances as arguments.
4.Based on the HTTPRequest method sent by client, service( ) method decides which servlet method, doGet() or doPost() to call. 
5. Then, to respond to the client, servlet uses response instance to write the response.
6.After the service( ) method is completed, container call the destroy( ) method. And the request and response instance are prepared for garbage collection.

Servlet API

there are 2 packages of java servlet API are as follow:

1.javax.servlet

This package contains several classes and interfaces that describe and define bonds between a class and environment for an object.

Interface: -

ServletResponse
ServletRequest
RequestDispatcher
ServletConfig
Servlet

Class: -

GenericServlet
ServletRequestWrapper
ServletResponseWrapper
ServletInputStream
ServletOutputStream

Exception

ServletException
UnavailableException

2.javax.servlet.http

This package contains several classes and interfaces that describe and define bonds between a servlet class running under HTTP protocol and environment for an object.

Interface:- 

HttpServletRequest
HttpServletResponse
HttpSession
HttpSessionContext
HttpSessionListener

Class: -

Cookie
HttpServlet
HttpServletRequestWrapper
HttpServletResponseWrapper
HttpSessionEvent




Thursday, January 10, 2019

10 Difference between C++ and C# Programming Language

What is the Difference Between C++ and C#?

difference between C++ and C# | C++ vs C sharp




No.
C++
C#

1.
C++ is a low-level language.
C# is a High-level Language.

2.
It supports the multiple inheritances.
It does not support multiple inheritances.

3.
In C++, you require to manage memory manually.
C# automatically manages Memory.

4.
In C++, after compiling code changed into machine code.
In C#, after compiling code is changed into an intermediate language code.

5.
C++ used for developed console applications.
C# programming used for Windows, mobile, and console applications.

6.
for Each loop is not supported in C++.
for Each loop support in C#.

7.
C++ does not support garbage collection.
C# supports garbage collection.

8.
C++ Does not support bound checking of arrays.

C# Support bound checking of arrays.
9.
Using this language can create a standalone application.

Using this language cannot create a standalone application.
10.
In C++, you can use pointer anywhere in the programme.

In C#, Only in Unsafe mode, you can use a pointer.

Sunday, December 30, 2018

Control Statement in PHP with Example

PHP Control Statement

What is Control Statement in PHP?

A block of code which decide the execution way of programme according to the value of the given condition.
PHP Control Statement with Example

There are some control statements that PHP supports.

1. PHP if Statement

If a given condition is true, only then if statement will execute.
Syntax:
if(condition) {
block1 of code;
}

if Statement Example: 
<?php
if ($15 > $10)
echo “15 is greater than 10”;
?>

Output:
15 is greater than 10

2. PHP if…else Statement

In PHP, if a statement is executed only when a specified condition is true. It evaluated to its Boolean values. If the condition is FALSE, it’ll ignore it.
Syntax:
if(condition) {
block1 of code
}else {
block2 of code
}

if - else Statement Example: Find a largest number between two numbers using if else statement;
<?php
$first_value = 5;
$second_value = 10;
if($first_value > $second_value) {
echo “$first_value is greater than $second_value”;
} else {
echo  “$second_value is greater than $first_value”;
}
?>
Output:
10 is greater than 5

3. PHP if….elseif….else Statement 

With this statement, you can select one of several blocks of code to be executed.
Syntax:
if (condition)
first blockto be executed if condition is true;
elseif (condition)
Second block to be executed if condition is true;
else
final block to be executed if condition is false;
if -else if else statement Example:
<?php
$d = date("D");
if($d == "Thu"){
echo "Have a nice weekend!";
} elseif($d == "Sun"){
echo "Have a nice Sunday!";
} else{
echo "Have a nice day!";
}
?>

Output:
Have a nice day!

4. PHP Switch Statement

It is like IF statements. If you want to select one of several blocks of code to be executed, use the Switch statement.
Syntax:
switch ( )
{
case1
break;
case2
break;
default:
default code
break;
}
?>

PHP Switch case Statement Example:
<?php
         $d = date("D");

         switch ($d){
            case "Mon":
               echo "Today is Monday";
               break;

            case "Tue":
               echo "Today is Tuesday";
               break;

            case "Wed":
               echo "Today is Wednesday";
               break;

            case "Thu":
               echo "Today is Thursday";
               break;

            case "Fri":
               echo "Today is Friday";
               break;

            case "Sat":
               echo "Today is Saturday";
               break;

            case "Sun":
               echo "Today is Sunday";
               break;

            default:
               echo “which day is this ?";
         }
      ?>

Output:
Today is Wednesday

Monday, December 17, 2018

An Ultimate Guides to Garbage Collection in Java

What is Garbage Collection in Java?


Java garbage collection is the method by that Java programs accomplishes automatic memory management. Java programs compile to bytecode which will be run on a Java Virtual Machine. Once Java programs run on the JVM, objects are created on the heap, which is a share of memory dedicated to the program. Ultimately, some objects cannot be required. The garbage collector finds these unused objects and deletes them to liberate memory.

Java garbage collection

When there are not any references to an object, it is assumed to be now not required, Also, the memory, obtained by the purpose can be rescued. There is no specific necessity to destroy an object as Java handles the de-allocation by itself.
The technique that accomplishes this is often called Garbage Collection. Programs that don’t de-allocate memory will eventually crash once there’s no memory left within the system to assign. These programs are said to have memory escapes.

Why we perform Garbage Collection in Java?

The purpose of garbage collection is to spot and discard objects that are not any longer required by a program, so their resources are saved and reused. A Java object is a focus to garbage collection once it becomes inaccessible to the program within which it’s used.

How Java Garbage Collection Works?

Many people assume garbage collection collects and discards dead objects. But, in Java garbage collection is doing the differing! Live objects are followed and everything else chosen garbage. As you will see, this basic misunderstanding will result in several performance issues.

Let's begin with the heap that is that the space of memory used for dynamic allocation. In most configurations, the package allocates the heap in before to be managed by the JVM whereas the program is running. 

This includes several important complications:
Object creation is quicker as a result of world synchronization with the package isn’t required for every single object. An allocation claims some area of a memory array and transfers the offset pointer forward. The next assignment starts at this offset and requests the next portion of the array.
When an object is not any longer used, the garbage collector retrieves the underlying memory and recycles it for future object allocation. This implies there is no specific deletion and no memory is given back to the operating system.

All objects are allotted on the heap area managed by the JVM. Every item that the uses by the developer is treated in this manner, organized with class objects, variables, and even the code itself. If an object is being documented, the JVM considers it active. Once an object is no longer documented and therefore is not approachable by the application code, the garbage collector removes it and retrieves the unused memory.

Garbage Collection Example in Java:

public class Example1{   
   public static void main(String args[]){  
        /* Here we are purposely assigning a nullvalue to a reference*/
Example1 obj=new Example1();  
obj=null; 
        /* Here we are purposely assigning reference a to another reference b*/
Example1a = new Example1();
Example1b = new Example1();
b = a;
System.gc();  /* representing garbage collection by calling this*/
   }  
   protected void finalize() throws Throwable
   {
        System.out.println("Garbage collection is performed by JVM");
   }
}
Output:
Garbage collection is performed by JVM
Garbage collection is performed by JVM

In this example, we are representing the garbage collection by calling System.gc(). During this example, we have overridden a finalize() method. This method is invoked directly before an object is destroyed by java garbage collection method. This is often the motive you would see within the output that this technique has been invoked double.