0% found this document useful (0 votes)
91 views

Creating Data Access Object (DAO) Design Pattern

The document discusses the Data Access Object (DAO) design pattern. It provides a technique for separating object persistence and data access logic. All basic data access code is contained in the DAO, which provides a simple interface to access data. The DAO pattern requires creating a Connection factory class to get a connection to the database. It then discusses writing database operations in a separate class like FinalResultDAO to insert user answers into the database table. The DAO class can then be used anywhere by making an object of the DAO class.

Uploaded by

Naveen Sriramulu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

Creating Data Access Object (DAO) Design Pattern

The document discusses the Data Access Object (DAO) design pattern. It provides a technique for separating object persistence and data access logic. All basic data access code is contained in the DAO, which provides a simple interface to access data. The DAO pattern requires creating a Connection factory class to get a connection to the database. It then discusses writing database operations in a separate class like FinalResultDAO to insert user answers into the database table. The DAO class can then be used anywhere by making an object of the DAO class.

Uploaded by

Naveen Sriramulu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Creating Data Access Object (DAO) Design Pattern 

Creating Data Access Object (DAO) Design Pattern


Data Access Object is the import component of the design patter of java. It provides a technique to separating the object persistence and data access
logic. All the basic data access code contains in  DAO and it provides a simple interface to access the data.

In DAO you need to create the Connection factory class. This class contains the code for getting connection to the database.

For Example-

public class ConnectionFactory {


        String driverName = "com.mysql.jdbc.Driver";
        String conUrl = "jdbc:mysql://192.168.10.13:3306/onlinexamination";
        String dbUser = "root";
        String dbPwd = "root";

        private static ConnectionFactory connectionFactory = null;

        private ConnectionFactory() {
                try {
                        Class.forName(driverName);
                } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                }
        }

        public Connection getConnection()  {


                Connection conn = null;
                try {
                        conn = DriverManager.getConnection(conUrl, dbUser,
dbPwd);

                } catch (Exception e) {
                        e.toString();
                }
                return conn;
        }

        public static ConnectionFactory getInstance() {


                if (connectionFactory == null) {
                        connectionFactory = new ConnectionFactory();
                }
                return connectionFactory;
        }
}
Then Write the operatios on the on the table in a seperate class

AS
package net.roseindia.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import net.roseindia.model.FinalResultModel;
import net.roseindia.model.PaperResultModel;

public class FinalResultDAO {


        Connection connect;
        PreparedStatement pst;
        ResultSet rs;
        int rightAns = 0, wrongAns = 0;
        int counter = 0, i = 0;

        public Connection getConnection() {


                Connection con;
                con = ConnectionFactory.getInstance().getConnection();
                return con;
        }

        public void selectedAns(PaperResultModel obPaperSubmitModel, String


language) {
                connect = getConnection();
                String ansQuesQuery = "insert into
result_info(username,quesno,selectopt,language) values(?,?,?,?)";
                try {
                        pst = connect.prepareStatement(ansQuesQuery);
                        pst.setString(1, obPaperSubmitModel.getUsername());
                        pst.setString(2, obPaperSubmitModel.getQuesno());
                        pst.setString(3, obPaperSubmitModel.getAnswer());
                        pst.setString(4, obPaperSubmitModel.getLanguage());
                        pst.execute();
                        System.out.println("Paper Successfully inserted in
database.");
                } catch (SQLException e) {
                        e.printStackTrace();
                } finally {
                        try {
                                if (rs != null) {
                                        rs.close();
                                }
                                if (pst != null) {
                                        pst.close();
                                }
                        } catch (Exception e) {
                                e.toString();
                        }
                }
        }
}

After writing the DAO class you can use in anywhere in you application by making an object of DAO class .
For example-
QuestionDAO obQuestionDAO = new QuestionDAO();
int i = obQuestionDAO.addQuestion(obQuestionModel);

You might also like