Thursday, 24 January 2013

C program to print some text without using semi-colon anywhere in the program

C program to print some text without using semi-colon anywhere in the program


#include<stdio.h>
#include<conio.h>
void main()
    {
      if(printf("Hi Mayank"))
        {
           }
     }

Output :   Hi Mayank

C program to run the body of if condition as well as body of else condition

C program to run the body of if condition as well as body of else condition

First program (using goto)


#include<stdio.h>

#include<conio.h>
void main()
{
      if(1)
        {
            printf("Hi Mayank");
            goto A;
         }
    else
       {
          A:
          printf("\nShekhar ");
          for(int i=1;i<11;i++)
          printf("%d ",i);
        }
 getch();
 }

Output :   Hi Mayank
                 Shekhar 1 2 3 4 5 6 7 8 9 10



Second program (using macro)




#include<stdio.h>

#include<conio.h>

#define else if(1)

void main()
{
      if(1)
        {
            printf("Hi Mayank");
         }
    else
       {
         
 printf("\nShekhar ");


          for(int i=1;i<11;i++)
          printf("%d ",i);
        }
 getch();
 }

Output :   Hi Mayank 
                 Shekhar 1 2 3 4 5 6 7 8 9 10



C program to print \n and "\n"

C program to print  \n 


#include<stdio.h>
#include<conio.h>
void main()
    {
        printf("\\n");
         getch();
    }

Output :     \n


C program to print  "\n" 


#include<stdio.h>
#include<conio.h>
void main()
    {
        printf("\%c\\n%c",(char)34,(char)34);
         getch();
    }

Output :     "\n"

Wednesday, 23 January 2013

Inserting and retrieving image from MySql

Insertion and retrieval of image from MySql

Step 1: Create a table in MySql for storing images.

mysql >connect test;
mysql >create table MyPictures
          >(id int(11),
          >name varchar(25),
          >photo blob);

Step 2: Run the following Java program to insert id, name & image into table MyPictures .
             The source image is present in E:\\Mayank.jpg  and again after retrieval the image is 
             saved in G:\\DatabasePhotos folder

         table name = MyPictures    (table to be connected)
         username = root                  (default username of MySql database)
         password = root                   (password is set by user during installation)
         database = test                    (database name within which the table to be connected lies)

Java Program :

import java.io.*;
import java.sql.*;
public class InsertPictureToMySql2 {
  public static void main(String[] args) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
    FileInputStream fis = null;
    PreparedStatement ps = null;
    try {
            conn.setAutoCommit(false);
            File file = new File("E:\\Mayank.jpg");
      fis = new FileInputStream(file);
      ps = conn.prepareStatement("insert into MyPictures(id, name, photo) values (?, ?, ?)");
      ps.setString(1, "7");
      ps.setString(2, "Mayank");
      ps.setBinaryStream(3, fis, (int) file.length());
      ps.executeUpdate();
      conn.commit();
    }catch(Exception ae)
{System.out.println("Problem in saving in database !! ");
}

Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select photo from MyPictures");
int i=1;
try{
if(rs.next())
{
String len1 = rs.getString("photo");
int len = len1.length();
byte [] b = new byte[len];
InputStream in = rs.getBinaryStream("photo");
int index = in.read(b, 0, len);
OutputStream outImej = new FileOutputStream("G:\\DatabasePhotos\\Photo"+i+".JPG");
while (index != -1)
{
outImej.write(b, 0, index);
index = in.read(b, 0, len);
System.out.println("========================================");
System.out.println("Photo added to database successfully");
System.out.println("Photo saved in DatabasePhotos folder");
System.out.println("========================================");
}
outImej.close();
i++;
}
}catch(Exception e)
{System.out.println("Problem in retrieving image from database !!");
}
 finally {
      ps.close();
      fis.close();
    }
  }
}






DB2 installation


DB2 installation

Screenshots :

After running the setup of DB2 proceed according to the screenshots.










Connection of Java program with DB2 database


Connection of Java program with DB2 database

