Thursday, 19 September 2013

Simple C Program In Ubantu

Step 1:
Open Terminal
Write Command "gedit" to open editor

Step 2:
Write C Program In editor
for eg:

#include<stdio.h>

void main()
{
    printf("Hello Bharat");
   
}


Save the Program "bharat.c"

Step 3:
Open The terminal
CD command for change directory
 cd Desktop
(For example you save the program in Desktop then use cd Desktop)

Step 4
First compile the program using command
gcc bharat.c -o  bharat

if you use only gcc bharat.c then create a.out file
-o file name is the output file

Step 5:
Run The Program using command
"./outputfile name"

./bharat

Step 6:
Output show in terminal
Hello Bharat

Monday, 16 September 2013

Separation of Number And Char from String.

Separation of Number And Char from String.

Program:


public class DemoProgram {
public static void main(String[] args) {
String s = "(123)-456SS-7891ABCC";
String s1 = "(123)-456SS-7891ABCC";
   s = s.replaceAll("[^0-9]", "");
   s1 = s1.replaceAll("[^a-z|A-Z]", "");
   System.out.println("Number=="+s);
   System.out.println("Char=="+s1);

}

OutPut:
Number==1234567891
Char==SSABCC

Wednesday, 28 August 2013

How to disable back button in Web Browser

 Disable back button in Web Browser

1)Using Filter In Web.xml
<filter>
<filter-name>noCacheFilter</filter-name>
<filter-class>com.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>noCacheFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>

Call The Servlet LoginFilter for every Request 
package com;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sap.mw.jco.JCO.Request;


public class LoginFilter implements Filter{


public void init(FilterConfig config) throws ServletException {
// If you have any <init-param> in web.xml, then you could get them
// here by config.getInitParameter("name") and assign it as field.
}

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
HttpSession session = request.getSession(false);
System.out.println("filter called");

response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
response.setDateHeader("Expires", 0);
chain.doFilter(req, res);
//response.sendRedirect(request.getContextPath() + "/login.jsp"); // No logged-in user found, so redirect to login page.
}

public void destroy() {
// If you have assigned any expensive resources as field of
// this Filter class, then you could clean/close them here.
}

}





Thursday, 22 August 2013

Addition of number in Given String using Java Program

Addition of number in Given String using Java Program
Example
String is "121Bharat23"
Addition on 1+2+1+2+3=9

******************************************************
public class StringOperation {
public static void main(String[] args) {

String GivenString="121Bharat23";
int Total=0;
char stringarray[]=GivenString.toCharArray();
for(int i=0;i<stringarray.length;i++)
{
//System.out.println(stringarray[i]);
if(Character.isDigit(stringarray[i]))
{
int b = Character.getNumericValue(stringarray[i]);
             Total+=b;
}
}
System.out.println("Total=="+Total);

}
}
********************************************
Out Put
Total==9




Sorting Array (Bubble Sort)

public class Sorting {


public static void main(String[] args) {

int a[]={9,3,6,8,1,2,4,6};
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]);
}
System.out.println("\n");
int temp;
for(int k=0;k<a.length;k++)
{
 for(int z=0;z<a.length-1;z++)
 {
if(a[z] > a[z+1])
{
temp=a[z];
a[z]=a[z+1];
a[z+1]=temp;
}

 }
}
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]);
}
}

}


Out Put

93681246

12346689

Wednesday, 19 June 2013

Call The Stored Procedure Using Java Program

Step 1:

Create a Stored Procedure in MySQL.

For Eg.

 BEGIN
SELECT order_No,Person_Name,Posting_Date,PlantID,Material,Activity,WorkCenter,Yield_Entity,Yield_Unit,Confirmation FROM production_confirmation where (order_No like ordernumber) AND (PlantID like plant) AND (Person_Name like name) AND (Posting_Date like pdate) ;
END

Parameters:

ordernumber varchar(20),plant varchar(20),name varchar(20),pdate varchar(15)


Step 2;

Write a Java Program to Connection to MySQL


Class.forName("com.mysql.jdbc.Driver");

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/project", "root",  "root");

Statement st=con.createStatement();

st.execute("call Store_Procedure('"+value1+"','"+value2+"','"+value3+"','"+value4+")");

ResultSet rs=(ResultSet) st.getResultSet();

rs.next();

String =rs.getString("order_No");

 

Done ................










Thursday, 16 May 2013

File Handling Using Java

Create Class File.Java
import FILE IO.*;

Example Code for Creating Folder



 // Report is the name of Folder Witch You want to create
    File path = new File("E:\\Report");
   System.out.println("path=="+path);

// Check Folder Is available Or Not  in given Directory

                     File file2 = new File("E:\\Report");
                    if (!file2.exists()) {
                    file2.mkdir();
                    }
//If Exist then Create Directory in file name and copy File into directory


if (file2.exists())
{
      File file1 = new File("E:\\Report\\"+filename+"\\"+filename);
                if (file1.mkdir()) {
                System.out.println("File Name Directory is created!");
                File uploadedFile = new File(file1 + "/" + filename);
                item.write(uploadedFile);
               
                } else {
                System.out.println("Failed to create directory!");
                }
      }





Delete Empty Directory
use This Code
File directory = new File("E:\\Report\\"+filename);
directory.delete();

//If the directory contain File then use this code


File fin = new File("E:\\Filename");
   System.out.println("fin-"+fin);

   for (File file : fin.listFiles()) {
       try {
FileDeleteStrategy.FORCE.delete(file);
System.out.println("delete");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
      // Delete when directory is empty
              fin.delete();
   }