Tuesday, 22 May 2018

DTO & DAO


DTO is an abbreviation for Data Transfer Object, so it is used to transfer the data between classes and modules of your application. DTO should only contain private fields for your data, getters, setters and constructors. It is not recommended to add business logic methods to such classes, but it is OK to add some util methods.
DAO is an abbreviation for Data Access Object, so it should encapsulate the logic for retrieving, saving and updating data in your data storage (a database, a file-system, whatever). Here is an example how the DAO and DTO interfaces would look like:
interface PersonDTO {
    String getName();
    void setName(String name);
    //.....
}

interface PersonDAO {
    PersonDTO findById(long id);
    void save(PersonDTO person);
    //.....
}
The MVC is a wider pattern. The DTO/DAO would be your model in the MVC pattern. It tells you how to organize the whole application, not just the part responsible for data retrieval.
As for the second question, if you have a small application it is completely OK, however if you want to follow the MVC pattern it would be better to have a separate controller, which would contain the business logic for your frame in a separate class and dispatch messages to this controller from the event handlers. This would separate your business logic from the view.




design_pattern

Saturday, 19 May 2018

Java DB - Connecting to mysql


package database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class JavaDB {

public static void main(String[] args) throws Exception {
createTable();
post();
get();
}
public static ArrayList<String> get() throws Exception{
  try{
  Connection conn=getConnection();
  PreparedStatement st=conn.prepareStatement("select * from employee");
 
  ResultSet rs = st.executeQuery();
 
  ArrayList<String> array = new ArrayList<String>();
  while(rs.next()) {
  System.out.println(rs.getInt("id"));
  System.out.print("");
  System.out.println(rs.getString("name"));
 
  array.add(rs.getString("name"));
 
  }
    return array;
  }
  catch(Exception e) {System.out.println(e);}
  finally {System.out.println("query completed ");}
return null;
}
public static void createTable() throws Exception{
try {
System.out.println("in Create table");
    Connection conn= getConnection();
    System.out.println("created connection");
    PreparedStatement st=conn.prepareStatement("create table IF NOT EXISTS employee(id INT AUTO_INCREMENT PRIMARY KEY,name varchar(20),age INT(10));");
   
    st.executeUpdate();
     
}catch(Exception e) {
System.out.println(e);
}
finally {System.out.println("Function completed ...");}
}
public static void post() throws Exception{
//final  int id= 99;
//final String name="srini";
//final int age=28;
try {
Connection conn= getConnection();
PreparedStatement st=conn.prepareStatement("insert into employee values(3,'sri',25);");
st.executeUpdate();
}catch(Exception e) {System.out.println(e);}
finally {  System.out.println("insertion completed");
}
} 
 
public static Connection getConnection() throws Exception{
    
    try {
    String driver ="com.mysql.jdbc.Driver";
    String url ="jdbc:mysql://localhost:3306/JavaDB";
    String username="root";
    String password="1234";
    Class.forName(driver);
    
    Connection conn= DriverManager.getConnection(url,username,password);
    System.out.println("connected");
    return conn;
    
    }catch(Exception e){
            System.out.println(e);
    }
          
          return null;
  
}

}


============

Thursday, 22 February 2018

method hiding & method overriding

up vote97down voteaccepted



public class Animal {
    public static void foo() {
        System.out.println("Animal");
    }
}

public class Cat extends Animal {
    public static void foo() {  // hides Animal.foo()
        System.out.println("Cat");
    }
}
Here, Cat.foo() is said to hide Animal.foo(). Hiding does not work like overriding, because static methods are not polymorphic. So the following will happen:
Animal.foo(); // prints Animal
Cat.foo(); // prints Cat

Animal a = new Animal();
Animal b = new Cat();
Cat c = new Cat();
Animal d = null;

a.foo(); // should not be done. Prints Animal
b.foo(); // should not be done. Prints Animal because the declared type of b is Animal
c.foo(); // should not be done. Prints Cat because the declared type of c is Cat
d.foo(); // should not be done. Prints Animal because the declared type of b is Animal
Calling static methods on instances rather than classes is a very bad practice, and should never be done.
Compare this with instance methods, which are polymorphic and are thus overridden. The method called depends on the concrete, runtime type of the object:
public class Animal {
    public void foo() {
        System.out.println("Animal");
    }
}

public class Cat extends Animal {
    public void foo() { // overrides Animal.foo()
        System.out.println("Cat");
    }
}
Then the following will happen:
Animal a = new Animal();
Animal b = new Cat();
Animal c = new Cat();
Animal d = null;

a.foo(); // prints Animal
b.foo(); // prints Cat
c.foo(); // prints Cat
d.foo(): // throws NullPointerException
---


First of all What is meant by method Hiding?
Method hiding means subclass has defined a class method with the same signature as a class method in the superclass. In that case the method of superclass is hidden by the subclass. It signifies that : The version of a method that is executed will NOT be determined by the object that is used to invoke it. In fact it will be determined by the type of reference variable used to invoke the method
What is meant by method overriding? 
Method overriding means subclass had defined an instance method with the same signature and return type( including covariant type) as the instance method in superclass. In that case method of superclass is overridden(replaced) by the subclass. It signifies that: The version of method that is executed will be determined by the object that is used to invoke itIt will not be determined by the type of reference variable used to invoke the method.
Why can't static methods be overridden?
Because, static methods are resolved statically (i.e. at compile time) based on the class they are called on and not dynamically as in the case with instance methods which are resolved polymorphically based on the runtime type of the object. 
How should static methods be accessed?
Static methods should be accessed in static way. i.e. by the name of class itself rather than using an instance.



https://stackoverflow.com/questions/16313649/what-is-method-hiding-in-java-even-the-javadoc-explanation-is-confusing