Link to home
Start Free TrialLog in
Avatar of bessy
bessy

asked on

Java Applet - MS Access Db

Hello,

I need some information of how to connect to an MS-Access Database from an applet. I basically need to know the commands to use to connect to the database and manage the database from the applet. A simple example will do.

Thanks
Bessy.

Avatar of dnoelpp
dnoelpp
Flag of Switzerland image

Hmmm, I have the feeling that you don't have a clue (or at least not a lot)...

Why not write an ActiveX control if you need Microsoft specific things (like accessing an Access database)?

Okay, for a starter, I can give you a link. This is a Java software to use COM from Java. You still need to know how to program Access' object model anyway, and this is not a Java topic. The software just opens the door for Windows applications like Excel, Word, Access, etc. to Java applications. With JIntegra you can automate Windows application which have a COM object model.

The link (go to JIntegra): www.linar.com

Perhaps you will get more help if you are more specific about your needs. Is the database on the client or on the server? Are you programming for Windows specifically (programming an applet with J++ and using Microsoft incompatible extensions)?

Cheers!
Avatar of bessy
bessy

ASKER

Well,

at the moment I have connected an ASP page to my access database. Everything works fine at that point. So I believe that my access model is already programmed since it interacts with the ASP. The extra thing I have no idea how to do is to connect my java applet to my database. The applet has to read data from my database as well and interact with it.

Thanks
hi !
i've got an idea...
create a servlet in your server that talks to ms access db...
then by accessing ur servlet (from ur applet), u should be able to talk to ur db...

hope it helps.

regards
Sendoh
just let me know if u want the detail...:)
ASKER CERTIFIED SOLUTION
Avatar of dnoelpp
dnoelpp
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bessy

ASKER

Well dnoelpp,

I tried to use it but the applet cannot access a txt file when it is online, it works only offline.This is my biggest problem.

On the other hand Sendoh I have absolutely no idea how I can do this with Servlets.

The other thing I thought about is to access my Database from Javascript and not ASP as I am currently doing and try to pass the variables from Javascript to the Applet. Does anyone know if this is possible? And if it is is there a specific kind of code I have to use?

Thank you all for responding
Bessy
Bessy, you're in the process of mixing up server-side and client-side Javascript.

In the ASP you can access the database with server-side script. This is not possible with client-side script because the database is not on the client.

This can get very confusing -- I worked once with MSXML both on client- and on server-side and had a lot of a headache about client- and server-side.

And as for applets ("offline" vs "online"): They can open web pages on the same server they are downloaded from (unsigned applets). Assume, you have on your server a page default.asp, you can open and read it in your applet with the following code (it works!!)

URL url = new URL("http://yourserver/yourpath/default.asp");
InputStream is = url.openStream();

(enclose it in try catch to catch IOException).

So why not exploit this to write an ASP page which dumps part of the database to the applet? That was my previous suggestion.
Avatar of bessy

ASKER

Well if you have tried it and it works I am gonna try it myself right now. Another question though to see if I understood it completely: If I am reading the characters of the asp I will need to filter them afterwards so that I will not take for example the tags i.e.<HTML>, as characters. Is this correct?
Well, I am gonna try it and I 'll get back to you...
Thanx
Bessy
Yes, if your ASP page produces HTML. But you could program your ASP page to produce only text. Like this:

--- 8< ------ 8< ------ 8< ------ 8< ---
Bantigerstr.,8,3018,Bern,Switzerland
Bundesterasse,1,3002,Bern,Switzerland
Place Centrale,3,2701,Verbier,Switzerland
Schaufelgrabenweg,4,3033,Wohlen b. Bern, Switzerland
--- 8< ------ 8< ------ 8< ------ 8< ---

This would be the output of a table consisting of 4 rows with the columns: street, houseno, postal code, city, country. You see, the values are separated by commas.

But, how your ASP page looks exactly, that's completely up to you. Maybe you prefer some other format which is even more easier to be parsed by your applet.
Avatar of bessy

ASKER

