Wednesday, February 19, 2014

MySql & Java: Getting Started From Scratch - 5 Simple Steps

This is quick starter guide to get started with MySQL and Java in 5 simple steps. The guide explains the steps required right from the scratch in Windows platform. It is assumed that the reader is aware of MySQL database and Java programming Language. If not familiar with these, its recommended to learn them first.

1. MySql Database Installation

Download the MySQL software from http://dev.mysql.com/downloads/mysql/ Download the right installer EXE(msi file) based on your computer architecture by clicking on download button. An image is shown below for your reference.







You need to create an Oracle account and log in into that to download this msi. Once the msi gets downloaded, just double click on the exe which guides you through further installation.

2. Install the Driver Called Connector/J

This  is required to connect to the mysql database using Java code. The same can be downloaded from http://dev.mysql.com/downloads/connector/j the way you download mysql msi. Double click to install the same.

3. Create sample database and a User

3.1: Open the MySQL Command Line Client. When you install mysql, a short cut gets created which should b
e available in Windows->All Programs->MySQL->MySQL Server 5.5 (Windows 7, MySql v5.5). If you can not find this under windows programs, you can start using the following command from command prompt.

"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysql.exe" "--defaults-file=C:\Program Files\MySQL\MySQL Server 5.5\my.ini"


3.2: The command line program asks for the password, supply the password that you had given while installing MySQL msi. 

3.3 Now create a sample database and change the database using MySQL command line client to the newly created one as shown below.

Enter password: *********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database mydatabase;

Query OK, 1 row affected (0.19 sec)

mysql> use mydatabase;
Database changed


3.4: Create a Database User and Grant privileges using command line client as shown below.

mysql> create user user1 identified by 'userpassword';                     
Query OK, 0 rows affected (1.28 sec)        

mysql> grant usage on *.* to user1@localhost identified by 'userpassword';
Query OK, 0 rows affected (0.01 sec)

mysql>
mysql> grant all privileges on mydatabase.* to user1@localhost;
Query OK, 0 rows affected (0.02 sec)

Now, the database is ready, moving on to Java part where we write a Java program to connect this database called mydatabase using user 'user1' and password 'userpassword'

4. Write a Java Program To Connect to Database & Create a table

4.1: To write Java program, you require an editor for example, Eclipse. Once the Java project is created, you need to place the driver Jar you had downloaded in step2 into Java project CLASSPATH. When you install Connector/J EXE, the jar gets stored under Drive:\Program Files\MySQL\MySQL Connector J\mysql-connector-java-5.1.29-bin.jar. Pace this jar into project's class path.

4.2: Create a simple Java project and create a class called MySqlConnect as shown below. 

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MySqlConnect {
       private Connection con = null;
       private Statement statement = null;
       private PreparedStatement preparedStatement = null;
       private ResultSet rs = null;
       public void connectToSql(){
              try{
                     //Load the driver
                     Class.forName("com.mysql.jdbc.Driver");
                     con =     DriverManager.getConnection
("jdbc:mysql://localhost/mydatabase","user1", "userpassword");
                     statement = con.createStatement();
                     DatabaseMetaData dbmd = con.getMetaData();
                     rs = dbmd.getTables(null, null, "EMPLOYEE", null);
                     rs.next();
                     boolean tableExists = (rs.getRow() == 1);
                     if(!tableExists){
                           String sql = "CREATE TABLE EMPLOYEE " +
                                         "(empid INTEGER not NULL, " +
                                         " firstname VARCHAR(255), " +
                                         " lastname VARCHAR(255), " +
                                         " cubicle VARCHAR(255), " +
                                         " PRIMARY KEY ( empid ))";

                           statement.executeUpdate(sql);
                     }
                     //Place a record into EMPLOYEE table
                     preparedStatement = con
                                  .prepareStatement("insert into  mydatabase.EMPLOYEE values (?, ?, ?, ?)");
                     preparedStatement.setInt(1, 100);
                     preparedStatement.setString(2, "John");
                     preparedStatement.setString(3, "Nelson");
                     preparedStatement.setString(4, "Cube1");
                     preparedStatement.executeUpdate();
              }catch(SQLException e){
                     //handle exception
                     e.printStackTrace();
              }catch(Exception e){
                     //nandle exception
                     e.printStackTrace();
              }finally {
                     try {
                           if (statement != null) {
                                  statement.close();
                           }
                           if (con != null) {
                                  con.close();
                           }
                     } catch (Exception e) {

                     }
              }
       }
       public static void main(String[] args){
              MySqlConnect mySqlConnect = new MySqlConnect();
              mySqlConnect.connectToSql();
       }
}


Note1:  If there are two packages to import from a) java.sql.*  b) com.mysql.jdbc.*.  The difference is that the (b) is specific to MySql database and (a) is generic one. We can (a) for any databases such as Oracle DB, MySQL, DB2 etc.

Note2: Change the primary key 'empid' to be auto_increment and give a default value so that mysql can increment by itself whenever new records are inserted.

5. Verify if the database table and record are created in MySql DB

Now, you've just created a table called EMPLOYEE under database 'mydatabase'. Open the MySql Command Line Client, issue following commands to verify if the Java program is doing the job or not.








We are done. The table that we created and record that we inserted using Java program are available in MySQL database!

To learn how to connect to MySQL database using Hibernate Java Framework, see next post.

Monday, February 17, 2014

How to make scrum meetings more effective/productive in Agile Methodology?