For connecting a Java program to DB2, we need the following software :
1) Jdk (any version)
2) DB2
3) DB2 Driver (db2jcc.jar & db2jcc4.jar )

Download db2jcc.jar & db2jcc4.jar from the link provided below :

http://www.ziddu.com/download/21414620/DB2_Driver.zip.html

Open the downloaded zip file to get db2jcc.jar & db2jcc4.jar files.

Few steps in DB2 :  After installing DB2, open the command prompt and type
       "clpplus  <username>@localhost:50000/<database_name>"
For ex :
        C: \Users\Mayank>clpplus db2admin@localhost:50000/sample

Now create table using sql commands and insert values in it and commit.

Set CLASSPATH of DB2 Driver :
Set the location of db2jcc.jar & db2jcc4.jar  in the environment variable CLASSPATH .


The Java Program shown below insert values in the table project already created in the database and retrieve the table data on the console.          
In the following program,
         table name = project
         username = db2admin (given by user during installation)
         password = db2admin (given by user during installation) 
         database = sample (default database of DB2) 

Java Program :

import java.sql.*;
public class DBCdb2
{public static void main(String args[]) throws Exception
{Class.forName("com.ibm.db2.jcc.DB2Driver");
Connection con = DriverManager.getConnection("jdbc:db2://172.16.2.38:50000/sample","db2admin","db2admin");
Statement st= con.createStatement();
int i3=st.executeUpdate("insert into project values('mayank',21)");   // insertion of values
int i4=st.executeUpdate("insert into project values('vikash',21)");     // insertion of values
ResultSet rs = st.executeQuery("select * from project");
while(rs.next())
System.out.println(rs.getString(1)+"    "+rs.getString(2));
rs.close();
st.close();
con.close();
}
}


Note :  In case of database like DB2, we need to download the jar file containing Driver class(es) (db2jcc.jar & db2jcc4.jar in case of DB2 database) separately and then set their classpath as they are not supplied along with the database software.

Connecting Java program to MySql database


Connection of Java program with MySql database

For connecting a Java program to MySql, we need the following software :
1) Jdk (any version)
2) MySql
3) MySql Driver (mysql-connector-java-version-bin.jar  ex : mysql-connector-java-5.1.21-bin.jar)
   
Download the  mysql-connector-java-5.1.21-bin.jar from the link shown below :
http://www.ziddu.com/download/21414370/mysql_connector_java_5.1.21_bin.jar.html


Few steps in MySql :  After installing MySql, perform the following steps to create a new database and connect to it as the database name is required in Java program.


i) mysql >create database <databasename>;                        ----> Creating new database
ii) mysql >connect <databasename>;                                     ----> Connect to the database

Now create table using sql commands.

iii) mysql >show databases;                                         ----> Shows all the databases
iv) mysql >show tables;                                                ----> Shows all the tables present in that databases


Now create table using sql commands and insert values in it and commit.

Set CLASSPATH of MySql Driver :
Set the classpath  i.e the location of mysql-connector-java-5.1.21-bin.jar in the environment variable named CLASSPATH .

However the Java Program shown below creates the table table2 from the program itself and inserts values in it. If we don't want to create the table in the program and like to use the table table2 already created in database, then skip the 7th line of the program containing the create table statement.          
In the following program,
         table name = table2    (table to be connected)
         username = root         (default username of MySql database)
         password = root        (password is set by user during installation)
         database = frndz        (database name within which the table to be connected lies)

Java Program :


import java.sql.*;
public class DBCmysql2
{public static void main(String args[]) throws Exception
{Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/frndz","root","root");
Statement st= con.createStatement();
int i1= st.executeUpdate("create table table2(name varchar(12), roll_no int(10))");    //create the table
int i2=st.executeUpdate("insert into table2 values('mayank',1009007)");                   //insertion of values
int i3=st.executeUpdate("insert into table2 values('vikash',1009014)");                     //insertion of values
ResultSet rs = st.executeQuery("select * from table2");
while(rs.next())
System.out.println(rs.getString(1)+"    "+rs.getString(2));                               //printing table data on console
rs.close();
st.close();
con.close();
}
}


   
Note :  In case of database like MySql, we need to download the jar file containing Driver class (mysql-connector-java-version-bin.jar) separately and then set its classpath as it is not supplied along with the MySql database software.

