wood burning stoves*
The moose likes JDBC and the fly likes JDBC applets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "JDBC applets" Watch "JDBC applets" New topic
Author

JDBC applets

Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

I can run JDBC applications just fine, but when I try to run an applet I get a ClassNotFoundException when it looks for my driver. There are 2 drivers available in DB2, one for applications and one for applets. I have double and triple checked, and I am using the correct one. So, I tried copying the DB2Driver.class over to the same directory where my HTML and java files are, and this corrected the ClassNotFoundException error, but now I get this error
java.lang.NoClassDefFoundError: DB2Driver (wrong name: COM/ibm/db2/jdbc/net/DB2Driver) Does anyone have a clue as to how to fix this problem? I would be forever greatful!
Here is my code:
Note: Class.forName("DB2Driver"); orginally said Class.forName("COM/ibm/db2/jdbc/net/DB2Driver"); before I copied the driver to the HTML directory.


[This message has been edited by Jim Yingst (edited September 05, 2000).]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I put in [ code ] tags to improve the readability of your code. You can learn more about them here.
As for your question, I think the problem is that inside the DB2Driver.class file the class identifies itself as part of the package COM.ibm.db2.jdbc.net. As such, the class file needs to be located in the appropriate set of subdirectories, namely COM/ibm/db2/jdbc/net/DB2Driver.class. (Change / to \ as necessary if you're using Windows of course.) The top of that subdirectory structure (the COM directory) should be in some directory that's in your CLASSPATH, such as the directory you've already put all your other class files in. You don't need all the subdirectories in the path (COM, ibm, etc.) but you do need to use the fully-qualified class name in your code: <code>Class.forName("COM.ibm.db2.jdbc.net.DB2Driver");</code> Hopefully that should do it.


"I'm not back." - Bill Harding, Twister
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I ran into this problem 10 months ago...
Make sure all of the IBM provided jar files are in your CLASSPATH. If you are still having problems.
Do a search in the JDBC for for monty6.
I made a post containing code that interacts with db2 via db2 connect. fyi, the data source name in this example is DSNT.
also, check you html tag(s)
<pre>
applet code="OutputApplet.class" archive="jar/ibm.jar" /applet
</pre>
Something like this.... check you documentation.
Hope this helps... Stay in touch...
Monty6

[This message has been edited by monty6 (edited September 05, 2000).]
[This message has been edited by monty6 (edited September 06, 2000).]
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Okay, I put the IBM jar files in the same directory as my HTML and class files (actually, they are zip files, not jar files). I also put the tag archive="db2java.zip" in the HTML. I followed the documentation from IBM to the letter. Anyway, this corrected one error and made another. Now I get a NoSuitableDriver Error. The driver that is used for applets from DB2 is clearly there in the db2java.zip. I don't understand why it is not suitable. Also, the DB2 documentation says to start the DB2's JDBC applet server on your Web server by typing 'db2jstrt portno' before running the applet. Do you know what this is for? I have searched and seached IBM's web site, but could not find any more useful information. Any ideas? Thanks!!!
Oh yes, I did load the driver with the fully-qualified class name just like Jim said to do.
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Okay, so I copied all the IBM jar files to the same directory as HTML and class files (actually they are zip files, not jar files). I also ammended my HTML with this tag 'archive="db2java.zip"' I followed the IBM documentation to the letter, and I got rid of one error and now have NoSuitableDriver Error. The driver that DB2 says to use for applets is clearly there in the db2java.zip file, so I do not understand why it is not suitable. Also in the documentation, it says to start DB2's JDBC applet server on your web server by typing 'db2jstrt portno' before running the applet. Can anyone briefly explain what this is for? I have searched IBM's web site for hours looking for more useful information. Anyone that can help will be greatly appreciated!!!
Oh yes, I loaded the driver with the fully-qualified class name just like Jim said to do. So, Class.forName() has been changed in the code.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
DB2 html java jdbc example:
html
<code>
APPLET CODE=JdbcTesta.class WIDTH=475 HEIGHT=400> /APPLET
</code>
Applet
<code>
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
public class JdbcTesta extends Applet {
Label lb1 = new Label( "Userid" );
TextField tf1 = new TextField(15);
Label lb2 = new Label( "Password" );
TextField tf2 = new TextField(15);
Button bt1 = new Button("Execute Sql");
Button bt2 = new Button("Reset Sql");
Button bt3 = new Button("Reset Result Set");
Button bt4 = new Button("Reset All");
Label lb3 = new Label( "Sql Code" );
String sql = "select col_name " +
"from dbname.tbname " +
"where partition_nbr = '09' " +
"and col_name = 'ED5197782';";
TextArea ta1 = new TextArea( sql, 05, 40, TextArea.SCROLLBARS_BOTH );
Label lb4 = new Label( "Result Set" );
TextArea ta2 = new TextArea( "", 05, 40, TextArea.SCROLLBARS_BOTH );
public void init() {
Panel p0 = new Panel();
p0.setLayout( new BorderLayout() );
Panel p1 = new Panel();
p1.setLayout( new GridLayout( 2, 4) ) ;
p1.add( lb1 );
p1.add( tf1 );
p1.add( lb2 );
p1.add( tf2 ); tf2.setEchoChar('*');
bt1.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
boolean editTF;
editTF = false;
editTF = editTFInput();
if ( editTF ) {
executeSql();
} else {
showStatus("Invalid input - Try Again ...");
}// end of if
}// end of actionPerformed method
} );// end of anaonymous class 'ActionListener'
p1.add( bt1 );
bt2.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta1.setText("");
showStatus("Reset Sql ...");
}// end of actionPerformed method
} );// end of anaonymous class 'ActionListener'
p1.add( bt2 );
bt3.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta2.setText("");
showStatus("Reset Result Set ...");
}// end of actionPerformed method
} );// end of anaonymous class 'ActionListener'
p1.add( bt3 );
bt4.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta1.setText("");
ta2.setText("");
showStatus("Reset All ...");
}// end of actionPerformed method
} );// end of anaonymous class 'ActionListener'
p1.add( bt4 );
p0.add( p1, BorderLayout.NORTH );
Panel p2 = new Panel();
p2.setLayout( new GridLayout( 4, 4 ) ) ;
p2.add( lb3 );
p2.add( ta1 );
p2.add( lb4 );
p2.add( ta2 );
p0.add ( p2, BorderLayout.CENTER );
this.add( p0 );
} // end of init
public boolean editTFInput() {
boolean editTF;
editTF = true;
if ( tf1.getText().length() != 7 ) { editTF = false; }
if ( tf2.getText().length() < 6 | | tf2.getText().length() > 8 ) {
editTF = false; }
return editTF;
}// end if method editTFInput

