Breaking News

Thursday 17 April 2014

How to get the AmazonS3 client object in java

It is very important to know and understand the API's provided from AWS.

AWS has provided some API's to connect to it's S3 bucket. We will using this to get the connection to amazon s3.

Below is direct code snippet which will give the created amazon s3 client object.


public AmazonS3 getAWSClient() {
AmazonS3 s3Client = null; 
BasicAWSCredentials awsCredential = new BasicAWSCredentials(
"ACCESS KEY", "SECRET ACCESS KEY");

ClientConfiguration clientConfig = new ClientConfiguration();
clientConfig.setProtocol(Protocol.HTTPS);
s3Client = new AmazonS3Client(awsCredential, clientConfig);
return s3Client;


Read more ...

Sunday 6 April 2014

How to change the project facet Dynamic web module in eclipse

Here is another post on changing the dynamic web module version inside eclipse.
This is very easy to change the version of the facet in eclipse.

The steps to change the version is as follows:

1) Right click on project and select properties



2) Click on Project Facets in properties tab. Inside project facet window there is an option  for check-box selection for Dynamic Web Module version.
  
   a) First Un-check the check-box


   b) Now change the version as per the requirement. Click on apply and Ok.



Now the project facet dynamic web module is changed to required version.
Read more ...

Saturday 5 April 2014

Spring Context Configuration for Database connection

Defining Spring context configuration for database connection

This post helps in understanding the database configuration in spring in the form of bean.
The code snippet for this configuration is as below.

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${driverClassName}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
</bean>

Some of the property values are configured in properties file.

The code snippet for properties file is as below.

jdbc.properties

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/database_name
username=admin
password=admin


Read more ...

Simple JDBC Connection in java

Simple JDBC Connection code in java

This post is to get the simple JDBC connection in java.

 // JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/DataBase_Name";


//  Database credentials
static final String USER = "admin";
static final String PASS = "admin";
public Connection conn = null;

/**
 *   This Method will return the Database Connection
 */

public Connection getDbConnection()
{ 
    try{
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL,USER,PASS);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch(SQLException se){
        }
return conn;
}
Read more ...

Split String depending on delimiter in java

To Split a String with delimiter condition and form an array of string

Considering a scenario where in we have read a line from csv or xls file in the form of string and need to separate the columns in array of string depending on delimiters.


 Below is the code snippet to achieve this problem..

{
...
...
String line = new BufferedReader(new FileReader("your file"));

String[] splittedString = StringSplitToArray(stringLine,"\"");

}

public static String[] StringSplitToArray(String stringToSplit, String delimiter) {
         StringBuffer token = new StringBuffer();
         Vector tokens = new Vector();
         char[] chars = stringToSplit.toCharArray();
         for (int i=0; i  0) {
                     tokens.addElement(token.toString());
                     token.setLength(0);
                     i++;
                 }
             } else {
                 token.append(chars[i]);
             }
         }<br />
         if (token.length() &gt; 0) {
             tokens.addElement(token.toString());
         }
         // convert the vector into an array
         String[] preparedArray = new String[tokens.size()];
         for (int i=0; i &lt; preparedArray.length; i++) {
             preparedArray[i] = (String)tokens.elementAt(i);
         }
         return preparedArray;
}

Above code snippet contains method call to StringSplitToArray where in the method converts the stringline into string array splitting the line depending on the delimiter specified or passed to the method. Delimiter can be comma separator(,) or double code(").
Read more ...

Read CSV File data and skip some rows from CSV file in java

To read the data from csv file need an opencsv.jar which helps in an easy way to read the csv file content which even contains data containing commas as well. Here is a simple example for reading the csv data which contains one of the columns containing comma (,)  in the data.

Our example contains csvdata.csv file data as :


Our aim is to read each row of the csv data and to skip some rows from the file.

Example to Read data from CSV File :

For reading the csv data, opencsv jar is used in the project. The project structure looks like



The project package contains two java files .

1. CsvDataForm.java -> This file contains the form field variables or the columns from the csv file.

2. CsvRead.java -> This file contains the main method which has reading columns from the csv file mapped to CsvDataForm.java

Two file looks like

CsvDataForm.java

package com.scrapillars.csv;

public class CsvDataForm {

private String Department;

private String Employee;

private String Designation;

private String Salary;

private String Address;

private String Degree;

public String getDepartment() {
return Department;
}

public void setDepartment(String department) {
Department = department;
}

public String getEmployee() {
return Employee;
}

public void setEmployee(String employee) {
Employee = employee;
}

public String getDesignation() {
return Designation;
}

public void setDesignation(String designation) {
Designation = designation;
}

public String getSalary() {
return Salary;
}

public void setSalary(String salary) {
Salary = salary;
}

public String getAddress() {
return Address;
}

public void setAddress(String address) {
Address = address;
}

public String getDegree() {
return Degree;
}

public void setDegree(String degree) {
Degree = degree;
}

}


CsvRead.java

package com.scrapillars.csv;

import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.bean.ColumnPositionMappingStrategy;
import au.com.bytecode.opencsv.bean.CsvToBean;
import com.scrapillars.csv.CsvDataForm;

public class CsvRead {

public static void main(String[] args) throws IOException {

CSVReader reader = new CSVReader(new FileReader("E:/csvdata.csv"), ',','\"', 1);

ColumnPositionMappingStrategy cpms = new ColumnPositionMappingStrategy();
cpms.setType(CsvDataForm.class);

String[] columns = new String[] { "Department", "Employee", "Designation", "Salary", "Address","Degree"};
cpms.setColumnMapping(columns);
CsvToBean csvToBean = new CsvToBean();
List csvDataList = csvToBean.parse(cpms, reader);

for (CsvDataForm csvData : csvDataList) {

System.out.println("Department : " + csvData.getDepartment()
+ "\t Employee : " + csvData.getEmployee()
+ "\t Designation : " + csvData.getDesignation()
+ "\t Salary : " + csvData.getSalary()
+ "\t Address : " + csvData.getAddress()
+ "\t Degree : " + csvData.getDegree()
);
}
}
}


Place the csvdata.csv file in any of the drive and provide the path to CSVReader. In our example csvdata.csv file is placed in 'E' drive. Change the drive name accordingly.

The Output of the CsvRead.java after run looks like :

Department : IT         Employee : JOHN        Designation : PROJECT LEAD       Salary : 10000$         Address : Texas, USA          Degree : MASTERS
Department : IT         Employee : PETER      Designation : TEAM LEAD              Salary : 8000$           Address : New York,USA   Degree : MASTERS

CSVReader from opencsv jar contains constructor which takes FileReader, comma separator argument, double code("") separator argument as to remove this separator from the data and an integer argument which tells number of rows to skip from csv file. If the requirement is to skip 2 rows while reading the data, then the value of the argument should be '2'.
Read more ...
Designed By