Connection of Java program with Oracle11g database

Connection of Java program with Oracle11g database

For connecting a Java program to Oracle11g, we need the following software :
1) Jdk (any version)
2) Oracle11g


Few steps in Oracle11g:  After installing Oracle11g, perform the following steps to create a new "user" and set "password" for it as it is required in Java program.

i) sql >conn / as sysdba;                                                              ----> Connection to Sysdba
ii) sql >create user <username>;                                                ----> Creating new user
iii) sql >grant dba <username> identified as <password>;        ----> Granting dba to the user
iv) sql >conn <username>/<password>;                                     ----> Connecting to the user

Now create table using sql commands and insert values in it and commit.

Set CLASSPATH of Oracle11g Driver class :
Set the classpath  C:\oraclexe\app\oracle\product\11.2.0\server\jdbc\lib\ojdbc6.jar in the environment variable named CLASSPATH if location of ojdbc jar file is the same as mentioned in above line otherwise change the classpath accordingly.

However the Java Program shown below creates the table table2 from the program itself and inserts values in it. If we don't want to create the table in the program and like to use the table table2 already created in database, then skip the 9th line of the program containing the create table statement.        
In the following program,
         table name = table2
         username = frndz
         password = frndz


Java Program :


import java.sql.*;
public class DBCoracle2
{
     public static void main(String args[]) throws Exception
{
    Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","frndz","frndz");
Statement st= con.createStatement();
int i1= st.executeUpdate("create table table2(name varchar(12), roll_no number(10))");//creating table table2
int i2=st.executeUpdate("insert into table2 values('mayank',1009007)");                       //insertion of values
int i3=st.executeUpdate("insert into table2 values('vikash',1009014)");                         //insertion of values
ResultSet rs = st.executeQuery("select * from table2");
while(rs.next())
System.out.println(rs.getString(1)+"    "+rs.getString(2));                                          //Print values on console
rs.close();
st.close();
con.close();
}
}

Note : When we are working with Oracle database, we need not bother about the jar file containing Driver class separately as it is given along with the Oracle11g software. During installation jar file containing Driver class(ojdbc6.jar in case of Oracle11g)  is supplied automatically. So after installing Oracle11g, we just need to set the classpath and we are ready to connect the database using java program. In case of other databases like MySql, DB2, PostgreSql etc we need to download the jar file containing Driver class(es) separately and then set their classpath as they are not supplied along with their database softwares.

Frndz Boolean Solver

Boolean Expression Simplifier
Boolean Solver is a desktop application developed by me in Visual Basic 6.0 . Using this application, the user can get the boolean expression in its simplest SOP form or POS form by entering the min or max terms respectively. 


Screenshots :  




Frndz Media Player


Frndz Media Player
Frndz Media Player is a desktop application developed by me in Visual Basic 6.0 to run multiple audios/videos concurrently.

Click here to download it

Snapshots :





Frndz Graph Plotter


Frndz Graph Plotter
Frndz Graph Plotter is a desktop application developed in VB.Net. So .Net framework is required to run it.

Click here to download it

Screenshots :







Frndz Sudoku Solver


Frndz Sudoku Solver
Sudoku Solver is a desktop application developed in Java. So JRE(any version 1.5 onwards) is required to run it.

Click here to download it

Screenshots :







Frndz_Sudoku

Frndz Sudoku
A puzzle game developed by me in Visual Basic 6.0 .

Click here to download the game

Screenshots of the game :










Tuesday, 22 January 2013

Dream Mario

Dream Mario is a game developed by me in Visual Basic 6.0. 

Click here to download the game

Screenshots of the game :









Please mail your comments/feedback to zmsrms@gmail.com