Wednesday, April 25, 2012

Create MDB (Access Database File) using Java

Required Jar (Click to Download)

Steps
  • Add all Jar file to your java project
  • use folllowing code
package net.yogesh.test;

import com.healthmarketscience.jackcess.ColumnBuilder;
import com.healthmarketscience.jackcess.Database;
import com.healthmarketscience.jackcess.Table;
import com.healthmarketscience.jackcess.TableBuilder;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Types;

/**
 *
 * @author yogesh.mpower
 */
public class java2mdb {

    private static Database createDatabase(String databaseName) throws IOException {
        return Database.create(new File(databaseName));
    }

    private static TableBuilder createTable(String tableName) {
        return new TableBuilder(tableName);
    }

    public static void addColumn(Database database, TableBuilder tableName, String columnName, Types sqlType) throws SQLException, IOException {
        tableName.addColumn(new ColumnBuilder(columnName).setSQLType(Types.INTEGER).toColumn()).toTable(database);
    }

    public static void createDB() throws IOException, SQLException {
        String databaseName = "D:/employeedb.mdb"; // Creating an MS Access database
        Database database = createDatabase(databaseName);

        String tableName = "EmployeeDetail"; // Creating table
        Table table = createTable(tableName)
                .addColumn(new ColumnBuilder("Emp_Id").setSQLType(Types.INTEGER).toColumn())
                .addColumn(new ColumnBuilder("Emp_Name").setSQLType(Types.VARCHAR).toColumn())
                .toTable(database);

        table.addRow(1, "yogesh");//Inserting values into the table
        table.addRow(2, "abc");
        table.addRow(3, "pqr");
    }

    public static void main(String[] args) throws IOException, SQLException {
        java2mdb.createDB();
    }
}
  

Output 

file created at path D:/employeedb.mdb
with data

 






 

Sunday, April 22, 2012

Create Pascal's Triangle in One Loop using Java.

Create Pascal's Triangle in One Loop.

What the Algorithm is ?????



Code for this in java


package net.yogesh.test;

import java.util.ArrayList;
import java.util.List;

public class pascal3 {

    public static void main(String[] args) {

        int noOfRows = 10;
        int counter = 1;
        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
       
        list = itMe(list, counter,noOfRows);
    }

    public static List<Integer> itMe(List<Integer> list, int counter,int noOfRows) {
        System.out.println(list);
       
        List<Integer> tempList = new ArrayList<Integer>();

        tempList.add(1);
        for (int i = 1; i < list.size(); i++) {
            tempList.add(list.get(i) + list.get(i-1));
        }
        tempList.add(1);
       
        if(counter != noOfRows)
            itMe(tempList, ++counter,noOfRows);
       
        return tempList;
    }
}

Output

[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
[1, 6, 15, 20, 15, 6, 1]
[1, 7, 21, 35, 35, 21, 7, 1]
[1, 8, 28, 56, 70, 56, 28, 8, 1]
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]

Thursday, April 19, 2012

Google Maps : Create Marker and Draw Line Runtime

in this demo user can add a marker by simply click on map

and line is automatically draw by map between marker

also marker is Draggable.

Demo image



Code


<!--
     Created By : Yogesh Prajapati
           Date : 03 April 2011
-->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
        <title> Search Demo </title>
        <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA" type="text/javascript"></script>
      

        <script type="text/javascript">
            var map;
            var secondLastMarker = null
            var lastMarker = null;
           
            function initialize() {
               
                if (GBrowserIsCompatible()) {
                   
                    map = new GMap2(document.getElementById("map_canvas"));
                    map.setCenter(new GLatLng(23.450, 72.483), 11);
                    map.addControl(new GSmallMapControl());
                   
                    GEvent.addListener(map, "click", function(overlay, latLng) {
                        var marker = new GMarker(latLng,{draggable: true});
                        map.addOverlay(marker);
                        if(lastMarker == null){
                            lastMarker = latLng;
                        }else if(secondLastMarker == null){
                            secondLastMarker = lastMarker;
                            lastMarker = latLng;
                            drawLine(map,secondLastMarker,lastMarker);
                        }else{
                            secondLastMarker = lastMarker;
                            lastMarker = latLng;
                            drawLine(map,secondLastMarker,lastMarker);
                        }
                       });
                }
            }
           
            function drawLine(map,firstPoint,LastPoint){
                var polyline = new GPolyline([secondLastMarker,lastMarker
                                              ], "#ff0000", 5);
                    map.addOverlay(polyline);
            }
                       
        </script>
    </head>

    <body onload="initialize()" onunload="GUnload()" style="background-color:aliceblue">
           <div id="map_canvas" style="width: 100%; height: 550px;"></div>
        Demo Created By : <b>Yogesh Prajapati</b><br/>
    </body>

