com.rajaram.bookmark.dao.BookmarkDaoImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.rajaram.bookmark.dao.BookmarkDaoImpl.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rajaram.bookmark.dao;

import com.rajaram.bookmark.constants.BookmarkSqlTableConstants;
import com.rajaram.bookmark.jdbc.BookmarkRowMapper;
import com.rajaram.bookmark.model.Bookmark;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * @author Rajaram
 * @since 21-04-2014
 * @version 0.1
 */
public class BookmarkDaoImpl implements BookmarkDao {

    @Autowired
    private DataSource dataSource;
    private String tableName;

    public BookmarkDaoImpl(String tableName) {

        this.tableName = tableName;
    }

    @Override
    public void insertBookmarks(String userName, List<Bookmark> bookmarkList) {

        String sql = "INSERT INTO " + tableName + " (user_name, url, "
                + "url_name, folder_name) VALUES (?, ?, ?, ?)";

        Connection conn = null;
        final int batchSize = 1000;
        int count = 0;

        try {
            conn = dataSource.getConnection();

            PreparedStatement prepareStatement = conn.prepareStatement(sql);

            for (Bookmark bookmark : bookmarkList) {

                prepareStatement.setString(BookmarkSqlTableConstants.userNameIndex, userName);
                prepareStatement.setString(BookmarkSqlTableConstants.urlIndex, bookmark.getUrl());
                prepareStatement.setString(BookmarkSqlTableConstants.urlNameIndex, bookmark.getName());
                prepareStatement.setString(BookmarkSqlTableConstants.folderNameIndex, bookmark.getFolder());

                prepareStatement.addBatch();

                if (++count % batchSize == 0) {
                    prepareStatement.executeBatch();
                }
            }
            prepareStatement.executeBatch(); // insert remaining records                   
            prepareStatement.close();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }
    }

    @Override
    public List<Bookmark> getBookmarks(String userName) {

        String sql = "select * from " + tableName + " where user_name = ?";

        Connection conn = null;
        BookmarkRowMapper rowExtract = new BookmarkRowMapper();
        List<Bookmark> bookmarks = null;

        try {
            conn = dataSource.getConnection();
            PreparedStatement prepareStatement = conn.prepareStatement(sql);
            prepareStatement.setString(1, userName);
            ResultSet rs = prepareStatement.executeQuery();

            bookmarks = rowExtract.mapRow(rs);

            rs.close();
            prepareStatement.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }

        return bookmarks;
    }

}