Saturday, 1 September 2018

Creating JDBC Connection in Java

Following are the steps to create JDBC connection in Java.

1. Register driver class.
2. Create connection object.
3. Create statement object.
4. Executing queriesoswd
5. Close connection.

Below are the code :
import java.sql.*;
class MysqlCon{
    public static void main(String args[]){
        try{
            Class.forName("com.mysql.jdbc.Driver");
            Connection con=DriverManager.getConnection(
                           "jdbc:mysql://localhost:3306/test","root","admin");
           //here test is database name, root is username and admin is  password 
            Statement stmt=con.createStatement();
            ResultSet rs=stmt.executeQuery("select * from emp");
            while(rs.next()) {
                System.out.println(rs.getInt(1) + ":" + rs.getString(2) + ":" + rs.getString(3));
            }
            con.close();

        }catch(Exception e){ System.out.println(e);}

    }
}

Tuesday, 28 August 2018

Defining Integer as Octal


Octal Number System :
  • Octal number system is the base 8 number system.
  • Legal digits in an octal literal can be 0 to 7 only.
  • If you try to write int a = 08 or a = 09 in Java, It will give compile time error.
  • Octal literal starts with leading zero, for example 010,023,011 etc.

Below is the Java program If we write int = 010, then we will be the output.

public class OctalNumberClass{

  public static void main(String []args){
     int abc = 010;
     System.out.println("Octal Values :"+abc);
  }
}

Output : Octal Values :8

Tuesday, 31 July 2018

Constructor

       The Constructor is similar to method except constructor must have the same name as class name and it must not return any type. By default, every class have constructor whether we have declared or not. It is called when we create object using new keyword. for ex. A a = new A(). Constructor are used to initialize the objects. There are two types of constructor.
       i. Default Constructor
       ii. Parameterized Constructor.

Example of Default Constructor :

class XYZ{  
XYZ(){
System.out.println("XYZ is created");
}  
public static void main(String args[]){  
XYZ b=new XYZ();  
}  
}

Example of Parameterized Constructor :

class XYZ{  
    int id;  
    String name;  
      
    XYZ(int i,String n){  
    id = i;  
    name = n;  
    }  
    void show(){
System.out.println(id+" "+name);
   }  
   
    public static void main(String args[]){  
    XYZ x1 = new XYZ(1,"Mangesh");  
    XYZ x2 = new XYZ(2,"Harish");  
    x1.show();  
    x2.show();  
   } 
}  

Thursday, 12 May 2016

What is thread in java.

       Every executable program is called as thread. and there are two ways to create a thread :
1. By extending Thread class.
2. By implementing Runnable method.

Example of By extending Thread class :

class ThreadDemo extends Thread{
public void run(){
System.out.println("Thread class extended");
}
public static void main(String[] args ){
ThreadDemo TD=new ThreadDemo ();
TD.start();
}
}

Example of By implementing Runnable method :

class ThreadDemo implements Runnable{
public void run(){
System.out.println("Runnable class Implement");
}
public static void main(String[] args ){
ThreadDemo TD=new ThreadDemo ();
TD.start();
}
}

Which is best thread.

If we extends Thread class then we can not extend another class. And if we implement Runnable interface and in future if we want to extend any class then we can extend if we implement Runnable interface.