• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java applet-oracle database connection

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all
can anyone provide me some simple Applet code for querying the
oracle database. please do specify classes to be included in the classspath
in case of applets.
i have already connected my application with oracle database but i m unable to connect my applet with the database.
i will be greatly thankful to you.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

The code for connecting from an applet is the same as for an application. An important difference is that applets can only connect to the host where they were served from (i.e., the web server). If the DB is running on that machine, all works fine; if it isn't, no connection is possible. Which of these is the case in your situation?

The two principal ways around this are to sign the applet, or to alter the local security policy; both are described here.
[ April 28, 2007: Message edited by: Ulf Dittmer ]
 
mintu kumar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for replying.
but i m still in doubt.
is it mandatory to sign an applet?
i m running my applet on my pc and oracle database is already installed there.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you said you were having problems, although you didn't say what kind of problems, or whether there were any exceptions. The most common source of problems with applets connecting to databases is cured by signing the applet. If you told us the specifics of your problem, maybe we could advise more specifically.
 
mintu kumar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here i m running the following code .
it executes the following query "Select * from emp";
the number in the text area decides the column number to be displayed.

import java.awt.*;
import java.applet.*;
import java.sql.*;
/*<applet code=project.class width=300 height=200>
</applet>*/

public class project extends Applet
{
Statement stmt=null;
Connection conn = null;
TextField t1;
public void init()
{
t1 = new TextField(2);
add(t1);
t1.setText("0");
}
public void paint(Graphics g)
{
int x=0,y=0,z=0;
String s1,s2,s;
try{
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
}
catch(Exception e)
{}
try{
conn = DriverManager.getConnection
("jdbc:oracle:thin:@anant:1521:student", "scott", "tiger");
stmt = conn.createStatement();
}
catch(Exception e)
{}
s1 = t1.getText();
x = Integer.parseInt(s1);
try{
ResultSet rset = stmt.executeQuery("select * from emp");
while (rset.next())
{
s2 = (rset.getString(x));
g.drawString(s2,100,100);
}
stmt.close();

}
catch(Exception e){}
}
public boolean action(Event event,Object obj)
{
repaint();
return true;
}
}

i m getting following error.

Exception in thread "AWT-EventQueue-1" java.lang.NoClassDefFoundError: oracle/jdbc/OracleDriver
at project.paint(project.java:23)
at java.awt.Container.update(Container.java:1730)
at sun.awt.RepaintArea.updateComponent(RepaintArea.java:239)
at sun.awt.RepaintArea.paint(RepaintArea.java:216)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:254)
at java.awt.Component.dispatchEventImpl(Component.java:4031)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:234)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)


this error comes when i run every jdbc applet.
can you suggest any solution.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you know what a "NoClassDefFoundError" is? Where is the JDBC driver (i.e., the jar file) located? The applet tag you quote doesn't include an "archive" tag, so this is most likely a classpath isue, which is trivial to correct.
 
mintu kumar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sir , i have included the drivers in the classpath. but i m still getting the
the same error. i have put archive.jar there, which contains ojdbc14.jar,
ojdbc14_g.jar, classss111.jar etc.
thanks for giving so much attention to my problem.
can you please try to run this program.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i have included the drivers in the classpath.


Applets do not use the classpath, so this won't help.

i have put archive.jar there, which contains ojdbc14.jar,
ojdbc14_g.jar, classss111.jar etc.


Jar files within jar files won't work for accessing class files by the JVM. A jar file must contain class files directly.

Also note that the applet tag does not contain an "archive" attribute, which is used to tell the JVM which jar files contain classes used by the applet. It is described here.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic