Friday, November 9

Thursday, November 8

Open Notepad Application using JAVA

/*
* Open Notepad Application using Java
*/


import java.io.File;

public class pdf {

    public static void main(String[] args) {

      try {
            Process p = Runtime.getRuntime().exec("notepad"); //Process starts notepad

            p.waitFor();
           
        System.out.println("Done");

        } catch (Exception ex) {
        ex.printStackTrace();
      }

    }
}

output:
~~~~~~~

Done


Note: it will open a notepad after the execution

Open a PDF file using JAVA

/*
* Open a PDF file in new window using JAVA
*/

import java.awt.Desktop;
import java.io.File;

public class OpenPDF {
    public static void main(String[] args) {
      try {
        File pdfFile = new File("c:\\sample.pdf"); //give your PDF location
        if (pdfFile.exists()) {

            if (Desktop.isDesktopSupported()) {
                Desktop.getDesktop().open(pdfFile);
            } else {
                System.out.println("Awt Desktop is not supported");
            }

        } else {
            System.out.println("File is not exists!");
        }

        System.out.println("Done");

      } catch (Exception ex) {
        ex.printStackTrace();
      }

    }
}

Output
~~~~~~
Done

Note: it open a PDF file in separate window

Wednesday, November 7

Oracle Database connection using Java



/**
 *  Program for simple database connectivity and records fetching from table 
 */

import java.sql.*;

public class dbCon {
    public static void main(String arg[]) throws SQLException{
        Connection con=null;
        Statement stmt=null;
        ResultSet rs=null;
        String name="";
            try{ 
                
                /*
                 * Java jdbc driver name -> oracle.jdbc.driver.OracleDriver
                 * Java url driver type  -> jdbc:oracle:thin:
                 * Address               -> @localhost "or" 120.15.87.153
                 * Port using Oracle     -> 1521
                 * Database Name         -> hbsp54db
                 * Username of Oracle    -> tp2
                 * Password of Oracle    -> tp2
                 */
                
                // Load the JDBC driver       
                Class.forName("oracle.jdbc.driver.OracleDriver");
                //Create a connection to the database
                con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:hbsp54db","tp2","tp2");
            }
            catch(Exception e){
                System.out.println("Unable to connect Database "+e);
            }
            
            String query = "Select username FROM users";     // use your table here       
            stmt = con.createStatement(); //Statment creation
            rs = stmt.executeQuery(query);  // resultset for executeQuery
            while(rs.next()){   //checking value in next record from database
              name= rs.getString(1); //geting username from user table
              System.out.println(name);
            }
            con.close();
                
}
}
 
output:
~~~~~~~

balamurgan
krishna
sri

PDF file generation code using java

// This code help to create a PDF file using java. that is stored in given file location.

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;

import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;

public class pdfGeneration {

    public static void main(String[] args) {
        try {
            OutputStream file = new FileOutputStream(new File("C:\\newPDF.pdf")); //Saved file path

            Document document = new Document();
            PdfWriter.getInstance(document, file);

            document.open();
            document.add(new Paragraph("Welcome to TECHIE SNACKS, Author Balamurugan.M.R"));
            document.add(new Paragraph("This is simple pdf generation code \n Created on \t"+ new Date().toString()));

            document.close();
            file.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Notes: Please and Must include the " iText-2.1.5" jar file then only it works. 

You will find the jar in given location " http://grepcode.com/snapshot/repo1.maven.org/maven2/com.lowagie/itext/2.1.5 ".

Enjoy the life :)

Reading CSV file using java

// This is code  read csv file columns and display as it is in console. this is not consider empty value

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.StringTokenizer;

public class csvRead {
        public static void main(String[] args) {            
                try
                {
                        String filePath = "C:/xls/csv_person.csv";   //csv file path
                        String str = "";
                        StringTokenizer st = null;
                        int lineNo = 0, tokenNo = 0;
                     
                        //create BufferedReader to read csv file
                        BufferedReader br = new BufferedReader( new FileReader(filePath));

                        //read comma separated file line by line
                             while( ( str =br.readLine()) != null) {                            
                                br.readLine();
                              
                               //break comma separated line using ","
                                st = new StringTokenizer(str, ",");
                                while(st.hasMoreTokens()) {
                                        tokenNo++;
                                        System.out.println( st.nextToken());
                                }
                              
                                //reset token number & increment the lineNo
                                 lineNo++;
                                tokenNo = 0;
                        }
                }
                catch(Exception e){
                        System.out.println("Exception while reading csv file: " + e);               
                }
        }
}

output:
~~~~~


USERNAME
CUSTOM1
CUSTOM2
CUSTOM3
TYPE
CREATED_ON
ALLOW_NONBUDDIES
CUSTOM5
String lenght46
20501427
vivek
hello
hai
2
01-jan-12
50
record
String lenght32
40021456
vivek
sample
record
String lenght0
String lenght0
String lenght0
String lenght46
20501427
vivek
hello
hai
2
01-jan-12
50
record
String lenght0
String lenght0
String lenght0
String lenght20
basker 65
vivek

Tuesday, November 6

#1045 - Access denied for user 'root'@'localhost' (using password: NO)

Hi All,

I have the error(#1045 - Access denied for user 'root'@'localhost' (using password: NO) )

I have read solution on other post that is as follow:-
I'm just to confirm you that it's the solution.

I was writing ( UPDATE mysql ) : so please follow the next steps if you want to resolve your problem with ( #1045 - Access denied for user 'root'@'localhost' (using password: NO):

1 : go to your WAMP icon on your PC desktop screen and LEFT CLICK to open the menu, you will see MYSQL folder, CLICK to see MYSQL CONSOLE, open it.

2: now you have DOS screen ( a black screen ) :

A: if you already set a password, type it
B: if you did not do this step yet, write the following red text
B1: use mysql; and click ENTER on your keyboard
3: now write the following red text and click ENTER :

UPDATE mysql.user SET Password=PASSWORD("YOUR PASSWORD") WHERE User="root";

 your command will execute when you write this sign ( ; ) at the end of your text and click ENTER.

NOTE: replace the password "YOUR PASSWORD" by your password.

4: now write the following red text and click ENTER :

FLUSH PRIVILEGES;

5: and to exit the black DOS screen now, write exit and click enter.

------------------------- we are finished from MySQL now --------------

6: go to WAMP folder ( open your My Computer, click on C driver, and you will see WAMP folder ), click on APPS folder, and than click on your PHPMYADMIN folder ( e.g C:\wamp\apps\phpmyadmin3.5.1) and find the config.inc.php

7: open config.inc.php and find the following orange text:

$cfg['Servers'][$i]['password'] = '';  // MySQL password  (only needed and add your password now that you used in step number 3 like that).

Example:
$cfg['Servers'][$i]['password'] = 'root';

[COLOR="rgb(255, 140, 0)"]$cfg['Servers'][$i]['password'] = 'YOUR PASSWORD; // MySQL password (only needed[/COLOR]

8: now save this modification, and close config.inc.php

9: go to your web browser and type the following link :

http://localhost/phpmyadmin/

and enjoy, this is what happened with me.

Backup files to google drive using PHP coding

 Dear All, To backup files to Google Drive using PHP coding, you can use the Google Drive API and the Google Client Library for PHP. Here...