Connectivity with MYSql

Today i come to know about how to connect the java application with mysql.

For connecting java application with the mysql database, you need to follow 5 steps to perform database connectivity.

In this example we are using MySql as the database. So we need to know following informations for the mysql database:

  1. Driver class: The driver class for the mysql database is com.mysql.jdbc.Driver.
  2. Connection URL: The connection URL for the mysql database isjdbc:mysql://localhost:3306/sonoo where jdbc is the API, mysql is the database, localhost is the server name on which mysql is running, we may also use IP address, 3306 is the port number and sonoo is the database name. We may use any database, in such case, you need to replace the sonoo with your database name.
  3. Username: The default username for the mysql database is root.
  4. Password: Password is given by the user at the time of installing the mysql database. In this example, we are going to use root as the password.

Java JDBC

July 1,2016

Today i come to know about the JDBC(Java Database Connection)& the connectivity.

Java JDBC

Java JDBC is a java API to connect and execute query with the database. JDBC API uses jdbc drivers to connect with the database.jdbc

 Use of JDBC:

Before JDBC, ODBC API was the database API to connect and execute query with the  database. But, ODBC API uses ODBC driver which is written in C language (i.e.  platform dependent and unsecured). That is why Java has defined its own API (JDBC API) that uses JDBC drivers (written in Java language).

JDBC Driver

JDBC Driver is a software component that enables java application to interact with the database.There are 4 types of JDBC drivers:

  1. JDBC-ODBC bridge driver
  2. Native-API driver (partially java driver)
  3. Network Protocol driver (fully java driver)
  4. Thin driver (fully java driver)

5 Steps to connect to the database in java

There are 5 steps to connect any java application with the database in java using JDBC. They are as follows:

  • Register the driver class
  • Creating connection
  • Creating statement
  • Executing queries
  • Closing connection

Java Swings

June 30,2016

Today i come to know some more components of java swings.

JTable class :

The JTable class is used to display the data on two dimensional tables of cells.

Commonly used Constructors of JTable class:

  • JTable(): creates a table with empty cells.
  • JTable(Object[][] rows, Object[] columns): creates a table with the specified data.

ProgressBar class:

The JProgressBar class is used to display the progress of the task.

Commonly used Constructors of JProgressBar class:

  • JProgressBar(): is used to create a horizontal progress bar but no string text.
  • JProgressBar(int min, int max): is used to create a horizontal progress bar with the specified minimum and maximum value.
  • JProgressBar(int orient): is used to create a progress bar with the specified orientation, it can be either Vertical or Horizontal by using SwingConstants.VERTICAL and SwingConstants.HORIZONTAL constants.
  • JProgressBar(int orient, int min, int max): is used to create a progress bar with the specified orientation, minimum and maximum value.

 

Java Swings

June 29,2016

Today i come to know about java swings to create window based applications.In java swings i learn about the Jcomponents.

Java Swings

Java Swing is a part of Java Foundation Classes (JFC) that is used to create window-based applications. It is built on the top of AWT (Abstract Windowing Toolkit) API and entirely written in java.

Hierarchy of Java Swing classes

The hierarchy of java swing API is given below.swinghierarchy

JButton class

The JButton class is used to create a button that have plateform-independent implementation.

Commonly used Constructors:

  • JButton(): creates a button with no text and icon.
  • JButton(String s): creates a button with the specified text.
  • JButton(Icon i): creates a button with the specified icon object.

JRadioButton class

The JRadioButton class is used to create a radio button. It is used to choose one option from multiple options. It is widely used in exam systems or quiz.

It should be added in ButtonGroup to select one radio button only.

Commonly used Constructors of JRadioButton class:

  • JRadioButton(): creates an unselected radio button with no text.
  • JRadioButton(String s): creates an unselected radio button with specified text.
  • JRadioButton(String s, boolean selected): creates a radio button with the specified text and selected status.

JComboBox class:

The JComboBox class is used to create the combobox (drop-down list). At a time only one item can be selected from the item list.

Commonly used methods of JComboBox class:

1) public void addItem(Object anObject): is used to add an item to the item list.
2) public void removeItem(Object anObject): is used to delete an item to the item list.
3) public void removeAllItems(): is used to remove all the items from the list.
4) public void setEditable(boolean b): is used to determine whether the JComboBox is editable.
5) public void addActionListener(ActionListener a): is used to add the ActionListener.
6) public void addItemListener(ItemListener i): is used to add the ItemListener.

 

Java Multithreading

June 28,2016

Today i come to know about the multithreading in java and its uses.

Multithreading

Multithreading in java is a process of executing multiple threads simultaneously.

Thread is basically a lightweight sub-process, a smallest unit of processing. Multiprocessing and multithreading, both are used to achieve multitasking.

Java Multithreading is mostly used in games, animation etc.

Advantages of Java Multithreading

1) It doesn’t block the user because threads are independent and you can perform multiple operations at same time.

2) You can perform many operations together so it saves time.

3) Threads are independent so it doesn’t affect other threads if exception occur in a single thread.

Life cycle of a Thread (Thread States)

