getting error while connecting to database
while i try to connect to the database i'm getting the foollowing error
java.security.AccessControlException: access denied (java.lang.RuntimePermission accessClassInPackage.sun.jdbc.odbc)
the part of my code which is used to connect to the database is as follows
/*
<applet code="Part1" width=500 height=500>
</applet>
*/
public class Part1 extends Applet implements ActionListener
{
public void init()
{
try
{
//Class.forName("oracle.jdbc.driver.OracleDriver");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{
System.out.println("EXCEPTION--"+e.getMessage());
System.out.println("DRIVER NOT FOUND");
}
Connection conn=null;
try
{
//Making the database connection
conn= DriverManager.getConnection("jdbc:odbc:xyz");
}
catch(Exception e)
{
System.out.println("EXCEPTION IN CONNECTION::"+e.getMessage());
System.exit(-1);
}
# 1
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.
(http://java.sun.com/sfaq/#socket)
Here an example:
http://www.oracle.com/technology/oramag/code/tips2004/031504.html
# 2
can you please suggest some solution to it
# 3
Actually this isn't even a connection problem per se - you're just plain not allowed to access that package (the JDBC/ODBC bridge) from an applet.
Applets in browsers aren't allowed to do things that could harm the machine they're running on for (I hope) obvious reasons. Access to the JDBC/ODBC bridge could be used to do all sorts of nasty things.
# 4
so according to you what should i do to resolve all this
# 5
Actually Applets are client side programs same like any of Javascript code snippet. If you want to do any database related operations better do in a client bean or JSP itself. However it is not recommended to do database related operations in a JSP file.
# 6
> so according to you what should i do to resolve all
> this
If your application is intended for a very small group of people (e.g. internal consumption within a company) you may be able to adjust the browser settings to permit your applet to carry out these operations.
Alternatively you could replace the Applet with a stand-alone Java Swing Application.
If your application is intended to be accessed by large numbers of people you could use a Server side technology to present and manipulate the data (Servlets and JSPs).
If you want a dynamic client-side application with access to the server-side data, then you could find a pure-Java JDBC driver that can talk directly to your back-end database, but this is likely to be problematic for other reasons; not least of which being firewall permissions for the connection and the limited availability of suitable drivers that can run in the constrained environment of an Applet.
Finally you could consider using an alternative communications medium such as SOAP, JSON, XML, or even CSV to communicate with a server-side Servlet that in turn talks to the database locally. This last is probably the most practical option.
I can't give you specific advice without knowing what you're actually trying to achieve.
# 7
If your application is intended for a very small group of people (e.g. internal consumption within a company) you may be able to adjust the browser settings to permit your applet to carry out these operations.
how can i adjust the browser settings to permit my applet to carry out the operation
can you please throw some light on it
# 8
applet should not - in theory look at databases directly - it is a bad design strategy. Make sure that the jdbc driver and other jar files required are packaged in the applet jar itself. The best way to use applets (if u have to use them) is to have a servlet hosted in a web container and have the applet connect to the servlet. Examples are there for this in any basic servlet textbook
think u mast use JSP to access database detail on client side .
it essy than doing in applet .
# 9
con = DriverManager.getConnection
("jdbc:oracle:thin:@localhost:1521:ora9202","scott","tiger");
# 10
public class Applet1 extends JApplet {
private JButton DbConnect = new JButton();
private JTextArea empValues = new JTextArea();
private Connection con;
private JLabel jLabel1 = new JLabel();
public Applet1() {
}
public void init() {
try {
jbInit();
} catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(null);
DbConnect.setText("Click to Connect to DB");
DbConnect.setBounds(new Rectangle(90, 225, 175, 40));
DbConnect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
connectToDB(e);
}
});
empValues.setBounds(new Rectangle(40, 50, 310, 150));
jLabel1.setText("Emp Records :");
jLabel1.setBounds(new Rectangle(45, 20, 155, 20));
this.getContentPane().add(jLabel1, null);
this.getContentPane().add(empValues, null);
this.getContentPane().add(DbConnect, null);
}
private void connectToDB(ActionEvent e) {
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
con = DriverManager.getConnection
("jdbc:oracle:thin:@localhost:1521:ora9202","scott","tiger");
empValues.setText("Connected to the Database.
Fetching Values from DEPT Tables.\n");
fetchValues();
} catch (SQLException ex){
System.out.println("Connection Error = " + ex.toString());
}
}
# 11
Sandeep, please don't give advice without thinking about it. For example, you have no reason to believe that the OP is using Oracle.
# 12
> how can i adjust the browser settings to permit my
> applet to carry out the operation
> can you please throw some light on it
http://www.google.com/search?q=Applet+permissions