public void executeSql() {
try {
String userid, password;
Vector queryResults = new Vector();
int accum1 = 0;
int accum2 = 0;
showStatus("Loading JDBC Driver ..." );
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
showStatus("Connecting To jdbc:db2 SNP ..." );
userid = tf1.getText().trim().toUpperCase();
password = tf2.getText().trim().toUpperCase();
Connection con =
DriverManager.getConnection("jdbc:db2 SNT", userid, password );
showStatus("Creating SQL Statement ..." );
String sqlsel1 = ta1.getText();
showStatus("Creating Java Statement Class ..." );
Statement stmt = con.createStatement();
showStatus("Executing SQL VIA JAVA Statement ..." );
ResultSet rs = stmt.executeQuery( sqlsel1 );
showStatus("Processing Result Set VIA JAVA Statement ..." );
// result set meta data
ResultSetMetaData meta = rs.getMetaData();
// add java result set to a vector
while ( rs.next() ) {
StringBuffer strbuf = new StringBuffer();
String col1 = rs.getString( "col_name" );
strbuf.append( col1 ) ;
int col2 = 1;
accum1 = accum1 + col2;
accum2 = accum2 + col2;
String rows = strbuf.toString();
queryResults.addElement( rows );
strbuf.setLength( 0 );
// if ( accum1 >= 0 ) {
// strbuf.append( col1 + " " + col2 + " " + accum1 + " " + accum2 +"\n" );
// String rows = strbuf.toString();
// strbuf.setLength( 0 );
// queryResults.addElement( rows );
// accum1 = 0;
// } // end of if
} // end of while
// add vector to a text area component
Enumeration enum = queryResults.elements();
while (enum.hasMoreElements()) {
String rows = (String)enum.nextElement();
ta2.append( rows );
} // end of while
showStatus("Processing Completed ..." );
queryResults.removeAllElements();// empty vector
rs.close();// close ResultSet
stmt.close();// close Statement
con.close();// close Connection
}// end try block

catch(ClassNotFoundException e) {
showStatus("ClassNotFoundException!!!: " );
ta2.append("ClassNotFoundException!!!: " + e ) ;
}
catch(SQLWarning e) {
showStatus("SQL_CODE !!!: " );
ta2.append("SQL_CODE !!!: " + e ) ;
}
catch(SQLException e) {
showStatus("SQL_CLASS !!: " );
ta2.append("SQL_CLASS !!: " + e ) ;
}// end of catch block(s)
} // end of executeSql
}// end of class JdbcTesta
</code>
Hope this helps,
Monty6
[This message has been edited by monty6 (edited September 06, 2000).]
[This message has been edited by monty6 (edited September 06, 2000).]
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Thanks monty6 for the code, however I cannot get this to work either. Now I am getting security errors. I am curious as to why you used the COM.ibm.db2.jdbc.app.DB2Driver as opposed to the COM.ibm.db2.jdbc.net.DB2Driver, because supposedly the 'net' driver is the one IBM says to use for applets. Does this make any difference?
Here is the error I get when I hit the Execute SQL button from your code:
java.lang.ExceptionInInitializerError: java.security.AccessControlException: access denied (java.util.PropertyPermission ibm.db2.instance.path read)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:195)
at java.security.AccessController.checkPermission(AccessController.java, Compiled Code)
at java.lang.SecurityManager.checkPermission(SecurityManager.java, Compiled Code)
at java.lang.SecurityManager.checkPropertyAccess(SecurityManager.java, Compiled Code)
at java.lang.System.getProperty(System.java, Compiled Code)
at COM.ibm.db2.jdbc.app.DB2Driver.<init>(DB2Driver.java:154)
at COM.ibm.db2.jdbc.app.DB2Driver.<clinit>(DB2Driver.java:118)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:124)
at JdbcTesta.executeSql(JdbcTesta.java, Compiled Code)
at JdbcTesta$1.actionPerformed(JdbcTesta.java:43)
at java.awt.Button.processActionEvent(Button.java:308)
at java.awt.Button.processEvent(Button.java:281)
at java.awt.Component.dispatchEventImpl(Component.java, Compiled Code)
at java.awt.Component.dispatchEvent(Component.java, Compiled Code)
at java.awt.EventQueue.dispatchEvent(EventQueue.java, Compiled Code)
at java.awt.EventDispatchThread.pumpOneEvent(EventDispatchThread.java, Compiled Code)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:92)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:83)
What do I need to do to get past this Exception? Do I need to modify my java.policy file? Modify source code? Modify HTML? or what? Thanks in advance for this tremendous help!! I did not know that applets could be this difficult. The JDBC Tutorial does not cover this stuff.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I'm :confussed: ....
I coded my applet and java code in a 2 days ...
I ran my applet on and NT box.
Connected to DB2 via DB2 Connect.
DB2 Connect had two data source names define to my userid.
There were DSNT and DSNP. Standard db2 sub-system names.
I have never run into the errors you are getting.
Still very :confussed:...!
I'll take to some of my db2 dba friends to see if they can
shead and light on the subject?
monty6
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I also ran into a simular security problem.
The security problem has been bugging me for the past 4 days.
To answer your question yes.
You do need to modify your ".java.policy" file.
Please review PolicyTool post for more info..
Monty6
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

I cannot believe I am having so much difficulty with these Applets. I did exactly what you said with the policytool, and now I am passed the security exceptions. I have a connection, but now I am getting this exception:
SQL_CLASS !!: COM.ibm.db2.jdbc.DB2Exception: [IBM][CLI Driver] SQL1032N No start database manager command was issued. SQLSTATE=57019
Does anyone know what this means? Very confused...
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Contact your DBA...
Being a DBA in a past life....
It sounds to me it is one of three things.
#1 your database on the mainframe was not available.
due to a db2 utility or it was varied of line from some reason
#2 if you are going thru a servier.... you connection was made
to the server and the gateway and/or connection from the
server to the mainframe was not started.
#3 check you code... make sure you do not have a typo...
( case maybe important )
Hope this helps...
Monty6
------------------
 
GeeCON Prague 2014
 
subject: JDBC applets