Friday 4 April 2014

How to read a file using Java Applet and jdbc thin client.


java oracle tutorial
This program is written in java and can be used in windows application as well as a servlet.
All you need to do is to do some changes in the code, the main objective of this program to understand the the connectivity from java to Oracle using jdbc thin client driver.
 Here is a link to download ojdbc.jar file. This file can also be used in programming JSP, where you need to put this file in you lib folder/directory.




import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import javax.swing.JScrollPane;
import javax.swing.JApplet;
public class readFileApplet extends Applet {
    String fileToRead="OracleCon.java";
    StringBuffer strBuff;
    TextArea txtArea;
    Graphics g;
    public void init()
    {
        txtArea = new TextArea(100,100);
        txtArea.setEditable(true);
        JScrollPane sp=new JScrollPane(txtArea);
        add(txtArea,"center");
        String prHtml = this.getParameter("fileToRead");
        if(prHtml!=null) fileToRead = new String(prHtml);
        readFile();
        add(sp);
    }
    public void readFile()
    {
        String line;
        URL url=null;
        try
        {
            url = new URL(getCodeBase(),fileToRead);
        }
        catch(MalformedURLException e){}
        try
        {
            InputStream in = url.openStream();
            BufferedReader bf = new BufferedReader(new InputStreamReader(in));
            strBuff = new StringBuffer();
            while((line=bf.readLine())!=null)
            strBuff.append(line + "\n");
            txtArea.append("File Name : " + fileToRead + "\n");
            txtArea.append(strBuff.toString());
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }
}

No comments: