Friday, 31 March 2017

STRUTS 1.X + FILE IMAGE UPLOAD DOWNLOAD + DB

https://github.com/namitsharma99/strutsStartup


SERVLET + JSP + DATABASE + IMAGE UPLOAD AND DOWNLOAD

Hello Buddies.

This is a commonly found task for most of the java developers, to provide a UI for uploading and downloading files. We have taken a step further and pushed the uploaded files into DB.

See the beauty of BLOB/ CLOB here.

The full code is available here -

https://github.com/namitsharma99/uploadFileServletDb

And description will be approaching soon.

Happy Coding!!

Monday, 27 March 2017

Epic Hustle - another one (a new compilation)





Enjoy Buddies & Happy Coding !

Write an excel file using java code

Hello Buddies.

Trying to write an excel file using java code this time.

Full code is available here -

https://github.com/namitsharma99/fileHandlingUsingJava/tree/master/src/com/java/code


Here it goes -



package com.java.code;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class WriteExcelFile {

public static void main(String[] args) {

List<String> employees = new ArrayList<String>();
employees.add("1,Namit,Engineer");
employees.add("2,Sharma,Developer");

try {
writeExcel(employees);
} catch (Exception e) {
e.printStackTrace();
}
/*
* Output will be -
*
* 1 Namit Engineer
* 2 Sharma Developer
*
* */

}

private static void writeExcel(List<String> employees) throws IOException {

/*
* the thumb-rule for any excel operation is work-book > work-sheet >
* row > cell
*/

FileOutputStream fileOutputStream = new FileOutputStream(
"/home/namit/Downloads/writtenExcel.xls");

HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
HSSFSheet hssfSheet = hssfWorkbook.createSheet();

for (int i = 0; i < employees.size(); i++) {
String row1 = employees.get(i);
String[] row1Cells = row1.split(",");

HSSFRow hssfRow = hssfSheet.createRow(i);

for (int j = 0; j < row1Cells.length; j++) {
HSSFCell hssfCell = hssfRow.createCell(j);
hssfCell.setCellValue(row1Cells[j]);
}

}

hssfWorkbook.write(fileOutputStream);
fileOutputStream.close();
hssfWorkbook.close();

}

}






Refer to the comments in the code for some tid-bits, and drop a note for any suggestion or query.

Happy Coding !!

Writing a text file using java code

Hello Buddies.

Trying to write a text file from a java class this time.

Full code is available here -

https://github.com/namitsharma99/fileHandlingUsingJava/tree/master/src/com/java/code


Here it goes -

package com.java.code;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class WriteTextFile {

public static void main(String[] args) {

String storeFileAt = "/home/namit/Downloads/newTextFile.txt";
try {
writeFile(storeFileAt);
} catch (IOException e) {
e.printStackTrace();
}

// output in the created file
// Hello, this is an example for java coding.
// Please refer to thecodebuddy.blogspot.in for more!
}

private static void writeFile(String storeFileAt) throws IOException {

FileOutputStream fileOutputStream = null;
File file = new File(storeFileAt);
fileOutputStream = new FileOutputStream(file);

String myContent = "Hello, this is an example for java coding. \n Please refer to thecodebuddy.blogspot.in for more!";

/*
* to avoid java.io.FileNotFoundException:
* /home/namit/writtenExamples/newTextFile.txt (No such file or
* directory), following check is used
*/
if (!file.exists())
file.createNewFile();

fileOutputStream.write(myContent.getBytes());

fileOutputStream.close();

}

}




Follow the comments in the code for a better understanding, and do try it out.

Your suggestions and try-outs are welcome.


Happy Coding!!

Saturday, 25 March 2017

JAVASCRIPT SAMPLES - A SMALL SERIES FOR HANDS ON IN UI SCRIPTS

Hello Buddies.

I have seen many instances where being a developer, who has tasted the front end some or the other time, you are asked if you have created something javascript or any other JS library.

