Sunday, February 19, 2012

Connecting jdbc driver from an applet

Hi,

I've found problems when executing

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

I get a java.lang.ClassNotFoundException, and I have done this steps:

1o I got the driver from msdn web page

2o I modified the classpath in Windows XP adding the path to the sqljdbc.jar, testing if there was any problems with blanks in the path, I tried to put the .jar file in a more simple folder, but it didn't work.

Perhaps I've to do something whith my policy.java or so archives to let applets do something about this but I have no idea.

Can someone help me? Thanks!

Hi there,

What IDE are you using (e.g. Eclipse, IntelliJ)? Are you coding and then running your app through the IDE?

If you are running your app through the IDE, something to be aware of is that most Java IDE's define their own classpath and by default don't use the CLASSPATH environment variable you set up in Windows.

To fix your problem, you need to configure the classpath used by your IDE so that it includes the sqljdbc.jar file.

If you're not sure how to do this then consult the doco that came with the IDE but if it's no help give us a buzz and we can probably work something out (I'm only familiar with Eclipse and IntelliJ though).

Hope that helps a bit, but sorry if it doesn't.
|||

Hi and thanks for your time,

I'm not using any kind of IDE. I've used to program with Eclipse but I'm not allow to do that now. With Eclipse I had add jar files other times without problems.I have to ask for it and I had this as my last solution.

So I'm doing it with a simple text editor and my JVM compiling everytime I want to. Then I open my applet with my internet explorer and that's when I get my error.

I'm looking for a solution with this conditions. Thanks

|||Hi there
I'm quite sure my problem is CLASSPATH, because I've found some examples where is tested my line code error and, in the catch block there is always a report like "You haven't registered SQL Server 2000 in the classpath" or so.
In my case, I have just add .jar file containing the driver, to the CLASSPATH variable. Does is it correct?

Thanks!
|||Yes, if the sqljdbc.jar file is not in the classpath then it needs to be there (if that's what you're asking).

So your classpath variable should be something like:

%classpath%;C:\JDBC\sqljdbc.jar

|||I've found another solution, and it seems to be working ok. Instead of using directly a new JDBC driver, I'm using the JDBC-ODBC generic bridge, so in the calling I should write

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")

The only thing I' have to take in account is that I must give permissions to the applet in order to be able to get that driver and use the classes. I can do that if I modify my java.policy file.
|||

Hmmm, this may be a year too late, but for the sake of others running into the same problem...

First, I've had better luck getting the sqljdbc driver to work with SQL Server 2005, using

DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());

rather than

Class.forName(...)

Second, I couldn't get this to work until I put an "ARCHIVE" parameter in the tag, referring specifically to the sqljdbc.jar:

<APPLET CODE="jdbcTest.class" ARCHIVE=sqljdbc.jar>

Next, I ran into a problem with running the applet from a web server that is *separate* from a remote database server (whose IP and port, below, I have changed in the interest of security):

Exception in thread "Thread-73" java.security.AccessControlException: access denied (java.net.SocketPermission 123.235.12.43:1234 connect,resolve)

As mentioned, the solution to this is to create a policy file, in this example named "jdbcTest.pol", that gives the applet the necessary permissions:

grant
{
permission java.net.SocketPermission "123.235.12.43:1234", "connect,resolve";
};

The problem with this seems to be that although it works when run under appletviewer, by specifying the policy file:

appletviewer -J-Djava.security.policy=jdbcTest.pol jdbcTest.html

It DOES NOT work when run in a browser! There does not seem to be a way to specify the policy file (for instance, as a parameter in the <APPLET> tag). A possible solution to this might be referred to in this snippet from the Java security FAQ:

"How can an applet open a network connection to a computer on the internet?

Applets are not allowed to open network connections to any computer, except for the host that provided the .class files. This is either the host where the html page came from, or the host specified in the codebase parameter in the applet tag, with codebase taking precendence."

So, it sounds like it might be possible to set the CODEBASE parameter to point to the remote database server (point to what, exactly, though?) or to have a page on the web server load the jdbcTest.class from a location on the remote database server. But I haven't got any of this to work yet! I'll post something here if I do. Otherwise, I'd be happy to hear from anyone who has got this to work.

The question is: Is it possible, given the security restrictions, to connect to a SQL Server running on a remote host from an applet running on a separate web server? If so, how?

No comments:

Post a Comment