Well I did the following test applet: Online.java (the code is shown below and I am trying to read my asp which is also online and it still does not work. Can u please spot what I have done wrong this time?

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.io.*;
import java.net.*;


public class Online extends Applet implements ActionListener  {

Button moo;
TextField ftext;

BufferedReader br;
URL url;

  public void init(){

    moo = new Button("Display");
    add(moo);
    moo.addActionListener(this);

    ftext = new TextField(10);
    add(ftext);
    ftext.addActionListener(this);

  }

  public void actionPerformed(ActionEvent e){

    if (e.getSource() == moo) {
     doIt();
    }

  }

  public void doIt(){

    try {
      url = new URL("http://www22.brinkster.com/bessy1978/online.asp");
      br = new BufferedReader(new InputStreamReader(url.openStream()));
    }
    catch (Exception e) {
      e.printStackTrace();
    }

    try {
      for(int i=1; i>1; i++){
        String c;
        String line=null;
        c = br.readLine();
        line = line + c;
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }

    }


  }

Replace this part

   try {
     for(int i=1; i>1; i++){
       String c;
       String line=null;
       c = br.readLine();
       line = line + c;
     }
   }
   catch (Exception e) {
     e.printStackTrace();
   }

with:

       StringBuffer contents = new StringBuffer();
       try {
         String line;
         while ((line = br.readLine()) != null) {
            contents.append(line);
         }
       }
       catch (Exception e) {
         e.printStackTrace();
       }

After then, in the StringBuffer contents is the whole HTML file of your Bessy airplane seats page.
Tip 1: br.readLine() returns null on EOF.

Tip 2: Don't forget to close br. (Not included in my example code)

Cheers!
Avatar of bessy

ASKER

Thank you very much for all the help.

Well I did replace my code with yours, it compiles fine, but still when the applet is online according to the code it is supposed to show me all the characters from the asp to the textfield by pressing the display button.

The address is: http://www22.brinkster.com/bessy1978/online.html and when the applet loads it does nothing...

Actually what I really have to do is much more complicated and it is my master thesis :-( It is an online booking system for a theatre and I have to display in an applet the seats with different colours depending whether or not the seat is booked. It is not an airline company :-)

Anyway, can you please check the above address to see by yourself what I mean?

Thanx again
Bessy
Hello Bessy

I am sorry, here in Switzerland it's already evening. And since it's Friday, you need to wait till Monday when I am back in office. I post this from home, where I don't have the development system.

But, however, in the mean time please post the applet source code .java with which you compiled Online.class
Thanks. Maybe I go to office tomorrow.

I really would like to help you till everything works...
I will give you the html page which hosts the applet and the applet source code.

A note, however, do you know about the Java Plugin?

Cheers and don't give up, we (and others from EE) will together get your applet up and running!
Avatar of bessy

ASKER

Hi :-)

Thanx a lot...

Well it's evening in England as well :-)

My source code for Online.java is:

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.io.*;
import java.net.*;


public class Online extends Applet implements ActionListener  {

Button moo;
TextField ftext;

BufferedReader br;
URL url;

  public void init(){

    moo = new Button("Display");
    add(moo);
    moo.addActionListener(this);

    ftext = new TextField(10);
    add(ftext);
    ftext.addActionListener(this);

  }

  public void actionPerformed(ActionEvent e){

    if (e.getSource() == moo) {
     doIt();
    }

  }

  public void doIt(){

    try {
      url = new URL("http://www22.brinkster.com/bessy1978/online.asp");
      br = new BufferedReader(new InputStreamReader(url.openStream()));
    }
    catch (Exception e) {
      e.printStackTrace();
    }

    StringBuffer contents = new StringBuffer();
      try {
        String line;
        while ((line = br.readLine()) != null) {
           contents.append(line);
        }
        br.close();
      }
      catch (Exception e) {
        e.printStackTrace();
      }
    }


  }

I did it as you told me... :-) Please check whenever you can... If you want to send me something my private email address is: bessy_ch@hotmail.com

Anyway, what is the Java Plugin? Can you let me know? Is it gonna help me at all?

Have a nice evening :-)
Bessy


