com.att.pirates.util.DBUtility.java Source code

Java tutorial

Introduction

Here is the source code for com.att.pirates.util.DBUtility.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.att.pirates.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author mercychan
 */
public class DBUtility {

    public static Connection getDBConnection() throws SQLException, Exception {
        Connection conn = null;
        PropertyReader reader = new PropertyReader();
        String forName = reader.readPropertyByName(PiratesConstants.CLASSFORNAME);
        if (StringUtils.isBlank(forName) || StringUtils.isEmpty(forName)) {
            throw new Exception("forName is empty from property file");
        }

        String serverName = reader.readPropertyByName(PiratesConstants.SERVERNAME);
        String databaseName = reader.readPropertyByName(PiratesConstants.DATABASENAME);
        String user = reader.readPropertyByName(PiratesConstants.USER);
        String password = reader.readPropertyByName(PiratesConstants.PASSWORD);

        if (StringUtils.isBlank(serverName) || StringUtils.isEmpty(databaseName) || StringUtils.isBlank(user)
                || StringUtils.isEmpty(password)) {
            throw new Exception("connection string is empty from property file");
        }

        String url = serverName + ";" + "databaseName=" + databaseName;
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        conn = DriverManager.getConnection(url, user, password);
        return conn;
    }
}