Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Applets and the fly likes db access through applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Applets
Bookmark "db access through applet" Watch "db access through applet" New topic
Author

db access through applet

Greg Triant
Greenhorn

Joined: Nov 25, 2007
Posts: 28
I use the following code inside my applet to connect to the db.

try{
Context ctx = new InitialContext();
if (ctx == null)
throw new Exception("Error - No Context!!!");

Context envContext = (Context)ctx.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/myDB");

con = ds.getConnection();
} catch (Exception e ){
e.printStackTrace();
}

But I am not getting the results that i need inserted, after the applet has finished running. I know almost nothing about applets, so any help is much appreciated!!!
Is it possible for me to connect to the db through an applet loaded on a web page?? Both wep application and db are on the same host. What's wrong with the above code???

PS. I hope this is the right place for the post....
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30580
    
154

Greg,
Welcome to JavaRanch!

I'm moving this to our applets forum. While the question is about both JDBC and applets, people in the applets forum will have the security information about applets accessing databases.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Greg Triant
Greenhorn

Joined: Nov 25, 2007
Posts: 28
Originally posted by Jeanne Boyarsky:
Greg,
Welcome to JavaRanch!

I'm moving this to our applets forum. While the question is about both JDBC and applets, people in the applets forum will have the security information about applets accessing databases.


x() OK Thanks! Never noticed the Applet thread
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42031
    
  64
What are "the results that i need inserted"? The code above doesn't perform any inserts, it merely tries to get a connection. Are there any exceptions? How are you setting up JNDI, and specifically the jdbc/myDB entry?


Ping & DNS - my free Android networking tools app
Greg Triant
Greenhorn

Joined: Nov 25, 2007
Posts: 28
I am including in a web page (jsp) a quite big applet (about 150 java class files). Somewhere in there, I am trying to initialize a db connection, in order to perform some insertions...

I only posted the connect() method, since after that the rest is pretty standard. It's stuff that I have used over and over on web pages to get database connectivity. The database resource is set by context.xml file.

The thing is that (using firefox) I don't get any errors, but I just don't see the desired outcome in the database afterwards....

Also, I should mention that when I run the java project as an Applet (not through a web page) but through java applet viewer, everything works fine.

So I guess the question is: What changes when I connect to the database from an applet which runs on a web page, in comparison to connecting to the database simply from a web page???
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42031
    
  64
In that case I'd insert a few strategically placed println statements in the code to see where it deviates from what you'd expect. E.g., is con null after the ds.getConnection() statement? Etc.
Greg Triant
Greenhorn

Joined: Nov 25, 2007
Posts: 28
This may sound a bit stupid but.... I already have.
When running the applet from the web page, I lose all of my out and err streams. Otherwise, when running the applet from the Java applet viewer, everything is printed on the console.

That's why I said earlier that I don't get any errors. Probably I just can't see them...

Other than that.... the way that I am trying to connect to the db from the applet is correct, right???
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
Hi Greg,

the output of the applet should go to the browser's Java Console. At least that's the experience I have. If it does not go there, check if you tried to re-direct System.out to some log file somewhere in your code. If so, you might well loose the output when running the applet in a browser, since applets are not allowed to create files in the general case.

Hope this helps,
Guido
Greg Triant
Greenhorn

Joined: Nov 25, 2007
Posts: 28
What do you mean by "the browser's console"?
Which object (if not System) would that be?
A line of code would be helpful...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42031
    
  64
No code is needed to use the Java Console - it's something the Java Plugin gives you for free, and to which it prints the out and err streams. If you're on Windows, look in the taskbar on the lower right of the screen for a little Java coffee cup icon and doubleclick it. You may need to go to the Java Plugin control panel to enable it if it's not there. Unix-ish operating systems also have that control panel.
Rachid Fydhan
Greenhorn

Joined: Nov 26, 2007
Posts: 8
Excuse me,

But how can this be useful?I mean succeeding to connect to db through an applet?is this required?

How about connecting to a servlet with HttpConnection ,using http get method (like ajax) and displaying the results returned from the server?

Ensuring ,separating and holding your db access by server will help you maintain the project easyly too.
And also using applets only for UI must be more economic to maintain..
[ November 26, 2007: Message edited by: Rachid Fydhan ]
Jignesh Patel
Ranch Hand

Joined: Nov 03, 2001
Posts: 626

Originally posted by Rachid Fydhan:
Excuse me,

But how can this be useful?I mean succeeding to connect to db through an applet?is this required?

How about connecting to a servlet with HttpConnection ,using http get method (like ajax) and displaying the results returned from the server?

Ensuring ,separating and holding your db access by server will help you maintain the project easyly too.
And also using applets only for UI must be more economic to maintain..

[ November 26, 2007: Message edited by: Rachid Fydhan ]


Well said. This is the way to go.
Greg Triant
Greenhorn

Joined: Nov 25, 2007
Posts: 28
Thanks for the advice...
However I am not getting from the db. I am INSERTING to it. And doing that from a servlet would require me to pass around a pretty huge java object.
Plus, I am not making something for public use. It will simply run on a local network....

BTW, problem still remains
Jignesh Patel
Ranch Hand

Joined: Nov 03, 2001
Posts: 626

let me understand?

Your problem is you are not able to see the browser console output? I am not sure when you say browser console?
or
Your problem is you are not able to insert the data by using applet and Jdbc?
Rachid Fydhan
Greenhorn

Joined: Nov 26, 2007
Posts: 8
Originally posted by Greg Triant:
Thanks for the advice...
However I am not getting from the db. I am INSERTING to it. And doing that from a servlet would require me to pass around a pretty huge java object.
Plus, I am not making something for public use. It will simply run on a local network....

BTW, problem still remains


Ok. I will try to tell my opinion as simple as i can.

Now you say "you will design it for use of in local network and Wont make the DB Access in APPLICATION SERVER..."

So what for an APPLICATION SERVER is used?

Only for displaying your applet?
As app. server sent applet to your win.exploerer,then is all job of app. server finishes?Is that all, it has to do? Nothing else?

I think this can be designed as standalone Swing application too.
No need to run an Application server.
Am i right?
Greg Triant
Greenhorn

Joined: Nov 25, 2007
Posts: 28
First of all, thanks for trying to help me. I really appreciate it...

My problem is that I can't insert in the db... (or access in general)
My problem is also that since I can't see the output stream, I can't tell what;s going wrong...

However the applet up to the point of db access works fine (except for the output printing...)
Rachid Fydhan
Greenhorn

Joined: Nov 26, 2007
Posts: 8
If you again will say me "no, i will try to Connect to db with a DataSource stored in jndi space" then you have to provide a jndi directory service for it.I mean JNDI space has remained at the server.As you sent Applet to browser...

So ,Here is you receipt;

1-You have to provide a directory service.(if you will store the datasource in jndi)
2-Although you have done 1. requirement,you wont connect to db immediately.

Because Applet Security resrictions will prevent you...To enable your applet to connect to db you will then have to make an operation called as "signing applet".

Does it look simple to manage?

I hope...
Greg Triant
Greenhorn

Joined: Nov 25, 2007
Posts: 28
Ouch!
Too many new stuff there for me...

Thanks anyway!
Greg Triant
Greenhorn

Joined: Nov 25, 2007
Posts: 28
Originally posted by Rachid Fydhan:


Ok. I will try to tell my opinion as simple as i can.

Now you say "you will design it for use of in local network and Wont make the DB Access in APPLICATION SERVER..."

So what for an APPLICATION SERVER is used?

Only for displaying your applet?
As app. server sent applet to your win.exploerer,then is all job of app. server finishes?Is that all, it has to do? Nothing else?

