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;
  
}

}


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