aspose file tools*
The moose likes JDBC and the fly likes java applet-oracle database connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "java applet-oracle database connection" Watch "java applet-oracle database connection" New topic
Author

java applet-oracle database connection

mintu kumar
Greenhorn

Joined: Apr 28, 2007
Posts: 10
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.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42648
    
  65
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 ]

Ping & DNS - my free Android networking tools app
mintu kumar
Greenhorn

Joined: Apr 28, 2007
Posts: 10
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
Marshal

Joined: Mar 22, 2005
Posts: 42648
    
  65
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

Joined: Apr 28, 2007
Posts: 10
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
("jdbcracle: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
Marshal

Joined: Mar 22, 2005
Posts: 42648
    
  65
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

Joined: Apr 28, 2007
Posts: 10
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
Marshal

Joined: Mar 22, 2005
Posts: 42648
    
  65
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java applet-oracle database connection