How to access the sql database in applet?

How to access the sql database in applet?Please help me.
[70 byte] By [SVPRMa] at [2007-11-27 5:40:06]
# 1

import java.applet.*;

import java.awl.*;

import java.sql.*;

//other packages

public class jdb extends Applet

{

Connection con;

Statement stmt;

String name="drvijay";

String phoneno="9842088860";

public void init(){}

pubilc void stop(){}

public void destroy(){}

public void start()

{

call();

}

public void call()

{

try{

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

conn=DriverManager.getConnection("Jdbc:Odbc:emp","sa","");

stmt=conn.createStatement();

stmt.executeUpdate("insert into empdetails(name,phoneno) values ('"+name + "',''"+ phoneno +"' )" );

}catch(SQLException e)

{

System.out.println("error"+e);

System.exit(0);

}

}

};

//<applet code="jdb" height="200" width="200"> </applet>

/*

u this jdbc statement in any of the method..

*/

drvijayy2k2a at 2007-7-12 15:16:14 > top of Java-index,Java Essentials,New To Java...
# 2

@ drvijayy2k2

I dont know how to connect to a database from an applet yet but I know there's no point making an applet which requires a odbc connection.

If an odbc connection is required, then its better to make it an application.

The purpose of making an applet with database connectivity is that it can de deployed to the net and the end user doesnt have to worry about creating a Data Source Name.

ArcherKinga at 2007-7-12 15:16:14 > top of Java-index,Java Essentials,New To Java...
# 3
I think it's not a good way to access a sql database in applet.Simple a applet require some data to showing which can be send by jsp using applet tag parameters easily.So u access sql database in your application and get data on jsp page is better idea.
S_Singha at 2007-7-12 15:16:14 > top of Java-index,Java Essentials,New To Java...
# 4

I am modify your program.

Like below:

import java.applet.*;

import java.awt.*;

import java.sql.*;

//other packages

public class jdb extends Applet

{

Connection con;

Statement stmt;

public void start()

{

call();

}

public void call()

{

try

{

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

con=DriverManager.getConnection("Jdbc:Odbc:emp","system","manager");

stmt=con.createStatement();

stmt.executeUpdate("insert into stu1 values ('OK')");

}

catch(SQLException e)

{

System.out.println("error"+e);

}

}

}

/*

<html>

<body>

<applet code="jdb" height="200" width="200"> </applet>

</body>

</html>

*/

I am complie the program it say like below:

jdb.java.19:unreported exception java.lang.class not foundException:must be caugth or declared to be thrown

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

SVPRMa at 2007-7-12 15:16:14 > top of Java-index,Java Essentials,New To Java...
# 5

> con=DriverManager.getConnection("Jdbc:Odbc:emp","system

> ","manager");

This line should be

con=DriverManager.getConnection("jdbc:odbc:emp","system","manager");

notice the caps.(java is Case sensitive)

The Class.forName() throws ClassNotFoundException

Also note that you have mentioned the SQLException only. You need to mention one more catch statement

catch(Exception e)

{

e.printStackTrace();

}

As I said before using a jdbc-odbc is not recommented if you plan to deploy this on the web.

Regards

Message was edited by:

ArcherKing

ArcherKinga at 2007-7-12 15:16:14 > top of Java-index,Java Essentials,New To Java...
# 6
Thanks ArcherKing.That error are corrected.I am run the program through appletviewer.It say:errorJava.security.AccessControlException: access denied(java.lang.RuningTimePermission accessClassInPackage sun.jdbc.odbc)
SVPRMa at 2007-7-12 15:16:14 > top of Java-index,Java Essentials,New To Java...
# 7

Oh I've forgot to mention that you have to set permissions.

I'm not at my home computer right now, so I cant give you exact procedure.

I can guide you a bit though.

run the policytool from the command prompt and set the permissions.

Can someone else help him out?

follow this link, hope it helps

http://forum.java.sun.com/thread.jspa?threadID=172396&tstart=0

Message was edited by:

ArcherKing

ArcherKinga at 2007-7-12 15:16:14 > top of Java-index,Java Essentials,New To Java...