Wednesday, November 23, 2011

Get Started with JDBC Basic First

If you are looking for working on some pieces of java code and jdbc driver using mysql. Your are on right blog. You just need to configure some of the factor before you start coding.

Tools Needed

1. Eclipse IDE

2. MySQL installed.
  • Leave mysql default port to 3306 and setup username and password which is required in files where we indicate the database connections configurations
  • Create Database called studentdb and create table called employee.
  • In employee table with column id, name and salary insert some data into it.
3. Go to and download mysql-connector-java-5.1.16-bin.jar , jar need to be added into project build path.

Lets Start simple Java

  1. Create Java Project using eclipse IDE. I hope you will understand what is java project, dynamic project and maven projects or ejb project. so simply create java projects.

    JDBC simple java project
2. Create package in src called com.vastik.jdbc and Create javabean (by definitons should have constructor and setter/getter) called Employee.

package com.vastika.jdbc;

public class Employee {

 private int ID;
 private String Name;
 private int Salary;

 public int getID() {
  return ID;

 public void setID(int iD) {
  ID = iD;

 public String getName() {
  return Name;

 public void setName(String name) {
  Name = name;

 public int getSalary() {
  return Salary;

 public void setSalary(int salary) {
  Salary = salary;

 public String toString() {
  return "EmployeeID=" + ID + "\tName=" + Name + "\tSalary=" + Salary;



3. Create DAO layer that actually deal with database connections and CRUD operations i.e.
create,              update, retrieve and delete.

package com.vastika.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class MyDAO {

 private final String SELECT_ALL = "SELECT * FROM employee";

  * Create getConnection Method which will allows in establishing the
  * connection to database prior to any CRUD operations. i.e. Create,
  * Retrieve, Update or Delete.
  * */
 public Connection getConnetion() {

  Connection conn = null;
  String url = "jdbc:mysql://localhost:3306/";
  String db = "studentdb";
  String driver = "com.mysql.jdbc.Driver";
  String username = "root";
  String password = "test";

  try {

    * registering with the driver with local mysql host
   conn = DriverManager.getConnection(url + db, username, password);


  catch (SQLException e1) {

   System.out.println("Not Connected check the try block!!!");

  catch (ClassNotFoundException e2) {


  return conn;


  * This getAllEmployee method will first call the getConnection Method to
  * get connection to DB, and then fetch the data from database and sets this
  * each rows values into each instance of employee object's instance
  * variable. And returns List of employee. 
  * */
 List getAllEmployee() {

  Connection conn = getConnetion();
  List emplList = new ArrayList();

  try {
   Statement stmt = conn.createStatement();
   ResultSet rs = stmt.executeQuery(SELECT_ALL);
   while ( {
    Employee emp = new Employee();
  catch (SQLException e) {
  return emplList;

4. Make sure your mysql is up and running. Now create Driver class that fetch the data from database and display in the console.

package com.vastika.jdbc;

import java.util.List;

public class Drive {

  * @param args
 public static void main(String[] args) {

  System.out.println("Lets connect to database");
  // creating myDAO
  MyDAO listEmp = new MyDAO();
  List list1 = listEmp.getAllEmployee();

  for (Employee emp : list1) {



Hope you will enjoy this basic of all.

Monday, November 21, 2011

GIT Server Side Hooks to prevent binary


There are number of approaches that we want to control the data to be pushed into git server side. for example we want to enforce certain policies that server willnot allow somekind of files push into the server.  we can specify which type files to be resctricted from pushing into git server. Git allows different kinds of server side hooks, most popular are pre-receive, post-receive and update.

Suppose we would like to just push textual files, configurations files and java source files into the server but want to block all the binary being pushed into the git server. There are some client side approach (global gitignore and putting .gitignore into the each project ) but everyone makes mistake.

we can enforce to block such binary into the git server side so that it will block the binary being pushed into git server. suppose i would like to use pre-receive hooks which will be run once push is made from client to server.  At the server side this hooks will check each no. of commits and scan through all the files in the commits and if it finds any binary it will reject the push with some information to the clients.

Here is some examples in bash scripts:

# Auther : Netra Chhetri
 # Please make this file as pre-receive and put into the serverside
 # This pre-receive scripts will checks the binary when user pushed into remove server, it checks each commits and scan the file types and rejects if it contains binary !!
 exec 3<&0
 while read oldrev newrev refname
        if [ "$oldrev" = "0000000000000000000000000000000000000000" ]; then
            for file in $(git diff-tree --name-only --root $newrev)
                    echo "$file"
                    if [[ $file == *.jar ]]||[[ $file == *.war ]]||[[ $file == *.ear ]]||[[ $file == *.class ]]; then
                         echo"Send information to client that push with one of commits contains binary"
            for file in $(git diff --name-only $oldrev..$newrev)
                    echo "$file"
                    if [[ $file == *.jar ]]||[[ $file == *.war ]]||[[ $file == *.ear ]]||[[ $file == *.class ]];then
                    echo"Send information to client that push with one of commits contains binary"
 exec 0<&3
 # --- Finished
 exit $ret