Okay, I told you how to read the web page into the Stringbuffer contents variable, but not how to display it. So, what we have here is that your applet REALLY HAS READED the web page but NOT DISPLAYED... :-)

And, printing to the console (System.out.println()) doesn't show very much because an Applet doesn't have a console. A console is very much like a DOS window where you can see the output from System.out.

To see the output you can add a TextArea and write something like this textArea.setText(contents.toString); But since I don't have the documentation at home I am not sure about the syntax. This will only show that you really did the feat: Reading the page from applet :-) but won't be of much use because you need to show the data in usable and modifiable form.

A question concerning the Java Plugin. Did you install the JDK on your computer? Which version? Because the JDK has the Java Plugin already integrated. The Java Plugin allows to take the same Java Virtual Machine for applets as for Java Applications. The problem is that Micro$oft stopped development for Java and that Internet Explorer has an old and incompatible Virtual Machine built-in. The difference is that the HTML code doesn't use the <APPLET> tag, but the <EMBED> (Netscape) or the <OBJECT> (Internet Explorer) tag. With the other tags Java Plugin tells the browser to use the newer Java Virtual Machine. We at our project in office use the Java Plugin. I will send you the exact HTML code to use the Plugin.

Java Plugin, for example, supports Swing. If you want to program with Swing, you need the Plugin. One note, however, your customers need to download the plugin (about 5 to 7 MB) to view your applet. We at the office decided to use the Plugin and wrote a small introduction page to explain why the download of the Plugin is needed.

Partly the situation is this complicated because of the war between Sun and Micro$oft. They don't want to work and help together and everybody feels this war when using applets... :-(

And now back to your exact problem. Your page shows a table with a lot of letters like A1, C1, etc. What exactly do you want to show and change from your applet?
Avatar of bessy

ASKER

Well,

as I already told you, this is just an example I am working on. My real applet shows the seats of a theatre. All the seats are buttons. I need the applet to check from the asp all the seats that are available, i.e. buttons of available seats in greeen colour, which would also be links to the form that the user has to complete to make the booking, on the other hand the seats that are not available will be shown in red colour. My supervisor is obssesed with this idea of the applet showing the available seats. So, the only thing I want the applet to do is just to check my asp for seats availability in each performance. It sounds to hard even to me... and I am running out of time at the moment. Anyway, do you think as an expert that it is possible for me to do something like that?

Thanx Again
Bessy
This seems to be a major project. One of the questions would be: What, if the seats aren't ordered in a grid-like fashion? Will then the applet paint the seats in a way that resembles the seat layout most? Then how does the applet know the exact layout? Difficult will be if some of the seats are oblique (tilted).

Okay, let's forget the seat layout for the moment. By the way, today was fine and hot wheather, so I didn't go to office... I went swimming with friends.

Let's assume the database just delivers a table like this:

Seat No | Free
--------|------
A1      | Yes
A2      | Yes
A3      | No
...

Then write an ASP page which prints a line for each seat. After the seat no print a comma, then "f" for free and "t" for taken. Like this:

A1,f
A2,f
A3,t
...

Then you can parse the output of the ASP page as follows:

      Hashtable seats = new Hashtable();
      try {
        String line;
        while ((line = br.readLine()) != null) {
           String seatNo = line.substring(0, line.indexOf(','));
           String freeOrTaken = line.substring(line.indexOf(',') + 1);
           seats.put(seatNo, freeOrTaken);
        }
      }
      catch (Exception e) {
        e.printStackTrace();
      }

After the while loop you have all seats in the hashtable and with seats.get(seatNo) you get the string freeOrTaken you read in (that means either "f" for free or "t" for taken).
Avatar of bessy

ASKER

Well,

good news...

I have managed to access a database from my applet, it just works with Netscape Communicator and NOT with Internet Explorer. So, now I can read from the database. At the moment I am trying to insert an new record in my database from the applet and I don't know the exact code for that...

So, I lock this question, thanx donoelpp for your help... :-)

Right now I am asking a new question about my new query....

Thanx again
Bessy