In Agile method of software development, the teams along with scrum master meet once in a day to understand what each team member is working on and are there any impediments. This meeting is called Scrum meeting/daily scrum meeting/daily stand up meeting or even morning meeting. It may not be appropriate to call this meeting as morning meeting since the teams meet a the time that all scrum team members are comfortable with. Its a mandatory meeting for all the team members. Some may join over the call, its totally accepted but need to attend this meeting. The meeting usually happens in front of the scrum or kanban board.

As an agile standard, the scrum meeting lasts for 15 minutes and may slightly vary based on the size of the scrum team. However the meeting is time boxed for defined time. This needs to be ensured by the scrum master. So, what important things can be discussed in a such a short time so that its useful for all the team members? well, there is a fixed agenda set by agile framework. Each team member needs to answer following 3 questions.

1. What user story I was working on yesterday?
2. What user story I'll be working on today?
3. Are there any road blocks/impediments doing the task?

Over the time, this may look boring for the teams, however, the teams need to understand the importance of this and may go little beyond this agenda to make it effective. Following are some benefits to get out of scrum meetings.

  • Lets say a user story A is dependent on story B and both stories A & B needs completion because they both have higher business value. In the scrum meeting, when the member working story A says, he/she has completed it, then story B can be planned on the same day by another person.
  • When a story is completed, the best person to verify the story can be decided. There are two states of the story before we claim a story is completed, 'resolved' state and 'verified' state. Usually the person who resolves the story will NOT be verifying it, but some other team members. Unit level testing is done by the person resolving the story but the other person verifies the story from broader perspective which includes thinking about the impacted areas if any. This is best practice if story is dealing with critical code change in a complex software. 
  • When a team member says that there is an impediment, its escalated to appropriate personal to get the resolution so that the sprint goals are not affected.
  • Some team members may be facing technical difficulties which they just mention at scrum meeting. "I've a difficulty doing this user story and I want to discuss it offline with concerned people" and the details of the issue are not discussed in this meeting. Offline, other team members can talk to the person facing the difficulty and try to help/resolve that. A way towards moving self organized teams.
  • There can be scenario where in particular story requires help from another cross functional team and scrum master talks to team members and discuss those during scrum of scrum meetings where scrum masters of different teams meet together for team dependencies. This may rarely happen even though you've identified the external dependency during PSI planning.

Scrum meetings bring transparency on what's being done/what user stories are being worked on and gives a glimpse on how the sprint is going on to the scrum master. Several other discussions among the team members may happen throughout the day informally may be over the coffee though.

Sunday, February 16, 2014

Finish First To Start Next - Applying This Rule To Agile?

Do we do multi-tasking in our daily work? Or are we just good at that? Many do including me. We always do juggling over many things. This is because we are surrounded by several tasks to perform in a day. This may be true for all, developers, project managers, engineering managers. The next question is are we productive doing this? The research says that we'll not be productive at the end of the day. Juggling over too many things leads no where. I liked the post by Tony Schwartz on Magic Of Doing One Thing At a Time which is worth reading.

In the context of agile development methodology, I thought of applying this rule. This is because, I saw frustrations of developers who execute user stories planned for a sprint, that they were not able concentrate on the planned stories. Because they were pulled by several other 'unplanned' tasks such as a critical customer issues. Well, while agile encourages to take the critical (high business value) tasks first, this is not a good practice from developers' perspective once the Sprint has started. Context switching has its own role to play. And people may not like too much context switching in a day. This is exactly the problem we faced. This typically happens in a product development team where in the product is already into customer site and product enhancements/features are being developed.

Field Issues pop up once the Sprint has started

Lets say, you are working on an important user story which requires your complete attention and concentration and you have started working on it already. And when your scrum master comes to you and says, "Hey, there is a customer issue reported from the field, would you take up that on high priority?" The answer from most of the developers will be "Yes", because nothing gets important than a customer issue. However, your mind has to switch between your current user story to the customer issue. And when you come back for the 'paused' user story, you require some time to put yourself into that task. You end up not achieving the sprint goals. So, is there anything that we can do to avoid context switching? Definitely yes.

Then we made a change in the process of how we execute the sprint stories. Knowing the fact that we are expecting customer issues, have dedicated resources to solely look into those. It does not matter whether there are customers issues reported or not. Sometimes yes and sometimes no. But planning this during Sprint planning makes life a lot better. And the dedicated resources will mentally be prepared to take up the field issues.

No User Story Getting Moved to 'Verified'

Finish the user stories that under development or under verification first before pulling the next user story from to-do list. During certain sprints, many stories can be under development and no stories moved to 'verified' state. This happens due to several reasons such as there is an impediment doing a particular story. Or the resource assigned does not have sufficient competency to complete the story. And the resource stops that story and moves on to the next. It is agreed that this kind of issues must be thought through upfront during sprint planning, however in case not enough grooming of the stories has happened, these issues may be uncovered during execution. In this kind of issues are brought up during daily scrum meetings and the entire team can come into rescue to finish.  A quick change (upon everybody's agreement) will solve this problem. A different person who understands the technical depth involved in the story takes the task and complete it. This is ensured by scrum masters since scrum masters are expected to know the scrum teams better or a team's collective decision for quick change in the day's plan.

These examples can be specific certain teams only and may not apply for all agile teams, but in case teams are facing the same problems, I think I've tried to propose resolutions.
Related Posts Plugin for WordPress, Blogger...