I think this can be designed as standalone Swing application too.
No need to run an Application server.
Am i right?


Sorry Rachid... I just noticed this one.


So.... YES YOU ARE RIGHT!!!
BUT, this thing which is 100% a stand alone Swing application that I have developed for my master, for some purpose we decided that it would be better empbedded in a web app. It makes sense too, but would be a lot to explain right now...
[ November 26, 2007: Message edited by: Greg Triant ]
Rachid Fydhan
Greenhorn

Joined: Nov 26, 2007
Posts: 8
If you dont use datasource.
Umm then i think you may not need to sign your applet...

As i saw it above ,your db and your server is at same machine right?
Opening connection to your host machine ,Connection will not stuck at Applet security restrictions.As its described in applet security.

Can you try something like this?



I got the code from here
[ November 26, 2007: Message edited by: Rachid Fydhan ]
Rachid Fydhan
Greenhorn

Joined: Nov 26, 2007
Posts: 8
Trying to connect trough Class.ForName and creating Connection by DriverManager might do that...
But one thing is that i am afraid of is Driver classes classpath may make issues to you too ...

How to???

Umm i dont know how to send separetely both Driver classes and applet to browser


sorry
Jignesh Patel
Ranch Hand

Joined: Nov 03, 2001
Posts: 626

Originally posted by Rachid Fydhan:
Trying to connect trough Class.ForName and creating Connection by DriverManager might do that...
But one thing is that i am afraid of is Driver classes classpath may make issues to you too ...

(


He can put his driver classes jar file in ext folder of the JDK so that he doesn't need to define class path. Remember JDK first loads bootstrap classes(java.lang) then extension classes and then go for classpath.
Jignesh Patel
Ranch Hand

Joined: Nov 03, 2001
Posts: 626

And as said previously by Richad, just use JFrame and make a stand alone application.

So that you can see the OS console to check what is happending behind.

Moreover one of the thread of ulf he mentioned following which might help you:

"It's considered bad design to use JDBC in applets, though, as it opens up the database to anyone connecting over the network. Within a closed network -say, inside of a company firewall- it may be OK, but it's still something to be thought through. If available, use a secure connection (like SSL) in that case. "
[ November 26, 2007: Message edited by: Jignesh Patel ]
Rachid Fydhan
Greenhorn

Joined: Nov 26, 2007
Posts: 8
Originally posted by Jignesh Patel:


He can put his driver classes jar file in ext folder of the JDK so that he doesn't need to define class path. Remember JDK first loads bootstrap classes(java.lang) then extension classes and then go for classpath.


JRE is enough, i think.

But that doesnt matter at all...As i said before "how to sent classes to client machine together with applet load"?

By doing that like yours, he probably has to copy jar and do the same thing for all applet users...
Jignesh Patel
Ranch Hand

Joined: Nov 03, 2001
Posts: 626

"how to sent classes to client machine together with applet load"?


Not able to understand where we are heading?

If you are thinking of passing database classes to other machines and then access database from those machine. So even assume that some how we are able to pass those classes but then accessing remote database will have firewall and protcol issues.
Greg Triant
Greenhorn

Joined: Nov 25, 2007
Posts: 28
The whole thing is pointless.... (for more than one reasons)
And I should have thought it through earlier.
You can't just insert in the servers db, what you generated on the client and all that from the client.
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 436
Hi,

I don't know exactly what problem you are facing? But even now you have a way to come out of this one. After creating "big" object on the client, send it to the server, using ObjectOutputStream, where you have a Servlet that takes ObjectInputStream. Now close stream on both ends. You need to have a copy of class of that object on the server too otherwise your servlet will have problem converting it back to object of that type again.

Now do what you want with that object.

Hope you find this helpful.


Maki Jav
[ November 29, 2007: Message edited by: Maki Jav ]

Help gets you when you need it!
 
GeeCON Prague 2014
 
subject: db access through applet