connecting an applet to a MS access database

please can someone tell me how i can connect an applet to a MS access database. each time i try i get a security exception, is there a way around this.
[165 byte] By [kingfahad786] at [2007-9-26 1:19:33]
# 1
hi,just have a look at my site www.lochmann.de ==> downloads.there you'll find examples.andi
BCBCBC at 2007-6-29 0:52:12 > top of Java-index,Archived Forums,Java Programming...
# 2
its all in german, do u know if this works at all. i am using MS access and IE and want to run it using appletviewer i still get error when i try to connect to the database, do u know a way around this, thnks
kingfahad786 at 2007-6-29 0:52:12 > top of Java-index,Archived Forums,Java Programming...
# 3
can u tell me how i can get my pogram to work like yours. i have an applet which uses Jswing components, put at the moment i just want to get it working in appletvierwer. however each time i try andconnect to the database i get a secuity exception, plz can u help
kingfahad786 at 2007-6-29 0:52:12 > top of Java-index,Archived Forums,Java Programming...
# 4
when i run this in a broser it works, however when i run it using appletviewer i still get an error do u know y?
kingfahad786 at 2007-6-29 0:52:12 > top of Java-index,Archived Forums,Java Programming...
# 5

Hi,

If you want to run it in the appletviewer, create a file named applet.policy and paste the following in that file:

grant codeBase "file:/c:/my_applet_folder/*" {

permission java.security.AllPermission;

};

Of course, replace the folder with the location of your applet.

Then copy the file in the same folder as the applet.

Then, run the appletviewer as follows:

appletviewer -J-Djava.security.manager -J-Djava.security.policy==applet.policy your_html_page.html

If you want to run the applet in the browser, you'll need to disable the sandbox in your browser settings or you'll need to sign the applet.

If your database is on the server, then no security actions are required and you'll need to use the RmiJdbcDriver.

Hope this helps,

Kurt.

leukbr at 2007-6-29 0:52:12 > top of Java-index,Archived Forums,Java Programming...
# 6

i have tried what u have said but now i dont even get the exception. can u help?

this is the code i am using to connect to the database from my applet:

public void addtodatab() {

try {

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

url = "jdbc:odbc:Option1a";

Connection connect = DriverManager.getConnection(url);

System.out.println("Connection successful");

}

catch (ClassNotFoundException cnfex) {

//process ClassNotFoundExceptions here

cnfex.printStackTrace();

System.out.println("Connection class unsuccessful\n"+ cnfex.toString() );

}

catch (SQLException sqlex) {

//process SQLExceptions here

sqlex.printStackTrace();

System.out.println("Connection sql unsuccessful\n" + sqlex.toString() );

}

catch (Exception ex) {

//process remaining Exceptions here

ex.printStackTrace();

System.out.println("Connection remaining unsuccessful\n" + ex.toString() );

}

}

and this is my pplet.policy file

grant codeBase "file:/c:/mydocu~1/login/*" {permission java.security.AllPermission;};

where im i going wrong!!!

kingfahad786 at 2007-6-29 0:52:12 > top of Java-index,Archived Forums,Java Programming...
# 7
i think my policy file was wrong, so i changed it togrant codeBase "file:/c:/mydocu~1/*" {permission java.security.AllPermission;};but i now get my original error of accesscontrolexception: access denied
kingfahad786 at 2007-6-29 0:52:12 > top of Java-index,Archived Forums,Java Programming...
# 8

hi,

you said the comnments of my code are in german - there are 2 samples, one of those has as well english comments (and a readme file in german/english).

i don't know why you have exceptions, and there is no use at all to edit your policy file - this is jdbc-odbc-bridge, so there is no need to edit the policy file.

if you still have problems, contact me again.

andi

BCBCBC at 2007-6-29 0:52:12 > top of Java-index,Archived Forums,Java Programming...
# 9
Hi,There is a security issue when you use the jdbc-odbc bridge (read http://java.sun.com/products/jdbc/faq.html#3 )Try runningappletviewer -nosecurity your_page.html
leukbr at 2007-6-29 0:52:12 > top of Java-index,Archived Forums,Java Programming...
# 10
Ok, the jdbc-odbc bridge can normally be used in the appletviewer without specifying the policy file.Can you provide the complete error you get?
leukbr at 2007-6-29 0:52:12 > top of Java-index,Archived Forums,Java Programming...
# 11

i have read that link u told me about and it says an applet can be run in appletviewer with the use of a JDBC-ODBC bridge b/c it assumes it is trusted. however i still get this error each time i want to conecct to the database.

i have a list of errors that go like:

at java.awt.EventDispatchThread.run(EventDispatchThread.java:85)

and i also get:

Connection remaining unsuccessful

java.security.AccessControlException: access denied(java.lang.RuntimePermission accessClassInPackage.sun.jdbc.odbc)

what do u htink the problem is. i have used the same code t oaccess the database as above. thanks for ur time

kingfahad786 at 2007-6-29 0:52:12 > top of Java-index,Archived Forums,Java Programming...
# 12
sorry about that, i did find yr code inenglish. however when i run it using appletviewer it doesnt work but when i run it in IE it works can u tell mey? it says in yr code that it should work in appletviweer. i am using JDK1.3 so i would have thought it would support it
kingfahad786 at 2007-6-29 0:52:12 > top of Java-index,Archived Forums,Java Programming...
# 13
Then you'll have to use the policy file anyway. You've said in a previous posting "now i dont even get the exception". What did you mean with that? Propably the security settings were working fine.
leukbr at 2007-6-29 0:52:12 > top of Java-index,Archived Forums,Java Programming...
# 14

sorry, what i meant by not gettin the exception was that the applet wasnt connecting to the database at all, i think that was b/c my policy file was wrong. i have a check in by code that when i connect to the database a message is printed to the screen "Connection Successful" but that was happening at all, so there was something wrong.i changed it back but got that same security exception. what do i do with the policy file now or what commend do i type in at the commend prompt

kingfahad786 at 2007-6-29 0:52:12 > top of Java-index,Archived Forums,Java Programming...
# 15

can I ask a question?

I don't know much about DBs from applets... ..

but I know a little about security in applets.. .

if the applet resides on the same server as your applet

then you should be able to connect no problems, right?

alternativly security restrictions changed as of 1.3.0_01

so ... .. that could also be the problem.

last but not least if all else fails try droping the class or

jar into the extensions folder , and it will run as trusted.

slinqi at 2007-6-30 21:34:34 > top of Java-index,Archived Forums,Java Programming...
# 16

hi slinqi,

let me just tell u the situation i am in. i have created an applet using swing components on my own pc. i have just tried to run it using appltviewer, b/c i was told IE or netscape doesnt support swing. but anyway, i need my applet ot access a database and at the moment the only viable solution is to use MS access 2000.

i have no server or anything whih the applet is running on or the database is stored on. everything is running on my own pc.. however i know that b/c of the security restrictions placed on applets i wont be able to access the database. but i read somewhere that if the applet was trusted it can access a database with a JDBC-ODBC bridge using appletviewer but each time i run it i get that exception, everuything runs in an application its just that when i transfer it to the applt i get all these security exceptions. i just need a way of stooping that so i can get the applet connected to the database over a JDBC_ODBC bridge. if there is another of connecting i would really like to know.

i heard that its possible with the use of servlets, but i dont know where to start with tthat, i cant find some examples of where applets that servlets to access databases

thansk for yr time anyway

its much apprecaited

kingfahad786 at 2007-6-30 21:34:34 > top of Java-index,Archived Forums,Java Programming...
# 17
Instead of Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); try one of these:Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance(); orDriverManager.registerDriver (sun.jdbc.odbc.JdbcOdbcDriver); and see what happens
leukbr at 2007-6-30 21:34:34 > top of Java-index,Archived Forums,Java Programming...
# 18

when i use the first command

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

it still gives me the original error about "accesscontrolexception"

when i use the second command i get error saying:

cannot reslove symbol

symbol: class odbc

location: package jdbc

DriverManager.registerDriver(sun.jdbc.odbc.JdbcOdbcDriver);

there error occurs at the second dot ie (sun.jdbc.(here)odbc.JdbcOdbcDriver)

how can i resolve this error

also im i writing the code correctly

try {

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

DriverManager.registerDriver(sun.jdbc.odbc.JdbcOdbcDriver);

url = "jdbc:odbc:Option1a";

Connection connect = DriverManager.getConnection(url);

System.out.println("Connection successful");

}

catch

kingfahad786 at 2007-6-30 21:34:34 > top of Java-index,Archived Forums,Java Programming...
# 19

hi again,

you said my sample runs in ie, but not with appletviewer? so how i told before: there are 2 samples - have a look at the other one.

the problem is, in one of these samples i use the driver for running in ie (com.xxx.xxx.xx... something like that).

in the other example i check where the applet is running:

in netscape and appletviewer you have to use the sun.xxx.xxx.xxx. driver !!!.

look at the examples again, or contact me once more.

andi

BCBCBC at 2007-6-30 21:34:34 > top of Java-index,Archived Forums,Java Programming...
# 20

Hi,

I've tried the examples and I've got exactly the same error. I've been able to fix it with -again- the policy file.

Please make sure the policy file is in the current working directory and it contains (internally) the right path. Also check wether the command line you're executing is ok.

If it doesn't run, please post all details (directory of class files, html page, ...).

The applet in \Entwicklung\Tests\JDBCTest\classes in the jdbctest.zip is working fine.

leukbr at 2007-6-30 21:34:34 > top of Java-index,Archived Forums,Java Programming...
# 21

i have now ran both JDBCTest.java files (located in the src directory) in appletviewer and they both give me the same problem. they both give me the DB not connected message, which is located in the start() method.

and now for some reason when i run either program just by double clikcing the html file it says that i am running in IE but it still says DB not connected.

is it possible to tell me which i have to use or implement in order to get my applet connected to a DB via the JDBC-ODBC bridge.

also i hav looked at yr codeee for both samples, and they both use the same code, can u tell me whether i am looking at the correct code plz

thanks again

kingfahad786 at 2007-6-30 21:34:34 > top of Java-index,Archived Forums,Java Programming...
# 22

Hi andi,

im sorry the JDBCTest.html file in the enwichlung directory is working when i double click it and run it in IE but the one in the privat directory doesnt work, can u tell me why?

also the hen i run either in appletviewer they dont work, is it possible for u tell me which part actually does the important part and connect to the databae. is it anything to do with the fatc that i am using JDK1.3 at all, b/c i think that hs some added security features!!!!

thanks

kingfahad786 at 2007-6-30 21:34:34 > top of Java-index,Archived Forums,Java Programming...
# 23

this is the part that connects to the database in my code:

public void addtodatab() {

try {

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

url = "jdbc:odbc:Option1a";

Connection connect = DriverManager.getConnection(url);

System.out.println("Connection successful");

}

catch (ClassNotFoundException cnfex) {

//process ClassNotFoundExceptions here

cnfex.printStackTrace();

System.out.println("Connection class unsuccessful\n"+ cnfex.toString() );

}

catch (SQLException sqlex) {

//process SQLExceptions here

sqlex.printStackTrace();

System.out.println("Connection sql unsuccessful\n" + sqlex.toString() );

}

catch (Exception ex) {

//process remaining Exceptions here

ex.printStackTrace();// + ex.toString()

System.out.println("Connection unsuccessful\n" );

}

}

(does it matter if i print to the statdardd input/output even though i am using an applet, im just doing that to check if everything is working, tell me if there is a problem)

the policy contains this code:

grant codeBase "file:/c:/mydocu~1/*" {permission java.security.AllPermission;};

i am working in this directory:

C:\mydocu~1\login> which contains all my class files/database/and policy file.

but im not sure what to type at the command promtpt

thanks for yr help

kingfahad786 at 2007-6-30 21:34:34 > top of Java-index,Archived Forums,Java Programming...
# 24

this is the part that connects to the database in my code:

public void addtodatab() {

try {

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

url = "jdbc:odbc:Option1a";

Connection connect = DriverManager.getConnection(url);

System.out.println("Connection successful");

}

catch (ClassNotFoundException cnfex) {

//process ClassNotFoundExceptions here

cnfex.printStackTrace();

System.out.println("Connection class unsuccessful\n"+ cnfex.toString() );

}

catch (SQLException sqlex) {

//process SQLExceptions here

sqlex.printStackTrace();

System.out.println("Connection sql unsuccessful\n" + sqlex.toString() );

}

catch (Exception ex) {

//process remaining Exceptions here

ex.printStackTrace();// + ex.toString()

System.out.println("Connection unsuccessful\n" );

}

}

(does it matter if i print to the statdardd input/output even though i am using an applet, im just doing that to check if everything is working, tell me if there is a problem)

the policy contains this code:

grant codeBase "file:/c:/mydocu~1/*" {permission java.security.AllPermission;};

i am working in this directory:

C:\mydocu~1\login> which contains all my class files/database/and policy file.

my html page contains this code:

<HTML>

<HEAD>

<TITLE>Startingframe</TITLE>

</HEAD>

<BODY>

<P>

<APPLET CODE="Startingframe.class" WIDTH=392 HEIGHT=232>

</APPLET>

</BODY>

</HTML>

but im not sure what to type at the command promtpt

thanks for yr help

kingfahad786 at 2007-6-30 21:34:34 > top of Java-index,Archived Forums,Java Programming...
# 25

Ok the policy file is now the problem. Replace the contents with the following to bypass the restrictions for all files:

grant codeBase "file:*" {

permission java.security.AllPermission;

};

Then run the following command line:

C:\mydocu~1\login>appletviewer J-Djava.security.manager -J-Djava.security.policy==applet.policy JDBCTest.html

leukbr at 2007-6-30 21:34:34 > top of Java-index,Archived Forums,Java Programming...
# 26
I've forgotten to add a minus sign:C:\mydocu~1\login>appletviewer -J-Djava.security.manager -J-Djava.security.policy==applet.policy JDBCTest.html
leukbr at 2007-6-30 21:34:34 > top of Java-index,Archived Forums,Java Programming...
# 27
I've forgotten to add a minus sign:C:\mydocu~1\login>appletviewer -J-Djava.security.manager -J-Djava.security.policy==applet.policy JDBCTest.html
leukbr at 2007-6-30 21:34:34 > top of Java-index,Archived Forums,Java Programming...
# 28
hi yeah it now works thanks for yr help, but i would like to know how i could use some of this code to get my applet to connect to a database. would parts do i need plz can u help
kingfahad786 at 2007-6-30 21:34:34 > top of Java-index,Archived Forums,Java Programming...
# 29
Instead of going through all that crap to get an applet to work, why don't you just change your program to be an application? No security issues, no hassle.
DrClap at 2007-6-30 21:34:34 > top of Java-index,Archived Forums,Java Programming...
# 30

here is a disservice to the forum, it was hacked at Starbucks while waiting...well I did re-learn somethings, hope it is useful for someone.

see lines 27 to 53 for 'commentary'

1import java.util.*;

2import java.net.URL;

3import java.sql.*;

4public class Access extends DatabaseUtility {

5public Access() {

6initialize();

7}

8

9public void initialize() {

10//System.out.println( "Access initialize" );

11urlDB = "JDBC:odbc:cars";

12driverDB = "sun.jdbc.odbc.JdbcOdbcDriver";

13try {

14 Class.forName( driverDB );

15} catch( Exception e ) {

16 //System.out.println(e.getMessage());

17 e.printStackTrace();

18}

19}

20}

21import java.awt.*;

22import java.awt.event.*;

23import java.applet.*;

24import java.net.*;

25import java.sql.*;

26/*

27author: Ron

28email: rmlchan@yahoo.com

29database name cars

30ID1 autonumber

31name text

32description text

33keytool -genkey -alias aSignKey -keyalg RSA

34

35

36

37

38

39

40keytool -export -alias aSignKey -file aSignKey.cer

41in Windows, double click on aSignKey.cer and install

42(in linux, import)

43javac *.java

44jar -cvf aJDBC.jar *.class

45jarsigner aJDBC.jar aSignKey

46java console output:

47test

48DS exSQL select * from job url: JDBC:odbc:cars

49con: sun.jdbc.odbc.JdbcOdbcConnection@2bcde

50stmt: sun.jdbc.odbc.JdbcOdbcStatement@bbf9e

51sun.jdbc.odbc.JdbcOdbcResultSet@513d61

52rs: first

53rs: second

54*/

55public class AppletJDBC extends Applet implements Runnable

56{

57Thread timer;

58public void init()

59{

60 Label lb = new Label( "hello" );

61 add( lb );

62 resize(300,300);

63 test();

64}

65public Panel getPanel()

66{

67 Panel p = new Panel();

68 Label lb = new Label( "hello" );

69 p.add( lb );

70 p.resize(300,300);

71 return p;

72}

73private void test()

74{

75 System.out.println( "test" );

76 IDatabaseUtility db = new Access();//Postgres();

77 String sql = "select * from job";

78 try

79 {

80ResultSet rs = db.executeSQL( sql );

81System.out.println( rs );

82

83while( rs.next() )

84{

85System.out.println( "rs: " + rs.getString( 2 ) );

86}

87 }

88 catch( Exception e )

89 {

90e.printStackTrace();

91 }

92}

93public void start() {

94 timer = new Thread(this);

95 timer.start();

96}

97public void stop() {

98 timer = null;

99}

100public void run() {

101 Thread me = Thread.currentThread();

102 while (timer == me) {

103try {

104Thread.currentThread().sleep(100);

105} catch (InterruptedException e) {

106}

107repaint();

108 }

109}

110public String getAppletInfo() {

111 return "Title: An Applet JDBC example \nAuthor: Ron \nAn Applet JDBC example.";

112}

113

114public String[][] getParameterInfo() {

115 String[][] info = {

116{"bgcolor", "hexadecimal RGB number", "The background color. Default is the color of your browser."},

117{"fgcolor1", "hexadecimal RGB number", "Default is blue."},

118{"fgcolor2", "hexadecimal RGB number", "Default is dark gray."}

119 };

120 return info;

121}

122public static void main( String[] args )

123{

124 Frame f = new Frame();

125 AppletJDBC aJDBC = new AppletJDBC();

126 Panel p = aJDBC.getPanel();

127 aJDBC.test();

128 f.add( p );

129 f.pack();

130 f.show();

131}

132

133}

134import java.util.*;

135import java.io.*;

136import java.net.URL;

137import java.sql.*;

138public abstract class DatabaseUtility implements IDatabaseUtility {

139protected static String urlDB = "";

140protected static String driverDB = "";

141protected Connection con = null;

142protected Statement stmt =null;

143protected ResultSet rs = null;

144protected int irs = 0;

145protected PreparedStatement ps = null;

146

147public DatabaseUtility() {

148}

149

150public int getStatus()

151{

152 return irs;

153}

154public PreparedStatement prepareStatement( String s )

155{

156 try

157{

158con = DriverManager.getConnection( urlDB, "", "" );

159ps = con.prepareStatement( s );

160}

161 catch( Exception e )

162{

163e.printStackTrace();

164}

165 return ps;

166}

167public ResultSet executeSQL( String sql ) throws Exception {

168System.out.println( "DS exSQL " + sql + " url: " + urlDB );

169try {

170 rs = null;

171 con = DriverManager.getConnection( urlDB, "", "" );

172 System.out.println( "con: " + con );

173 stmt = con.createStatement();

174 System.out.println( "stmt: " + stmt );

175 if( sql.startsWith( "select" ) )

176rs = stmt.executeQuery( sql );

177 else if( sql.startsWith( "delete" ) ||

178sql.startsWith( "insert" ) ) {

179irs = stmt.executeUpdate( sql );

180 }

181 else if( sql.startsWith( "update" ) ) {

182irs = stmt.executeUpdate( sql );

183 }

184}

185catch( Exception e ) {

186 e.printStackTrace();

187 throw e;

188}

189return rs;

190}

191

192}

193import java.util.*;

194import java.io.*;

195import java.net.URL;

196import java.sql.*;

197public interface IDatabaseUtility {

198

199public ResultSet executeSQL( String sql ) throws Exception;

200public int getStatus();

201public PreparedStatement prepareStatement( String s );

202}

203<HTML>

204<head>

205<title>JDBC Applet</title>

206</head>

207<body>

208<h1>JDBC Applet</h1>

209<p>

210<!--"CONVERTED_APPLET"-->

211<!-- CONVERTER VERSION 1.3 -->

212<OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"

213WIDTH = 170 HEIGHT = 150 codebase="http://java.sun.com/products/plugin/1.3/jinstall-13-win32.cab#Version=1,3,0,0">

214<PARAM NAME = CODE VALUE = "AppletJDBC.class" >

215<PARAM NAME = CODEBASE VALUE = "./" >

216<PARAM NAME = ARCHIVE VALUE = "aJDBC.jar, postgresql.jar" >

217<PARAM NAME="type" VALUE="application/x-java-applet;version=1.3">

218<PARAM NAME="scriptable" VALUE="false">

219<COMMENT>

220<EMBED type="application/x-java-applet;version=1.3" CODE = "AppletJDBC.class" CODEBASE = "./" ARCHIVE = "aJDBC.jar, postgresql.jar" WIDTH = 170 HEIGHT = 150 scriptable=false pluginspage="http://java.sun.com/products/plugin/1.3/plugin-install.html"><NOEMBED></COMMENT>

221alt="Your browser understands the <APPLET> tag but isn't running the applet, for some reason."

222Your browser is completely ignoring the <APPLET> tag!

223</NOEMBED></EMBED>

224</OBJECT>

225<!--

226<APPLET CODE = "AppletJDBC.class" CODEBASE = "./" ARCHIVE = "aJDBC.jar, postgresql.jar" WIDTH = 170 HEIGHT = 150>

227alt="Your browser understands the <APPLET> tag but isn't running the applet, for some reason."

228Your browser is completely ignoring the <APPLET> tag!

229</APPLET>

230-->

231<!--"END_CONVERTED_APPLET"-->

232</body>

233</HTML>

mchan0 at 2007-6-30 21:34:35 > top of Java-index,Archived Forums,Java Programming...
# 31

hi fahad,

i am not sure what happend....

day by day lot's of coders download my examples...until now no problems occured.

the all downloaded it, and it worked.

are you sure you have used my samples in the right way?

if there are still any questions or problems contact me at andi@lochmann.de and i will go through the code with you step by step.

andi

BCBCBC at 2007-6-30 21:34:35 > top of Java-index,Archived Forums,Java Programming...
# 32

hi again fahad.....

ok, just to be sure i downloaded my own example from my site (the second one !)

i downloaded it - created an odbc dsn called 'Fred' to the database i want to connect (there is a mdb with the sample!), startet my applet in the appletviewer ==> perfect!

then i startet it in the ie ==> perfect.

may be you tried the 1st example which is a bit more difficult as far as i remember.

as i told you before: contact me if there are still any problems!!!

andi

BCBCBC at 2007-6-30 21:34:35 > top of Java-index,Archived Forums,Java Programming...
# 33
kingfahad786 , considering noone has added to this in a few days can I assume you have managed to get it running as an applet?
slinqi at 2007-6-30 21:34:36 > top of Java-index,Archived Forums,Java Programming...
# 34

slinqi,

i am sorry to say but i am still having problems, from what i have read u cannot access a MS access databse using an applet via the jdbc-odbc bridge, so therefore i am forced to consider other options such as servlets or ANYTHING!!!! but if u have any ideas plz tell me thanks

kingfahad786 at 2007-6-30 21:34:36 > top of Java-index,Archived Forums,Java Programming...
# 35
but I showed you an applet using the bridge to access above.
mchan0 at 2007-6-30 21:34:36 > top of Java-index,Archived Forums,Java Programming...
# 36
i have tried to go through that code but i am finding it difficult to understand what is going on, especially that html code, if u could explain plz email me or post it here, (king_fahad60@hotmail.com) thansk
kingfahad786 at 2007-6-30 21:34:36 > top of Java-index,Archived Forums,Java Programming...
# 37
http://java.sun.com/docs/books/tutorial/jdbc/basics/example-1dot2/OutputApplet.java
mchan0 at 2007-6-30 21:34:36 > top of Java-index,Archived Forums,Java Programming...