If you have, great, it gives you an edge. But what if you feel like you haven't spent quite a sometime on the UI alone, but were always going with the application all together, at your workplace.



To deal with such scenarios, I thought why not we should have some basic javascript functions that on the one side can be fun to create like games, and on the other side can showcase your effort spent on the same.

So combining few of such interesting samples, that will give you a flavour of maths, logical reasoning and of course UI programming.

You can refer to these 4 posts for this moment and can come back later with better versions or suggestion for our buddies, or to see if the post has been updated, if that interests you ;)

Moving on, here are the 4 add ons to your profile -

1. http://thecodebuddy.blogspot.in/2017/03/javascript-samples-tic-tac-toe-game.html
    Click here

2. http://thecodebuddy.blogspot.in/2017/03/javascript-samples-hungry-snake.html
    Click here

3. http://thecodebuddy.blogspot.in/2017/03/javascript-samples-shooting-game.html
    Click here

4. http://thecodebuddy.blogspot.in/2017/03/javascript-samples-racing-cars-simple.html
    Click here

These posts also have got the links to their respective git repositories. Feel free to explore and discover.

Happy Coding!!

JAVASCRIPT SAMPLES - TIC TAC TOE GAME & DYNAMIC TABLE CREATION IN JSP

Hello Buddies.

It's time to play tic-tac-toe, but this time, not with pen and paper. 
We will create our own page that allows us to play tic-tac-toe and determines the winner.

You can get the complete code here -

https://github.com/namitsharma99/game-tic-tac-toe






So what should we start with?

Here we go -



<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> < !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > < html > < head > < meta http - equiv = "Content-Type" content = "text/html; charset=UTF-8" > <!-- Latest compiled and minified CSS --> < link rel = "stylesheet" href = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" > <!-- jQuery library --> < script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" > < /script> <!-- Latest compiled JavaScript --> < script src = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" > < /script> < link rel = "stylesheet" href = "./stylish.css" / > < title > GaMe - BeGiN < /title> < script type = "text/javascript" > function createTable() { document.getElementById("rules") .setAttribute("style", "display: block"); document.getElementById("createT").setAttribute("style", "display: none"); document.getElementById("destroyT").setAttribute("style", "display: block"); var theTable = document.getElementById("myDivTable"); var createdTable = document.createElement("table"); createdTable.setAttribute("class", "whatATable"); createdTable.setAttribute("border", "1"); var tabBody = document.createElement("tbody"); createdTable.appendChild(tabBody); for (var i = 0; i < 3; i++) { var myRow = document.createElement("tr"); tabBody.appendChild(myRow); for (var j = 0; j < 3; j++) { var myCol = document.createElement("td"); myCol.setAttribute("class", "whatACell"); // var myText = document.createTextNode("Cell "+ i + "," + j); // as alternative var myButton = document.createElement("input"); myButton.setAttribute("id", i + "," + j); // myButton.setAttribute("value","Cell "+ i + "," + j); // myCol.appendChild(myText); // as alternative myCol.appendChild(myButton); // myCol.setAttribute("id", i + "," + j); myRow.appendChild(myCol); } } theTable.appendChild(createdTable); } function evaluate() { // alert('hello!'); var a00 = document.getElementById("0,0").value; var a01 = document.getElementById("0,1").value; var a02 = document.getElementById("0,2").value; var a10 = document.getElementById("1,0").value; var a11 = document.getElementById("1,1").value; var a12 = document.getElementById("1,2").value; var a20 = document.getElementById("2,0").value; var a21 = document.getElementById("2,1").value; var a22 = document.getElementById("2,2").value; if ((a00 != '' && a01 != '' && a02 != '' && (a00 == a01 && a01 == a02)) || (a10 != '' && a11 != '' && a12 != '' && (a10 == a11 && a11 == a12)) || (a20 != '' && a21 != '' && a22 != '' && (a20 == a21 && a21 == a22)) || (a00 != '' && a10 != '' && a20 != '' && (a00 == a10 && a10 == a20)) || (a01 != '' && a11 != '' && a21 != '' && (a01 == a11 && a11 == a21)) || (a02 != '' && a12 != '' && a22 != '' && (a02 == a12 && a12 == a22)) || (a00 != '' && a11 != '' && a22 != '' && (a00 == a11 && a11 == a22)) || (a02 != '' && a11 != '' && a20 != '' && (a02 == a11 && a11 == a20)) ) { alert('We have a winner!'); } } $(document).keyup(function (event) { // alert('Handler for .keypress() called. - ' + event.charCode); evaluate(); }); function destroyTable() { location.reload(); } < /script> < /head> < body class = "extraContainer" > < div class = "container" align = "center" > < hr > < button onclick = "createTable()" id = "createT" class = "btn btn-success" > Create Table < /button> < hr > < div id = "rules" style = "display: none" > < h3 > Only X or 0 allowed! < /h3> < /div> < hr > < div id = "myDivTable" > < /div> < hr > < button onclick = "destroyTable()" id = "destroyT" style = "display: none" class = "btn btn-danger" > Destroy Table < /button> < hr > < /div> < /body> < /html>


