Using the URL classes

You can use the URL classes to:

For example, you can mail a document using the URL class. Your client must be connected to a mail server so that the machine referenced by System Properties (in this example, it is salsa.sybase.com), is running a mail server such as sendmail.

For this example, the steps are:

  1. Create a URL object.

  2. Set a URLConnection object.

  3. Create an OutputStream object from the URL object.

  4. Write the mail. For example:

    import java.io.*;
    import java.net.*;
    public class MailTo {
    		public static void sendIt()
    			throws Exception{
    			System.getProperty("mail.host", "salsa.sybase.com");
    			URL url = new URL("mailto:name@sybase.com");
    			URLConnection conn = url.openConnection();
    			PrintStream out = new PrintStream(conn.getOutputStream(),true);
    			out.println ("From janedoes@sybase.com");
    			out.println ("Subject: Works Great!");
    			out.println ("Thanks for the example - it works great!");
    			out.close();
    			System.out.println("Message Sent");
    		}
    }
    
    

  5. Install mailto:URL for sending e-mail messages from within the database:

    select MailTo.sendIt()
    

You can also use the URL class to download a document from an HTTP URL. When you start, the client connects to a Web server. The steps are:

  1. Create a URL object.

  2. Create an InputStream object from the URL object.

  3. Use read on the InputStream object to read in the document.

The following code sample reads the entire document into Adaptive Server memory and creates a new InputStream on the document in memory.

import java.io.*;
import java.net.*;
public class URLprovess {
		public static InputStream readURL()
			throws Exception {
			URL u = newURL("http://www.xxxx.con");
			InputStream in = u.openStream");
			//This is the same as creating URLConnection, then calling
			//getInputStream(). In Adaptive Server, you must read the entire
			//document into memory, and then create an InputStream on the
			//in-memory copy.
			int n = 0;
			int off = 0;
			byte b() = new byte(50000);
			for)off = 0; (off<b.length512) &&
				((n = in.read(b.off,512)! = 1);off+=n) {}
			System.out.prinln("Number of bytes read :" + off);
			in.close();
			ByteArrayInputStream test = new ByteArrayInputStream(b,-,off);
			return (InputStream) test;
		}
}
			

After you create the new InputStream class, you can install this class and use it to read a text file into the database. The following example inserts data into table mytable.

create table mytable (cl text)
go
insert into mytable values (URLprocess.readURL())
go
Number of bytes read :40867
select datalength(cl) from mytable
go

---------------------
40867