</html>

Note:

Line is just polyLine if you want to draw path between them than you need to do something different
this is just for demo purpose.

Create your own mini google map Search Engine

First of all see the example



in this demo  google api is used for Map and searching
Markers and search box is placed custom.

this is just a standalone HTML code you can copy and paste to your favourite writer
and save as html.

for doing such a thing use following code

<!--
     Created By : Yogesh Prajapati
           Date : 28 March 2011
-->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
        <title> Search Demo </title>
        <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA"
        type="text/javascript"></script>

        <script type="text/javascript">
            var map;
            function initialize() {
               
                if (GBrowserIsCompatible()) {


                    function doGenerateMarkerHtmlCallback(marker,html,result) {
                        html.innerHTML+="<b>Result Coordinates: "+result.lat+","+result.lng+"</b><br>";
                        html.innerHTML+="<b>Marker Location: "+marker.getLatLng().toUrlValue()+"</b>";
                        return html;
                    }


                    var options = {
                        resultList : document.getElementById('results'),
                        onMarkersSetCallback : function(markers) {
                            fillTable(markers);
                        },
                        onGenerateMarkerHtmlCallback:doGenerateMarkerHtmlCallback
                        //onGenerateMarkerHtmlCallback : function(marker, div, result) {div.innerHTML = result.title + "<br/>"; return div; }
                    };

                    map = new GMap2(document.getElementById("map_canvas"), {googleBarOptions: options});
                    map.setCenter(new GLatLng(23.450, 72.483), 8);
                    map.addControl(new GSmallMapControl());
                    map.enableGoogleBar();

                    var myLayer = new GLayer("com.google.webcams");
                    map.addOverlay(myLayer);

                }
            }

            function deleteAllRows()
            {
                for(var i = document.getElementById("tblResult").rows.length; i > 0;i--)
                {
                    document.getElementById("tblResult").deleteRow(i -1);
                }
            }
           
            function setCenterOnMap(lat,lng){
                map.setCenter(new GLatLng(lat,lng), 15);
            }

            function fillTable(markers){
                deleteAllRows();
                var table = document.getElementById('tblResult');
               
                var counter=65;
               
                for(i=0;i<markers.length;i++){
                    var row = table.insertRow(i);
                    var cell0 = row.insertCell(0);
                    var  newImage = "url(http://www.google.com/mapfiles/marker" + String.fromCharCode(counter) + ".png)";
                    cell0.style.backgroundImage = newImage;
                    cell0.style.backgroundRepeat="no-repeat";
                    cell0.style.width="18px";
                    cell0.style.height="35px";
                    cell0.align="center";
                    cell0.vAlign="top";
                    counter++;
                   
                    var cell1 = row.insertCell(1);
                    cell1.innerHTML = "<a href=javascript:void() onClick=javascript:setCenterOnMap(" + markers[i].marker.getPoint().lat() + "," + markers[i].marker.getPoint().lng() + ")>"  + markers[i].result.title + "</a>";
                    cell1.vAlign="top";

                    //alert(markers[i].marker.getPoint().lat() + " - " + markers[i].marker.getPoint().lng());
                }
            }
           
        </script>
    </head>

    <body onload="initialize()" onunload="GUnload()" style="background-color:aliceblue">
        <table border="0" width="100%" >
            <tr>
                <td style="width: 250px;" align="center" style="vertical-align: top">
                    <div id="results" style="visibility: collapse;height: 0px"></div>
                    <table border="0" width="100%" style="height: 450px">
                        <tr>
                            <td style="height: 10px">

                                <table border="0" width="100%">
                                    <tr>
                                        <td align="right" style="width: 65px">
                                            <img src="http://www.google.com/mapfiles/marker.png"/>
                                        </td>
                                        <td>
                                            &nbsp;&nbsp;Search Results
                                        </td>
                                    </tr>
                                </table>
                                <table border="0">
                                    <tr>
                                        <td>
                                            <img src="myLine.png" width="210px" height="1px" style="vertical-align: top"/>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr>
                            <td style="vertical-align: top">
                                <table id="tblResult" border="0" width="100%" cellspacing="5" >
                                </table>
                            </td>
                        </tr>
                    </table>
                </td>
                <td>
                    <div id="map_canvas" style="width: 100%; height: 550px;"></div>
                </td>
            </tr>
           
            <tr>
                <td align="right" colspan=2>
                    Demo Created By : <b>Yogesh Prajapati</b><br/>
                </td>
            </tr>
        </table>

    </body>

</html>

for any help related to Map contact.....

Disqus for yogi's talk

comments powered by Disqus