P.S. Please if you know the answer to my other query please let me know ASAP.... :-)
I am afraid, you're going in the wrong direction. Basically you can't access an Access database if it is not on the same computer. (network drives count being on the "same computer".) Perhaps you can read it (download from the webserver), but not change.

Let's assume the theater has an access database on their web server. Then I can't access it from my dial-up account here.

The ASP pages, being on the same computer, can access the database. So the applet needs to talk to the ASP page and ask it to retrieve and change values.

Here you're again in the process of mixing up server- and client-side.

I am sorry.
Avatar of bessy

ASKER

I see what you mean... :-(

I noticed that problem yesterday evening, and I was very happy before I thought it would work properly... :-(  

I am trying to write to a txt file from the asp and I get the message that permission is denied... Do you know any servers that wouldn't have this kind of restriction and I could write on this file?

Thanx
Bessy
It shouldn't be a big problem to write a text file from asp. But this file is on the server, not on the client.

If you would want to write a file on the client, this is not allowed because of security. Imagine an evil website which writes so many files that the hard disk at the client is filled to the brim? That's why it's forbidden to write a text file on the client.

So, do you want to continue with the solution I suggested? Client-Server on the web usually work a little convoluted because therer are two programs running at the same time: the asp page and the applet. And they don't have the same environment and the same permissions...

Please answer, because I want to help you that you get at least a minimal working solution.
Avatar of bessy

ASKER

Well of course I need your help... :-)

What I am using is www.brinkster.com server and I am trying to write a txt file from asp to the server and it says permission denied.  

The code I am using is:

<html>
<head>
<title>Another Test</title>
</head>
<body bgcolor="#000080" text="#FFFFFF" link="#3399FF" vlink="#666666" alink="#FF9900">
<font face="Verdana, Arial, Helvetica">

<%
Dim fso, f
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set f = fso.CreateTextFile(Server.MapPath("\bessy1978\Test.txt"), True)
f.WriteLine("This is a test.")
f.Close
Set f = Nothing
Set fso = Nothing
%>

<b><font face="Times New Roman, Times New Roman, Times" color="#FFFFFF" size="3">
<p>&nbsp;</p>
</body>
</html>


and I get the following error:

Microsoft VBScript runtime error '800a0046'
Permission denied
/bessy1978/anothertest.asp, line 12

if you want to see it by yourself check this: http://www22.brinkster.com/bessy1978/anotherTest.asp

Sorry, I am very tired, I don't know what I should do... :-(

Thanx Again
Bessy
I think that writing to a file in your home page folder on the server should be possible. The best bet is to write them an e-mail and ask them to give you the permission.

Avatar of bessy

ASKER

I have send them about 5 emails so far, and nothing... That's why I am asking if you know any other servers that support it...
Avatar of bessy

ASKER

I have send them about 5 emails so far, and nothing... That's why I am asking if you know any other servers that support it...
Bessy, with the following sketch of a solution, you don't need to write txt files and you can access and modify an Access .mdb file on your server from a client's applet:

1. Write an ASP page which dumps a table as text with comma-separated values (You know this part already).

2. Write an ASP page which, on call, changes some value in your database. Call it with parameters, like this:

modify.asp?seatno=a1&value=free

3. Write an applet which calls both pages whenever needed.

This is a proofed solution and not very difficult to implement, compared to other server-client solutions!

Forget your other approaches, they don't work because you can't access the database directly from client to server.
Do you still work on this project?
Avatar of bessy

ASKER

Well, I left it as it was... Never mind, you helped me a lot anyway...

Why don't you post an answer so I can award you the points?

Thanx
Bessy
That's a policy from me because I never assume that a suggestion from me will really be an answer for you. You can accept an comment as an answer very easily and don't need to wait for an "answer".

If someone posted an answer and you feel it wasn't worth the bytes :-) you could reject it and wait for more comments or answers.

At the top right corner of each comment you have a button labeled "Accept comment as answer". Just click on one comment of me which you liked the best. Thank you!
Avatar of bessy

ASKER

No,thank you... :-)

Bessy