A thread can be in one of the five states. According to sun, there is only 4 states in thread life cycle in javanew, runnable, non-runnable and terminated. There is no running state.The life cycle of the thread in java is controlled by JVM. The java thread states are as follows:

  1. New
  2. Runnable
  3. Running
  4. Non-Runnable (Blocked)
  5. Terminated

threadstates

 

 

 

Interface in java

June 27,2016

An interface in java is a blueprint of a class. It has static constants and abstract methods only.

The interface in java is a mechanism to achieve fully abstraction. There can be only abstract methods in the java interface not method body. It is used to achieve fully abstraction and multiple inheritance in Java.

Java Interface also represents IS-A relationship.

It cannot be instantiated just like abstract class.

Why use Java interface?

There are mainly three reasons to use interface. They are given below.

  • It is used to achieve fully abstraction.
  • By interface, we can support the functionality of multiple inheritance.
  • It can be used to achieve loose coupling.

Java Polymorphism

June 24,2016

Today i have learnt about the polymorphism and its rules.

Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.

Any Java object that can pass more than one IS-A test is considered to be polymorphic. In Java, all Java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object.

It is important to know that the only possible way to access an object is through a reference variable. A reference variable can be of only one type. Once declared, the type of a reference variable cannot be changed.

Virtual Methods

In this section, I will show you how the behavior of overridden methods in Java allows you to take advantage of polymorphism when designing your classes.

We already have discussed method overriding, where a child class can override a method in its parent. An overridden method is essentially hidden in the parent class, and is not invoked unless the child class uses the super keyword within the overriding method.

Method Overriding in java

June 23,2016

Today i have learnt about the concept of overriding in java and understanding the problem of overriding.

Method Overriding in Java

If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in java.

Usage of Java Method Overriding

  • Method overriding is used to provide specific implementation of a method that is already provided by its super class.
  • Method overriding is used for runtime polymorphism

Rules for Java Method Overriding

  1. method must have same name as in the parent class
  2. method must have same parameter as in the parent class.
  3. must be IS-A relationship (inheritance).

Java Inheritance

June 22,2016

Today i have learnt about java inheritance and how it is used.Also i have come to know the use of ‘this’ and ‘super’ keyword.

Inheritance in Java

Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.Inheritance represents the IS-A relationship, also known as parent-child relationship.

Why use inheritance in java

  • For Method Overriding (so runtime polymorphism can be achieved).
  • For Code Reusability.
    1. class Subclass-name extends Superclass-name
    2. {
    3. //methods and fields
    4. }

    extends Keyword

    extends is the keyword used to inherit the properties of a class. Following is the syntax of extends keyword.

    Syntax

    class Super {
       .....
       .....
    }
    class Sub extends Super {
       .....
       .....
    }
    
    

    Types of inheritance in java

    On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.typesofinheritance

    The super keyword

    The super keyword is similar to this keyword. Following are the scenarios where the super keyword is used.

    • It is used to differentiate the members of superclass from the members of subclass, if they have same names.
    • It is used to invoke the superclass constructor from subclass.

    Usage of java this keyword

    Here is given the 6 usage of java this keyword.

    1. this keyword can be used to refer current class instance variable.
    2. this() can be used to invoke current class constructor.
    3. this keyword can be used to invoke current class method (implicitly)
    4. this can be passed as an argument in the method call.
    5. this can be passed as argument in the constructor call.
    6. this keyword can also be used to return the current class instance.

 

 

Java-Files & I/O and Exception

June 21,2016

Today i have learnt about the input-output stream and its use. Also i have learn about the exceptions i java and he to handle the exceptions.

Stream

A stream can be defined as a sequence of data. There are two kinds of Streams −

  • InPutStream − The InputStream is used to read data from a source.
  • OutPutStream − The OutputStream is used for writing data to a destination.

    streams

Reading and Writing Files

As described earlier, a stream can be defined as a sequence of data. The InputStream is used to read data from a source and the OutputStream is used for writing data to a destination.

Here is a hierarchy of classes to deal with Input and Output streams.

file_io

FileInputStream

This stream is used for reading data from the files. Objects can be created using the keyword new and there are several types of constructors available.

File f = new File("C:/java/hello");
InputStream f = new FileInputStream(f);

FileOutputStream

FileOutputStream is used to create a file and write data into it. The stream would create a file, if it doesn’t already exist, before opening it for output.

File f = new File(“C:/java/hello”); OutputStream f = new FileOutputStream(f);

Exception Handling

An exception (or exceptional event) is a problem that arises during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally, which is not recommended, therefore, these exceptions are to be handled.

An exception can occur for many different reasons. Following are some scenarios where an exception occurs.

  • A user has entered an invalid data.
  • A file that needs to be opened cannot be found.
  • A network connection has been lost in the middle of communications or the JVM has run out of memory.

Exception Hierarchy

All exception classes are subtypes of the java.lang.Exception class. The exception class is a subclass of the Throwable class. Other than the exception class there is another subclass called Error which is derived from the Throwable class.

The Exception class has two main subclasses: IOException class and RuntimeException Class.

exceptions1