We have 2 beautiful snippets here, if you could just observe the jsp code.

1. First of all, createTable(). It shows you how to create a table in the jsp dynamically.
   (Sounds useful isn't it. You can use it elsewhere as well)

2. Then comes the evaluate() method, that will evaluate if any valid triplet is created or not with every keypress.

Short and simple, and rest upto you.

I don't say it's the most efficient way, but surely it is an easy way, that along with its implementation will make you comfortable with javascript methods.

Do share you versions of the game or tips to do better coding for the buddies.

Happy Coding!!

JAVASCRIPT SAMPLES - HUNGRY SNAKE

Hello Buddies.

Trying to make a simple snake like game using basic javascript and jquery this time.

The complete code is available here -


https://github.com/namitsharma99/hungrySnake





Here we go -

Logic we need to maintain while coding -

1. Arrow keys will redirect the snake into desired direction, and snake keeps on moving on its own.
2. As soon as our snake will eat up the food, that food will be removed from the screen, and a new food will be showed somewhere on the screen, using random function.
3. This will give a score up.
4. But as soon as we hit the wall, in any of the 4 directions, game gets over.
5. Reset button will reset the game.






Key-points to consider in the code -

1. Arrow keys direction -

switch (event.keyCode) { case 27: reset(); break; case 37: $("#snake0").stop() moveLeft(); break; case 39: $("#snake0").stop() moveRight(); break; case 38: $("#snake0").stop() moveUp(); break; case 40: $("#snake0").stop() moveDown(); break; }



2. Food generation and swallowing methods as follow -

function generateFood() { var max = 380; var min = 0; var randomPosX = Math.random() * (max - min) + min; var randomPosY = Math.random() * (max - min) + min; $("#food0").css({ top : randomPosY, left : randomPosX, position : 'absolute' }); }
function foodSwallowed() { var snakePos = $("#snake0"); var positionOfSnake = snakePos.position(); var leftOfSnake = positionOfSnake.left; var topOfSnake = positionOfSnake.top; var foodPos = $("#food0"); var positionOfFood = foodPos.position(); var leftOfFood = positionOfFood.left; var topOfFood = positionOfFood.top; if (Math.abs(leftOfFood - leftOfSnake) < 20 && Math.abs(topOfFood - topOfSnake) < 20) { score += 100; $("#scoreDisplay").val(score); generateFood(); } }

So, once you get the code flow, your snake is ready to flow :P

Try something better and share in the comment section for the buddies.

Happy Coding!

Featured post

JAVA based project, that can be used to hit DB using JDBC, from WSO2 ESB

Hi Buddies, Here is a small project that will enable you to hit MySQL DB using WSO2 ESB - https://github.com/namitsharma99/customM...