Pages

Monday, December 17, 2012

Synonyms in PL/SQL

Hi falks,

In pl/sql synonym refers to schema objects which are created by a particular user to access an objects which  didn't belongs to him.Sometimes we have to join certain modules of a product to retrieve data for business purposes. Normally the particular user has to create the synonym for the related object according to the data requirement. The owner of the object should give grants for this user. 

Owner can create synonyms and give grants to other users who are using different schema s.

User cannot create a synonym for different schema other than his own schema from different schema other than his own schema.

Example statement of creating synonym from different schema for a certain table. Just imagine that your schema is ABC and other schema which include the table you want is DEF and table is employees. Statement is,

      CREATE OR REPLACE PUBLIC SYNONYM synemployees FOR ABC.employees;

User don't have to give any kind of grants to the synonym for his schema. It will assign all the grants automatically when synonym is creates.
But user can give grants to other users (schemas) while creating the synonym.

Thursday, December 6, 2012

Anagram words in text file and file write and replace text through java

Hi all,

Before few days I was having an assignment which need to apply Anagram to the words in a given text file through java. No way of using predefined methods or algorithms for this. Therefore I created simple java program to do this. This might also help you to understand about anagram and some techniques of applying anagram through java.

This is the source code.....

Just imagine that the existing file name is "test"

import java.io.*;
import java.util.StringTokenizer;
 

public class FileRead {

    static String sentence [];
    static String temp [];
    static int i = 0;
    static String first="";

 public static void Read() {

  try{

  // Read the existing text file

  FileInputStream fstream = new FileInputStream("test.txt");
  DataInputStream in = new DataInputStream(fstream);
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  

  String strLine;
 

  while ((strLine = br.readLine()) != null) {

     StringTokenizer stk = new StringTokenizer(strLine);
     sentence = new String[stk.countTokens()];
    temp = new String[sentence.length];
    while(stk.hasMoreTokens()) {
    sentence[i] = stk.nextToken();
    i++;

  }

  for(int x=0;x<sentence.length;x++){

if(x==0){
temp[x]= sentence[(sentence.length-1)];
}
else{
temp[x]= sentence[x-1];

}

  }
  for(int x=0;x<temp.length;x++){
     first=first+temp[x]+" ";

  }
  // Print original sentence
  System.out.println (strLine);
  System.out.println ("\n");
  System.out.println ("Apply anagram for the above sentence.");
  //Print Anagramed sentence.
  System.out.print (first+"\n");

  first="";

You can write this new phrase by using Filewritter and Bufferwritter as below mentioned. I'm using new text file to write new phrase......


// Write Anagramed sentence to new text file
File file =new File("test1.txt");
    //if file doesnt exists, then create it
    if(!file.exists()){
    file.createNewFile();
    }
    FileWriter fileWritter = new FileWriter(file.getName(),false);
    BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
    bufferWritter.write(first);


The statement "FileWriter fileWritter = new FileWriter(file.getName(),false);" includes "false" in the end of the statement. We can use true instead of using false. The different of these two is , we can write new word or phrase right after the existing word or phrase in the text file by using "true". When we use "false" it will replace the existing word or phrase by given word or phrase.